Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

MyDate Class using java

You will define a class that has a default constructor, a regular constructor, and a copy constructor. The class will also have toString, lessThan, and equals methods.
Create a class called “MyDate”. The MyDate class must have integer instance variables for the day, month, and year. MyDate must have a constructor that accepts a string, in the format “DD/MM/YYYY”, that initializes the day, month, and year instance variables. MyDate must have a copy constructor that accepts another MyDate object as its argument, performing a deep copy of it. MyDate must have a default (no-argument) constructor that initializes the date to 00/00/0000.
The toString method of MyDate must return a string giving the day, followed by the month name, followed by a comma, followed by the last two digits of the year. For example “16 December, 87” is valid. “April 3 2014” is invalid.
MyDate must have a method called lessThan, which accepts another MyDate object, and compares the argument to the current object. If the current date is less than the given date, it returns true. Else, it returns false.
Lastly, MyDate must have an equals method. Equals must accept another MyDate object, and compares the argument to the current object to see if all of their values are the same.
Be sure to use proper privacy modifiers. Using the given tester class, you will be able to tell if you have defined your MyDate correctly (for the most part).

Answer:

MyDate Class

import java.util.*;

public class MyDate {
	private int day, month, year; 
	
   /*
	* This constructor accepts a string, in the format “DD/MM/YYYY”, 
	* that initializes the day, month, and year instance variables.
	*/
	public MyDate(String Date){
		StringTokenizer myTokens = new StringTokenizer(Date,"/");
		this.day = Integer.parseInt(myTokens.nextToken());
		this.month = Integer.parseInt(myTokens.nextToken()); 
		this.year = Integer.parseInt(myTokens.nextToken());
	}
	
	
   /*
	* This copy constructor accepts another MyDate object as its argument,
	* performing a deep copy of it.
	*/
	public MyDate(MyDate aDate){
		day = aDate.day; 
		month = aDate.month; 
		year = aDate.year; 
	}
	
   /*
	* This is a default (no-argument) constructor that initializes the date to 00/00/00
	*/
	public MyDate(){
		String day = "00"; 
		String month = "00"; 
		String year = "0000"; 
		System.out.println(day+"/"+month+"/"+year);
	}
	
   /*
	* toString method of MyDate returns a string giving the month
	* name, followed by the day, followed by a comma, followed by
	* the last two digits of the year
	*/
	public String toString(){
		String monthName = ""; 
		int lastDigits = year % 100; 
		if(month == 1){monthName = "January"; }
		if(month == 2){monthName = "February"; }
		if(month == 3){monthName = "March"; }
		if(month == 4){monthName = "April"; }
		if(month == 5){monthName = "May"; }
		if(month == 6){monthName = "June";}
		if(month == 7){monthName = "July";}
		if(month == 8){monthName = "August"; }
		if(month == 9){monthName = "September"; }
		if(month == 10){monthName = "October";}
		if(month == 11){monthName = "November";}
		if(month == 12){monthName = "December";}
		
		if(lastDigits < 10){
        return day + " " + monthName  +  ",0" + lastDigits;  
        }
        return day + " " + monthName  +  "," + lastDigits;
	}
	
	/*
	 * This method called lessThan, which accepts another MyDate object,
	 * and compares the argument to the current object. If the current 
	 * date is less than the given date, it returns true. Else, it returns false.
	*/
	public boolean lessThan(MyDate d){
		if (year < d.year || (year == d.year && month < d.month) || (year ==d.year && month == d.month && day < d.day))
			return true;
		else
			return false;
	}
	
	/*
	 * This method called equals method which accepts another MyDate object, and compares
	 * the argument to the current object to see if all of their values are the same.
	 */
	public boolean equals (MyDate d){
		if (year == d.year && month == d.month && day == d.day)
			return true;
		else
			return false;
	}
	
	
}

Tester Class

public class Tester {
	public static void main(String args[]){    
		MyDate dateArray1[], dateArray2[], anotherDate;
		int index1, index2;

		dateArray1 = new MyDate[3]; // Create an array of MyDate
		dateArray1[0] = new MyDate("26/05/1999"); // Create three elements of dataArray1 by creating three objects of class MyDate
		dateArray1[1] = new MyDate("08/06/1994");
		dateArray1[2] = new MyDate("15/03/2000");

		dateArray2 = new MyDate[3];
		dateArray2[0] = new MyDate("25/05/1999"); // Create three elements of dataArray1 by creating three objects of class MyDate
		dateArray2[1] = new MyDate("25/01/2000");
		dateArray2[2] = new MyDate("26/05/1999"); 

		for (index2 = 0; index2 < dateArray2.length;index2++){   
			for (index1 = 0; index1 < dateArray1.length; index1++){   
				if (dateArray2[index2].lessThan(dateArray1[index1])) // Compare each elements in dataArray1 with each element of dateArray2
					System.out.println(dateArray2[index2].toString() 
							+ " is less than " 
							+ dateArray1[index1].toString());
				else System.out.println(dateArray2[index2].toString() 
						+ " is not less than " 
						+ dateArray1[index1].toString() + " \n");
			} 
		}
		
		if (dateArray2[2].equals(dateArray1[0])){
			System.out.println(dateArray2[2].toString() 
					+ " is the same date as " 
					+ dateArray1[0].toString());
			
		} else {
			System.out.println("Cannot happen!!");
		}
		
		if (dateArray2[2].equals(dateArray1[1])){
			System.out.println(dateArray2[2].toString() 
					+ " is not the same date as " 
					+ dateArray1[0].toString() + "\n are you sure this is OK?");
			
		} else {
			System.out.println("You got it right!!");
		}
		
		anotherDate = new MyDate(dateArray2[1]);
		if (dateArray2[1].equals(anotherDate)){
			System.out.println(dateArray2[1] 
					+ " is the same date as " 
					+ anotherDate);
			
		} else {
			System.out.println("Are you sure???");
		}
		
	}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!