Java MyDate code

Status
Not open for further replies.

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hello all,

Pretty much got all the code done just confused on one part of the assignment, part A. How would I pass in an instance?


Here's the assignment, there were more that I deleted, was just get/set methods and all those work:

a. Write a method called Compare. This method should take a MyDate object as a parameter. If the passed in object contains a date that is before the date stored in the current class instance then return -1. If the date is the same then return 0. If the passed in object contains a date that is after the one stored in the current instance then return 1.
The method signature should be:
int Compare (MyDate other)

b. Write a method called Show. This method should print the date on the screen.
The method signature should be:
void Show()

c. Inside of main() create a few instances of MyDate.

d. Inside of main() you should write code that demonstrates that the MyDate class works properly for ALL of its methods.


Code:
/**
 * @(#)MyDate.java
 *
 * MyDate application
 *
 * @author 
 * @version 1.00 2011/2/20
 */
 
public class MyDate {
	private int year;
	private int month;
	private int day;
	
	public MyDate(){
		year = 2011;
		month = 2;
		day = 15;
	}
	
	public int GetYear(){
		return year;
	}
	
	public int GetMonth(){
		return month;
	}
    
    public int GetDay(){
    	return day;
    }
    
    public void SetYear(int newYear){
    	year = newYear;
    }
    
    public void SetMonth(int newMonth){
    	if(newMonth > 12 || newMonth < 0){
    		
    	}
    	else{
    	month = newMonth;
    	}
    }
    
    public void SetDay(int newDay){
    	day = newDay;
    }
    
    /*public int Compare(MyDate){
    	
    }*/
    
    public void Show(){
    	System.out.printf("%d/%d/%d\n",month,day,year);
    	
    }
    
    
    public static void main(String[] args) {
    	
    	MyDate x1 = new MyDate();
    	MyDate x2 = new MyDate();
    	MyDate x3 = new MyDate();
    	
    	x1.SetYear(2011);
    	x1.SetMonth(5);
    	x1.SetDay(20);
    	int a1 = x1.GetYear();
    	int b1 = x1.GetMonth();
    	int c1 = x1.GetDay();
    	
    	
    	x2.SetYear(2010);
    	x2.SetMonth(8);
    	x2.SetDay(2);
    	int a2 = x2.GetYear();
    	int b2 = x2.GetMonth();
    	int c2 = x2.GetDay();
    	
    	
    	x3.SetYear(2011);
    	x3.SetMonth(3);
    	x3.SetDay(15);
    	int a3 = x3.GetYear();
    	int b3 = x3.GetMonth();
    	int c3 = x3.GetDay();
    	
    	
    	x1.Show();
    	x2.Show();
    	x3.Show();
    
    }
}
 
Solution
i revise and add imports and method on your code...hope it helps, i am a newbie too ☺


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ADMIN
*/

public class MyDate {
private int year;
private int month;
private int day;

public MyDate(){
year = 2011;
month = 2;
day = 15;
}

public int GetYear(){
return year;
}

public int GetMonth(){
return month;
}

public int GetDay(){
return day;
}

public void SetYear(int newYear){
year = newYear;
}

public void SetMonth(int...

kyeana

Distinguished
May 21, 2008
230
0
18,860
Your compare function needs to have a name for MyDate, like:

Code:
public int Compare(MyDate date){ 
     // 'date' is now a MyDate object, which you can compare to this current
     // object
}
 

megeh_09

Distinguished
Mar 4, 2011
7
0
18,520
i revise and add imports and method on your code...hope it helps, i am a newbie too ☺


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author ADMIN
*/

public class MyDate {
private int year;
private int month;
private int day;

public MyDate(){
year = 2011;
month = 2;
day = 15;
}

public int GetYear(){
return year;
}

public int GetMonth(){
return month;
}

public int GetDay(){
return day;
}

public void SetYear(int newYear){
year = newYear;
}

public void SetMonth(int newMonth){
if(newMonth > 12 || newMonth < 0){

}
else{
month = newMonth;
}
}

public void SetDay(int newDay){
day = newDay;
}

//string to date converter...
private Date convertStringToDate(String stringDate) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

Date theDate = dateFormat.parse(stringDate);

return theDate;
}

//this is compare...it should have two object arguments...
public int Compare(MyDate d1, MyDate d2) throws ParseException{
return (convertStringToDate(String.valueOf(d1.GetMonth()).concat("/"+String.valueOf(d1.GetDay())).concat("/"+String.valueOf(d1.GetYear()))).
compareTo(convertStringToDate(String.valueOf(d2.GetMonth()).concat("/"+String.valueOf(d2.GetDay())).concat("/"+String.valueOf(d2.GetYear())))));
}

public void Show(MyDate d1, MyDate d2) throws ParseException{
System.out.printf("%d/%d/%d",month,day,year);
System.out.println("\t"+Compare(d1, d2));
}

public static void main(String[] args) throws ParseException {
MyDate x0 = new MyDate(); //default...
MyDate x1 = new MyDate();
MyDate x2 = new MyDate();
MyDate x3 = new MyDate();

x1.SetYear(2011);
x1.SetMonth(5);
x1.SetDay(20);
//... lines deleted


x2.SetYear(2010);
x2.SetMonth(8);
x2.SetDay(2);
//... lines deleted


x3.SetYear(2011);
x3.SetMonth(3);
x3.SetDay(15);
//... lines deleted

System.out.println("LEGEND:\n0 - [ = ]\n1 - [ > ]\n-1 - [ < ]\n"); //line temporary...to show comparison legend

x0.Show(x0, x0); //added to show default date...[ temporary ]
x1.Show(x1, x0);
x2.Show(x2, x1);
x3.Show(x3, x2);
}
}
 
Solution
Status
Not open for further replies.