Your first Java programming assignment is to write a small application.
Before starting this assignment, we assume that you are familiar with any
Java programming environment. You may
use Java compilers on different systems, or a programming environment
such as J++ or Cafe'.
Writing the programs
Create a class called Rational for performing arithmetic with fractions, and write a test program to use this class: (This is based on assignment 6.3 in the Deitels book.)
Use integer variables to represent private instance variables of the class - the numerator and the denominator. The fractions are always to be stored in lowest terms (i.e. if you get or compute 2/4, store 1/2 instead). You can do this by writing a reduce method as described below, which all the other methods can call.
Write one constructor function that that initializes an object with an initial numerator and denominator (and reduces to lowest terms). Write another constructor function that takes no arguments and initializes the number to some default value. 0 would be a good choice; note that 0 is stored as the rational number 0/1.
You should also write the following public methods. Since this is supposed to be an exercise in writing Java classes and not in remembering 6th grade arithmetic, all the definitions are given for you.
a/b + c/d = (a*d + c*b) / b*d
a/b - c/d = (a*d - c*b) / b*d
a/b * c/d = a*c / b*d
a/b < c/d iff (a*d < c*b)
// assume u > 0, v > 0 private int gcf (int u, int v) { int t; while (u > 0) { if (u < v) { t=u; u=v; v=t; } u = u - v; } return v; }You must also write a test program for this class. Write an application that reads a set of numerators and denominators from standard input, converting these to Rational numbers and storing them in an array (i.e. array elements are of type Rational). Now find the maximum number in the array and sum the array elements.
What to put on the homework page as the submission of your homework
Submit your assignment by linking the description of your program, a link to the Java source code and a small file showing sample output run (like my .run files) to your homework page.
One way to make the .run file if you're running on carver, or another Unix system, is to type "script MyProgram.run" at the command prompt. Then everything that is typed on your workstation is copied into this file. So run your program "java MyProgram" and when you're done, type ^D (control-D) at the command line to finish the script. Then the file MyProgram.run will show how your program runs.
For how to write a description of your homework and document your program, and other information, please see these homework guidelines.