Here is the assignment. I need some help on it. I'm clueless.
In this assignment you will write a class called Fraction to represent positive and negative fractions, i.e. numbers like -5/4 or ½. Your class should be in a file called Fraction.java and needs to be declared as a public class. Create a file called TestFraction.ja va (in the same directory as Fraction.java) containing the public class TestFraction, and the main method. Use the main method in TestFraction to help test your Fraction methods and constructor. Write your own test code for each method in TestFraction as you complete each new method in Fraction, and run it with the command java TestFraction. Hand in only the file Fraction.java --- we will test it with our own test code.
Your Fraction class should represent a fraction by only two integers, as shown in the code here which you should copy into Fraction.java to get started. Similarly copy this into TestFraction.ja va and add your own tests to get started.
First write a constructor for the Fraction class. Its signature should be as follows.
public Fraction(int numerator, int denominator)
Store the fraction in its lowest terms by dividing out by the greatest common divisor (as provided in the sample code you saved from above). Ensure that the denominator is always stored as a positive number. If the constructor is given a negative denominator, then negate the denominator and the numerator before assigning them to instance variables. Don't worry unduly about the case where a denominator of zero is provided: just cause an error by executing the following statement.
throw new RuntimeError("a ttempt to create zero denominator Fraction");
Then write a toString() method with the following signature as discussed in class.
public String toString()
This should print out a fraction in the form e.g. -12/47 or 1/3
Finally write methods with signatures as follows to perform arithmetic.
public Fraction plus(Fraction f)
public Fraction minus(Fraction f)
public Fraction multiply(Fracti on f)
public Fraction divide(Fraction f)
These will be called as e.g. answer = f1.minus(f2) //compute f1 - f2
In this assignment you will write a class called Fraction to represent positive and negative fractions, i.e. numbers like -5/4 or ½. Your class should be in a file called Fraction.java and needs to be declared as a public class. Create a file called TestFraction.ja va (in the same directory as Fraction.java) containing the public class TestFraction, and the main method. Use the main method in TestFraction to help test your Fraction methods and constructor. Write your own test code for each method in TestFraction as you complete each new method in Fraction, and run it with the command java TestFraction. Hand in only the file Fraction.java --- we will test it with our own test code.
Your Fraction class should represent a fraction by only two integers, as shown in the code here which you should copy into Fraction.java to get started. Similarly copy this into TestFraction.ja va and add your own tests to get started.
First write a constructor for the Fraction class. Its signature should be as follows.
public Fraction(int numerator, int denominator)
Store the fraction in its lowest terms by dividing out by the greatest common divisor (as provided in the sample code you saved from above). Ensure that the denominator is always stored as a positive number. If the constructor is given a negative denominator, then negate the denominator and the numerator before assigning them to instance variables. Don't worry unduly about the case where a denominator of zero is provided: just cause an error by executing the following statement.
throw new RuntimeError("a ttempt to create zero denominator Fraction");
Then write a toString() method with the following signature as discussed in class.
public String toString()
This should print out a fraction in the form e.g. -12/47 or 1/3
Finally write methods with signatures as follows to perform arithmetic.
public Fraction plus(Fraction f)
public Fraction minus(Fraction f)
public Fraction multiply(Fracti on f)
public Fraction divide(Fraction f)
These will be called as e.g. answer = f1.minus(f2) //compute f1 - f2
Comment