Java Reference
In-Depth Information
Programming Projects
1. Write a class called RationalNumber that represents a fraction with an integer numerator and denominator.
A RationalNumber object should have the following methods:
public RationalNumber(int numerator, int denominator)
Constructs a new rational number to represent the ratio (numerator/denominator). The denominator cannot be 0 ,
so throw an IllegalArgumentException if 0 is passed.
public RationalNumber()
Constructs a new rational number to represent the ratio (0/1).
public int getDenominator()
Returns this rational number's denominator value; for example, if the ratio is (3/5), returns 5 .
public int getNumerator()
Returns this rational number's numerator value; for example, if the ratio is (3/5), returns 3 .
public String toString()
Returns a String representation of this rational number, such as "3/5" . You may wish to omit denominators of 1 ,
returning "4" instead of "4/1" .
An extra challenge would be to maintain your RationalNumber objects in reduced form, avoiding rational numbers
such as 3/6 in favor of 1/2 , or avoiding 2/-3 in favor of -2/3 . Another possible extra feature would be methods to
add, subtract, multiply, and divide two rational numbers.
2. Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the
following methods:
public Date(int year, int month, int day)
Constructs a new Date object to represent the given date.
public void addDays(int days)
Moves this Date object forward in time by the given number of days.
public void addWeeks(int weeks)
Moves this Date object forward in time by the given number of seven-day weeks.
public int daysTo(Date other)
Returns the number of days that this Date must be adjusted to make it equal to the given other Date .
public int getDay()
Returns the day value of this date; for example, for the date 2006/07/22, returns 22 .
public int getMonth()
Returns the month value of this date; for example, for the date 2006/07/22, returns 7 .
public int getYear()
Returns the year value of this date; for example, for the date 2006/07/22, returns 2006 .
public boolean isLeapYear()
Search WWH ::




Custom Search