Programming Exercise 4
Simple Classes

These problems are taken from Mark Weiss' Data Structures and Problem Solving Using Java book. 

1.    Write a class to represent rational numbers. The fields should be two ints, one that stores the numerator and one for the denominator. Store the rational number in reduced form, with the denominator always non negative.

Provide a reasonable set of constructors and the methods add, subtract, multiply, divide, toString, equals, and compareTo. Make sure the toString handles the case when the denominator is negative.

 

2.    Write a class to represent complex numbers.

You can refresh your memory on complex numbers at any of a number of websites such as

http://www.ping.be/math/complget.htm or http://www.clarku.edu/~djoyce/complex/ or http://www-ccrma.stanford.edu/~jos/r320/Complex_Numbers.html

Implement a reasonable set of constructors and the methods add, subtract, multiply, divide, toString, and equals. Also provide accessors for the real and imaginary parts.

 

3.    Write a class to represent a standard playing card. Playing cards have a suit (hearts, spades, clubs, diamonds) and a value (two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, and ace). Decide what method the class should have.