Write a class used to represent fractions. It should contains fields for numerat
ID: 3818486 • Letter: W
Question
Write a class used to represent fractions. It should contains fields for numerator and denominator, and it should be able to perform some simple arithmetic operations.
The fields are:
numerator
denominator
Implement at least the following methods:
public Fraction(int n, int d)
Constructor that creates a Fraction with numerator and denominator d. If d is 0, throw an ArithmeticException. This can be done with the statement throw new ArithmeticException();
public int getNum()
Returns the value of the numerator field
public int getDenom()
Returns the value of the denominator field
public void setNum(int n)
Sets the numerator field to the value given in n
public void setDenum(int d)
Sets the denominator field to the value given in d. If d is 0, throw an ArithmeticException.
public Fraction add(Fraction a)
Returns the fraction that is the sum of the subject of the method and a. For example (new Fraction(3,4)).add(new Fraction(1,4)) is 16/16 i.e 1/1 We sum the fractions a/b and c/d as (a*d+b*c)/b*d then reduce.
public boolean equals(Fraction a)
Returns true if subject of method and argument of call are equal. Fractions a/b and c/d are equal if a*d and b*c are equal or since the fractions are normalized, if a==c and b==d.
public String toString()
Returns a String representation of the fraction. For example, if the numerator is 1 and the denominator is 2, the String "1/2" is returned.
Fractions should be stored in reduced form. For example, 2/4, 3/6, 4/8, etc. should all be stored as 1/2. 4/10 should be stored as 2/5. Use the Euclidean Algorithm for determining the greatest common divisor, so that you can store fractions in reduced form.
Write a simple main driver method to test your fraction class and each of its methods.
Explanation / Answer
class Fraction
{
private int numerator;
private int denominator;
public Fraction() //default constructor
{
numerator = 0;
denominator = 1;
}
public Fraction (int n, int d)//argument constructor
{
numerator = n;
if(d == 0)
throw new ArithmeticException();
else
denominator = d;
}
//get methods
public int getNum()
{
return numerator;
}
public int getDenom()
{
return denominator;
}
//set methods
public void setNum(int n)
{
this.numerator = n;
}
public void setDenum(int d)
{
if(d == 0)
throw new ArithmeticException();
else
this.denominator = d;
}
public Fraction add(Fraction a) //add fractions
{
this.numerator = this.numerator *a.denominator + a.numerator*this.denominator;
this.denominator = this.denominator * a.denominator;
return this;
}
public boolean equals(Fraction a) //compare two fractions
{
if(numerator == a.numerator && denominator == a.denominator)
return true;
else
return false;
}
public String toString () { return numerator + "/" + denominator; }
public void reduce() {
int factor;
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}
private int gcd (int x, int y) {
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
}
class TestFraction
{
public static void main (String[] args)
{
Fraction f1 = new Fraction(3,4);
Fraction f2 = new Fraction(1,4);
Fraction sum = new Fraction();
sum = f1.add(f2);
sum.reduce();
System.out.println("Sum of fractions = "+sum);
if(f1.equals(f2))
System.out.println("Fraction f1 is equal to fraction f2");
else
System.out.println("Fraction f1 is not equal to fraction f2");
}
}
Output:
Sum of fractions = 1/1
Fraction f1 is not equal to fraction f2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.