CSC224 Java For Programmers
Homework 4
due: See COL
Modify the given Fraction class
as follwing :
- add one more constructor which takes string as parameter:
it should be able to handle the following cases :
- integer in string format : e.g Fraction ("213") ;
- fraction in string format : e.g. Fraction("12/45") ;
you may use String.split() method to extract the numerator and denominator from the string
- adding following class (static) methods
- Fraction plus (Fraction f1, Fraction f2) // f1 + f2
- Fraction minus (Fraction f1, Fraction f2) // f1 - f2
- Fraction times (Fraction f1, Fraction f2) // f1 * f2
- Fraction over (Fraction f1, Fraction f2) // f1 / f2
- Fraction negate (Fraction f1) // -f1
- Fraction invert (Fraction f1) // 1/f1
- Fraction power (Fraction f,int n) // f n where n could be negative
- boolean less (Fraction f1, Fraction f2) // return true if f < f2 , false elsewise
- boolean greater (Fraction f1, Fraction f2) // return true if f > f2 , false elsewise
- boolean same (Frction f1, Fraction f2) // return true if f == f2 , false elsewise
- boolean proper (Fraction f1) // return true if f1 is proper ; 1/2 is proper while 3/2 is not
- adding the following instance method(s)
- String properString() // returns the string format of the fraction number in proper format :
e.g 3/2 ==> 1 1/2
- Modify the following :
- every fraction number created must be in its simplest form,
i.e. there is no common factor
between numerator and denominator.( e.g. 1/2 instead of 2/4.)
- if the fraction is negative, keep denominator positive and numerator negative.
- improve toString() so that it produces a better looking string format of fraction:
- if denominator is 1 , then display the fraction as integer, in other words,
2/1 should be displayed as 2.
- make sure that string likes 1/-2 never happen ( it should produce -1/2 instead.)
Implement your own driver to test your Fraction class.
Warning :
You have to named the above methods exactly the same way they are listed as above.
(I'll use my driver to test your Faction class which expects you to name those methods
exactly as I listed above.)
A sample test driver class can be download from :
test.class
and Fraction class from :
Fraction.class .
To submit :
Only the Fraction.java file. through COL