Recitation R13 - The Fraction Class
CS160: Foundations in Programming
Setting up the Class
- Declare two instance variables of type int called numerator and denominator.
- Create a private static method called gcd.
- This method takes two integer parameters and returns an int. It is extremely
important that you correctly implement this method because all of our other
methods will depend on it.
- gcd returns the largest number that divides both integers evenly.
If you would like a more visual description, click here.
Testing Phase One
Because the gcd method will be used in one of our constructors, let's make sure that it's working properly. Call the gcd method in your main with the following inputs:
- 45 and 54 should yield 9
- -2 and 18 should yield 2
- 97 and 17 should yield 1
Creating the Constructors and the toString methods
- The first constructor should take two integers.
These integers should be simplified using the gcd method
before assigning them to the instance variables.
After simplifying, assign the first integer to the numerator
and the second integer to the denominator.
- The second constructor should take one integer.
Inside the constructor you can make a call to your first constructor by using
the following code: this(myInt, 1); myInt represents the parameter.
This will call the first contructor you made and assign a fraction using those arguments.
- Create two accessor methods called getNumerator and getDenominator. These methods should have no parameters and return ints. These methods should return the values of the instance variables.
- We need a way to represent the fractions. Create a toString method that returns the following String without a newline: numerator / denominator
Testing Phase Two
After the code you wrote to test the gcd method, create and print three Fractions:
Print all three fractions out and make sure your output matches the following:
- The first Fraction should take be instantiated with one integer, 7.
- The second Fraction should be instantiated with two integers, 6 and 8.
- The third Fraction should be instantiated with two integers, 5 and 6.
// Fraction one
7 / 1
// Fraction two
3 / 4
// Fraction three
5 / 6
In this next part you will create methods that add, subtract, multiply, and divide fractions.
- Each of the methods will have one parameter of type Fraction, called other, and return an Object of type Fraction.
- Each of these methods will be dealing with two Fractions.
- The numerator and denominator for your first Fraction is stored in the instance variables numerator and denominator.
- The numerator and denominator for the second Fraction can be accessed through the Fraction that is passed in by your parameter. To access the numerator call other.getNumerator() and to access the denominator call other.getDenominator().
- Create a public method called add.
- Inside the method create two int variables for the new Fractions numerator and denominator.
- These variables should hold the numerator and denominator of the two Fractions added together.
- If it has been a while since you have added fractions, go here for a refresher.
- Once you have your new values for numerator and denominator, create a new Fraction and return it.
- Create a public method called subtract. This method very similar to your last method, however, instead of adding the numerators you will want to subtract them.
- Create a public method called multiply. This method should return a new Fraction that is the product of the numerators and the denominators.
- Create a public method called divide. This method should return a new Fraction where the numerator is the product of the numerator from the first Fraction with the denominator from the second Fraction and the denominator is the product of the denominator from the first Fraction and the numerator from the second Fraction.
Testing Phase Three
From phase two, the second Fraction should be 3/4 and the second Fraction should be 5/6. Test your methods out by adding, subtracting, multiplying, and dividing these Fractions together. Your output should be:
19 / 12
-1 / 12
5 / 8
9 / 10
© 2016 CS160 Colorado State University. All Rights Reserved.