next up previous
Up: APS105S Home

APS 105S - Computer Fundamentals

Assignment #4: Objects and Classes

Winter 1999

(To be completed for your lab period February 1/2)

 

Objective:   To learn the fundamentals of object-based programming.

1. A Bank Account Management Program

This part is optional and will not be marked. Doing it will help you understand the differences between objects and static methods.

Modify your assignment #3 to use an Account object. The object will contain all of the state variables about the account, such as its balance, lowest balance since interest was applied, and the interest rate. Remember to keep these variables private to the class.

2. A Rational Class

Write a class Rational for peforming arithmetic with fractions. A rational object uses an integer for the numerator and another integer for the denominator.

Write the following methods for the Rational class:

1.
public void add(Rational) adds another rational to this one
2.
public void subtract(Rational) subtracts another rational from this one
3.
public void multiply(Rational) multiplies another rational to this one
4.
public void divide(Rational) divides another rational from this one
(note: beware of integer division! use multiplication instead)
5.
public void reduce() removes common factors from the numerator and denominator
6.
public double eval() returns a double (numerator divided by denominator)
7.
public static Rational getRational(String) prints String as a prompt, asks for the numerator and denominator, returns a Rational object
8.
public String toString() returns a string, "3/8" for example
9.
public Rational() default constructor
10.
public Rational( int numerator, int denominator ) constructor giving initial numerator, denominator values

Write a short main routine which accesses these methods. Like the bank account program, it should remember one rational number and ask you if you want to add, subtract, multiple, divide, reduce, print, or evaluate to this rational number.

For +-*/, it must prompt you for the numerator and denominator of another rational number to operate with.

The reduce() method should only remove common factors; it does not print out any result. Instead, the print option will call the toString() method and print the result. The evaluate() method returns the value of fraction as a double, and the main program should print this result.


next up previous
Up: APS105S Home
Guy G. Lemieux
1999-01-30