CMPS109 Programming Assignment 2

 

Objective

The objective of this assignment is to make us of inheritance and create a new abstract data type using the Java class mechanism.

Rational Numbers

 

Rational numbers are numbers that can be represented as a ratio an integer and a non-zero positive integer. The are sometimes useful when precision is very important. For example, 1/3 cannot be precisely represented using any of the standard numeric types in Java (or most other widely used programming languages).

For this assignment you must create a class to represent the new type “rational number”. You should call your class (type) Rational. You new class should be a subclass of the standard Java class java.lang.Number. You will use your new type to implement a rational number Polish notation calculator (see Section 12.6 in the text). The main class of your program must be called Calculator.

In addition to the methods required by java.lang.Number, Your class must also provide methods for addition, subtraction, multiplication, and division of two rational numbers, and likewise for a rational and an int. The methods for implementing these binary operations must be done in proper OOP style as discussed in Section 6.9 of the text. That is they should be instance methods (not static methods) that take only one explicit parameter (the right hand operand of the binary operator). The left hand operand of the binary operation is the implicit object being operated on (i.e. “this”).

Finally, your class must include a toString() method that can be used to display rational numbers in the format nnn/ddd, where nnn is the numerator of the rational number (possibly including a leading minus sign) and ddd is the denominator of the rational number.

 

A Simple Rational Number Calculator

 

You must use your new class to build a rational number calculator that implements the following specification.

Input: The input will be from standard input (which will be redirected to read from a file for obvious reasons[1]). The input (file) will contain a sequence of “commands” one per line. A command will not contain any white space characters. Any characters on a line following a white space character (space or tab) should be ignored. The possible commands are:

 

Output:

 

 

You must always print rational numbers in simplest form (no common factors except 1 in the numerator and denominator).

Print any rational with a zero numerator simply as “0”.

Print any rational with one in the denominator as an integer (just the numerator - no slash).

 

The conversion commands are admittedly a bit artificial but they allow for full testing of the required functionality of the class Rational.

 

Here is a sample input file and the corresponding output when this input file is read using file redirection.

 

Sample Input:

 

-1/3

1/3

+

1/2

+

3/4

*

3/8

/

2

3

+

*

300001/3

double

float

long

int

short

byte

dup

+

top

pop

0

+

 

Sample Output:

 

-1/3

1/3

0

1/2

1/2

3/4

3/8

3/8

1

2

3

5

5

300001/3

100000.33333333333

100000.336

100000

100000

-31072

-96

300001/3

600002/3

600002/3

600002/3

0

5

 

Additional Grading Criteria

 

Your program will have missed one of the key concepts of this assignment if the binary operator methods of your class Rational are not properly designed (i.e. one explicit parameter). Failure to design and implement them correctly can result in the loss of up to 25 points in the functionality portion of the grade. This comes under functionality (which might better be called “correctness”) because improper design would interfere with reusing your Rational class in other applications.

 

 



[1] Be sure and test your program using file redirection. Some input operations behave slightly differently when reading from the console and when reading from a redirected file. Specifically, if you erroneously open the same file multiple times, this may work when typing at the command line but will fail with file redirection.