CMPS 134 Fall 2025
Prog. Assg. #6: The Fraction Instance Class
Due: 11:59pm, Tuesday, November 11

Specification

Welcome to the Fractions Calculator!

> 1/2 + 3/4
5/4

> -1/2 + 3/4
1/4

> -1/2 - -3/-4
-5/4

> 123/456 * 1/1
41/152

> 123/456 * 2/1
41/76

> 41/76 / 16/8
41/152

> 1/1 - 13/7
-6/7

> 4/7 = -8/-14
0/1

> 1/2 = 4/5
1/1

> quit
Goodbye.
For this assignment you are to complete the given Fraction instance class so that it satisfies its specification, as detailed in the comments that precede each method and that accompany the declarations of its instance variables. You will notice that several of its methods include the comment Stubbed, indicating that the method needs additional code to make it right.

As its name suggests, each instance of the Fraction class represents a fraction. More precisely, it represents a common (or simple) fraction, which consists of an integer numerator and a non-zero integer denominator (and represents the ratio between those two numbers, or what is known as a rational number).

Provided to you, for the purpose of testing your work, is the application program, FractionCalculator, which is shown "in action" to the right. (User input is in boldface.) As you can see, it prompts the user to enter a simple arithmetic expression in which an operator is applied to two fractions, and it responds with the result.1

Standard computer hardware processors are designed to perform operations directly upon both integers and real numbers, the latter represented using a floating point scheme, but they do not deal directly with rational numbers.

One advantage of operations on integers (vs. numbers stored in floating point notation) is that results produced are exact, assuming that they are within the range of values representable within whatever quantity of memory has been allocated to store the result. (E.g., In Java, a value of type int is restricted to being in the range -231 .. 231-1, because it is stored in a memory cell of 32 bits.)

The range (from low to high) of numbers representable using floating point notation (and the same quantity of memory) is much larger. However, operations on floating point numbers generally produce only approximate results, because of limited precision. (Loosely speaking, precision refers to the limit upon how many significant digits can be represented. Again, see Wikipedia's article on floating point arithmetic.)

Because a rational number is represented by two integers, operations performed upon such numbers need not suffer a loss of precision, as is the case with floating-point numbers.

1/2
12/4
2/8
1) "1/2" aka 1/2 accumulates to 1/2
2) "12/4" aka 3/1 accumulates to 7/2
3) "2/8" aka 1/4 accumulates to 15/4
The mean value is 5/4
Also provided to you is MeanFraction, a program that reads from a text file (such as data0.txt or data1.txt) containing a sequence of fractions, one per line, determines the mean, and prints it. (Well, it also prints a running commentary identifying each fraction in the file and the running total as it sums them.) See the figures to the left and right, which show a data file and what the program produces when fed it. You can use MeanFraction, along with FractionCalculator, to test your work.


Program Submission

As usual, the Java source code file (Fraction.java) that you submit (to the relevant Brightspace folder) must contain comments identifying all agents (i.e., persons and AI entities) with whom you collaborated and listing any defects of which you are aware.


Footnotes

[1] The = operator is for the purpose of comparing two fractions to see if they are equal. The result is the fraction 0/1 if they are equal and 1/1 if they are not.