Assignment 2

2.1

Implement a class, named Rational, for reduced-form rational numbers as specified in the following:

class Rational {
	int numerator;
	int denominator;
 
	public Rational(int numerator, int denominator){
                // initializes the member variables
        }
	public Rational add(Rational r){
		// return the sum of this and r.
	}
	public Rational sub(Rational r){
		// return the difference between this and r (this-r).
	}
	public Rational mult(Rational r){
		// return the product of this and r.
	}
	public Rational div(Rational r){
		// return the quotient of this divided by r.
	}
}

A rational number is in the reduced form if the greatest common divisor of the numerator and the denominator is 1.

2.2

Design and implement a class 'ParkingMeter' for parking meters. A user can insert quarters and check remaining parting time. The constructor should take the maximum parking minutes and the rate (minutes per quarter).

2.3

Implement a class 'Car' with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a method 'drive' that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and methods 'getGas', to return the current fuel level, and 'addGas' to tank up. Sample usage:


   Car myBeemer = new Car(29); // 29 miles per gallon
   myBeemer.addGas(20);        // tank 20 gallons
   myBeemer.drive(100);        // drive 100 miles
   System.out.println(myBeemer.getGas());