Question: Will someone please help me with my code I can't get it to run. Thenks public class Fraction implements Comparable { private

Will someone please help me with my code  I can't get it to run. Thenks

 

 

public class Fraction implements Comparable
{
   private int numerator;
   private int denominator;
   
   public Fraction(int numer, int denom)
   {
       numerator = numer;
       denominator = denom;
   }
   
   public void add(Fraction other)
   {
       numerator = numerator * other.getDenominator() +
           other.getNumerator() * denominator;
       denominator *= other.getDenominator();
   }
   
   public void subtract(Fraction other)
   {
       numerator = numerator * other.getDenominator() -
           other.getNumerator() * denominator;
       denominator *= other.getDenominator();    
   }
   public void multipy(Fraction other)
   {
       numerator *= get.otherNumerator();
       denominator *= get.otherDenominator();
   }
   public int getNumerator()
   {
       return numerator;
   }
   public int getdenominator()
   {
       return denominator;
   }
   public void setNumerator(int x)
   {
       numerator = x;
   }
   public void setDenominator(int x)
   {
       denominator = x;
   }
   public String toString()
   {
       return numerator + "/" + denominator;
   }
   public int compareTo(Fraction other)
   {
       int diff = this.numerator * other.getDenominator() -
           other.getNumerator() * this.denominator;
           if(diff > 0)
           {
               return 1;
           }
           else if(diff < 0)
           {
               return -1;
           }
           else
           {
               return 0;
           }
   }
   public boolean equals(Object other)
   {
       return other instanceof Fraction && compareTo((Fraction)other) == 0;
   }
}

 

public class FractionTester extends ConsoleProgram
{
   public void run()
   {
       Fraction f1 = new Fraction(1,2);
       Fraction f2 = new Fraction(2, 4);
   
       System.out.println(f1.compareTo(f2));
       
       Fraction f3 = new Fraction(3, 4);
       System.out.println(f1.compareTo(f3));
       
       Fraction f4 = new Fraction(4, 5);
       System.out.println(f1.compareTo(f4));
       
       System.out.println(f1.equals(f2));
       System.out.println(f1.equals(f3));
   }
   
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

There are a few errors in your code that need to be addressed Heres a corrected version of your code java public class Fraction implements Comparable ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!