Reputation: 11
Anyway - The question / confusion that I have involves the first bit of code that I have in my program to test the compareTo
method.
Would I use the variables at the top of the code as my variables in the static void main
area, or assign new variables, like I do have?
The values in public Date()
... <-- Is that the date that my code in static void main
is comparing to? (If so, I have a piece of code that I want to use that uses the current date, rather than what's in Date()
).
I may have more questions later on, but I hope that someone can clear up my confusion better than my book or google has proven thus far.
package date;
import java.util.*;
public class Date implements Comparable
{
static Scanner console = new Scanner(System.in);
private int dMonth; //Part a of confusion 1
private int dDay;
private int dYear;
public Date()
{
dMonth = 1; //Confusion 2
dDay = 1;
dYear = 1900;
}
public Date(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
public void setDate(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
public int getMonth()
{
return dMonth;
}
public int getDay()
{
return dDay;
}
public int getYear()
{
return dYear;
}
public String toString()
{
return (dMonth + "." + dDay + "." + dYear);
}
public boolean equals(Object otherDate)
{
Date temp = (Date) otherDate;
return (dYear == temp.dYear
&& dMonth == temp.dMonth
&& dDay == temp.dDay);
}
public int compareTo(Object otherDate)
{
Date temp = (Date) otherDate;
int yrDiff = dYear - temp.dYear;
if (yrDiff !=0)
return yrDiff;
int monthDiff = dMonth - temp.dMonth;
if (monthDiff !=0)
return monthDiff;
return dDay - temp.dDay;
}
public static void main(String[] args) //Part b of confusion 1
{
int month;
int day;
int year;
Date temp;
System.out.print("Enter date in the form of month day year");
month = console.nextInt();
day = console.nextInt();
year = console.nextInt();
System.out.println();
}
}
Upvotes: 1
Views: 133
Reputation: 22461
dMonth
, dDay
and dYear
are member variables. If you want to use them directly inside your main
method you will have to use the keyword static
so that they become class variables. But no, that is not what you want.Your main method is doing nothing useful really. Your confusion point 2 is a constructor:
Date d = new Date(); // Data Instance -> First constructor
d.getMonth(); // 1
d.getDay(); // 1
d.getYear(); // 1900
Date d2 = new Date(2, 2, 1901);
d2.getMonth(); // 2
d2.getDay(); // 2
d2.getYear(); // 1901
d2.setDate(3, 3, 1902);
d2.getMonth(); // 3
d2.getDay(); // 3
d2.getYear(); // 1902
d.getMonth(); // Still 1 since member variables of d are independent of d2
d.compareTo(d2); // -2 -> (1900 - 1902)
You can create date instances inside your main method and use code like the one above to access member variables (probably the whole point of your exercise).
Upvotes: 2
Reputation: 10173
As mentioned in the comments, I think you need to read about the difference between static methods/attributes and the ones in instances. I think this is what you should be doing in the main
method:
System.out.print("Enter date in the form of month day year");
Date date1 = new Date(console.nextInt(), console.nextInt(), console.nextInt());
System.out.print("Enter second date in the form of month day year");
Date date2 = new Date(console.nextInt(), console.nextInt(), console.nextInt());
System.out.println("Comparison result:");
System.out.println(date1.compareTo(date2));
Regarding your confusion points:
private int dMonth; //Part a of confusion 1
private int dDay;
private int dYear;
These are special variables. Each instance (that is, every object created with new Date
) has its own value for dMonth
, dDay
and dYear
. It is not accessible from the main
because main
is a static method, and thus doesn't have access to instance variables.
If you didn't understand, at least you know the names to search further.
public Date()
{
dMonth = 1; //Confusion 2
dDay = 1;
dYear = 1900;
}
Those values are used when you create a new Date
object without specifying which month/day/year you want. So new Date(2, 3, 2013)
means 2/3/2013, while new Date()
means 1/1/1900.
Upvotes: 3