Java Reference
In-Depth Information
(year == otherDate.year && getMonth() < otherDate.getMonth())
||
(year == otherDate.year && month.equals(otherDate.month)
&& day < otherDate.day) );
}
The version of our date class with this method is given in Display 4.7. The other new
methods in that class will be discussed shortly in the subsection entitled “The Methods
equals and toString .” Right now, let's discuss this new method named precedes .
An invocation of the method precedes has the following form, where date1 and
date2 are two objects of our date class:
date1.precedes(date2)
This is a Boolean expression that returns true if date1 comes before date2 . Because it
is a Boolean expression, it can be used anyplace a Boolean expression is allowed, such as
to control an if-else or while statement. For example,
if (date1.precedes(date2))
System.out.println("date1 comes before date2.");
else
System.out.println("date2 comes before or is equal to date1.");
Display 4.7
A Class with Methods equals and toString (part 1 of 2)
1 import java.util.Scanner;
2 public class DateFourthTry
3{
4
private String month;
5
private int day;
6
private int year; //a four digit number.
7
public String toString()
8
{
9
return (month + " " + day + ", " + year);
10
}
11
public void writeOutput()
12
{
13
System.out.println(month + " " + day + ", " + year);
This is the method equals in the
class DateFourthTry.
This is the method
equals in the class
String.
14
}
15 public boolean equals(DateFourthTry otherDate)
16 {
17 return ( (month.equals(otherDate.month))
18 && (day == otherDate.day) && (year == otherDate.year) );
19
}
(continued)
 
Search WWH ::




Custom Search