Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In JAVA . Write a class for a Fraction. This class will hold the numerator and d

ID: 3861842 • Letter: I

Question

In JAVA . Write a class for a Fraction. This class will hold the numerator and denominator of a fraction. Each time you write a method, test that method in the Driver Class (So this program need to have two classes, the Fraction class and the driver class).. Add two fractions returning the sum in lowest terms, susbtract the two fractions returning the difference in lowest terms. multiply the two fractions and divide the two fractions , and finally compare the two fractions and determine and return the decimal value of the fraction. Include a static method to generate and return a random fraction that is between 0 and 1. A toString() method that returns a string in form of a/b    .Here is a simple way to find the greatest common divisor of two numbers: start with the smaller of the two numbers and determine if the number goes into the larger of the two numbers evenly, if it does, this is the greatest commonn divisor, if not, repeately substract 1 from the value of the smallest number until you find a value that goes into both number evenly.                                                                                                                           

Explanation / Answer

/////////////////////================= Fraction.java =================//////////////////////////////
public class Fraction {
   private int numerator;
   private int denominator;
  
   public Fraction(int n,int d){
       setNumerator(n);
       setDenominator(d);
   }
  
   public int getNumerator() {
       return numerator;
   }
   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }
   public int getDenominator() {
       return denominator;
   }
   public void setDenominator(int denominator) {
       this.denominator = denominator;
   }
   public double getValue() {
       return (((double)numerator)/((double)denominator));
   }
  
   public Fraction add(Fraction f2){
       return new Fraction((numerator * f2.denominator) + (f2.numerator * denominator), (denominator * f2.denominator));
   }
  
   public Fraction sub(Fraction f2){
       return new Fraction((numerator * f2.denominator) - (f2.numerator * denominator), (denominator * f2.denominator));
   }
  
   public Fraction mul(Fraction f2){
       return new Fraction((numerator * f2.numerator) , (denominator * f2.denominator));
   }
  
   public Fraction div(Fraction f2){
       return new Fraction((numerator * f2.denominator) , (denominator * f2.numerator));
   }
   public boolean equals(Fraction frac) {
        return this.compareTo(frac) == 0;
    }

    public int compareTo(Fraction frac) {
        long t = this.getNumerator() * frac.getDenominator();
        long f = frac.getNumerator() * this.getDenominator();
        int result = 0;
        if(t>f) {
            result = 1;
        }
        else if(f>t) {
            result = -1;
        }
        return result;
    }
    public String toString(){
       return ""+numerator+"/"+denominator+"";
    }
}

/////////////////////================= FractionDriver.java =================//////////////////////////////


public class FractionDriver {

   public static void main(String[] args) {
       Fraction f1 = new Fraction(1, 2);
       Fraction f2 = new Fraction(4, 2);
      
       System.out.println("Add "+f1+" and "+f2+" = "+f1.add(f2)+" , value :"+f1.add(f2).getValue());
       System.out.println("Sub "+f1+" and "+f2+" = "+f1.sub(f2)+" , value :"+f1.sub(f2).getValue());
       System.out.println("Mul "+f1+" and "+f2+" = "+f1.mul(f2)+" , value :"+f1.mul(f2).getValue());
       System.out.println("Div "+f1+" and "+f2+" = "+f1.div(f2)+" , value :"+f1.div(f2).getValue());
       System.out.println("is Equal "+f1+" and "+f2+" = "+f1.equals(f2));
   }

}

/////////////////////================= OUTPUT =================//////////////////////////////

Add 1/2 and 4/2 = 10/4 , value :2.5
Sub 1/2 and 4/2 = -6/4 , value :-1.5
Mul 1/2 and 4/2 = 4/4 , value :1.0
Div 1/2 and 4/2 = 2/8 , value :0.25
is Equal 1/2 and 4/2 = false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote