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.
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();
}
}