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

Hello, please completely read the entire instructions and try to follow its guid

ID: 3853167 • Letter: H

Question

Hello, please completely read the entire instructions and try to follow its guidelines. This is from a beginner java programmer class so please nothing to extravegant. I have learned for/if/while loops along with arrays and parameters. A lot of the for and if statments involve using .length so please try to incorporate that if possible. I know I am adding a lot of instructions but please feel free to use other tools if they are no other ways. Just not to far advanced that I should not know it. Thank you so much for your help! Below are the instruction and in there it will tell you everyhitng on what should be in the code:

Fraction class Write a class encapsulating the concept of a fraction, assuming a fraction has the following attributes: Instance variables An integer representing the numerator An integer representing the denominator ' ' Methods accessor methods mutator methods · toString method * equals method method returning the double value of numerator divided by denominator Method returning a new Fraction object that is the fraction reduced to its lowest terms. For example if the Fraction is 20'60 the method should return the Fraction 1/3 Helper method that returns the greatest common divisor for the denominator and the numerator Write a client class to test all the methods in your class (See sample output from the test program on the next page.) UML diagram for the Fraction class: Fraction -nurnerator: int denominator: int + setNumerator( newNumerator int) + setDenominator( newDenominator: int) boolean + getNumerator(): int + getDenominator): int + getDoubleFraction: double getReducedFraction: Fraction equals( otherFraction: Fraction ): boolean + toString(): String ged(): int The setNumerator method sets the numerator to newNumerator The setDenominator method will return a boolean variable called success that will indicate whether or not this method was successful. The method should begin by setting success to truc. It should then validate the newDenominator and set the denominator to newDenominator if newDenominator-0. otherwise it will not change the valuc of the denominator and it will set success to false " The getNumerator method returns the numerator " The getDenominator method returns the denominator. * The getDoubleFraction method returns the fraction(numerator/denominator) value as a double " The getReducedFraction method returns reduced Fraction Object

Explanation / Answer

Fraction.java

public class Fraction{
   private int numerator;
   private int denominator;
   public static void main(String a[]){
       Fraction f = new Fraction();
       System.out.println("TESTING MUTATOR and toString METHODS ");
       f.setNumerator(12);
       System.out.println("After using setNumerator(12);");
       System.out.println("The toString method returned: "+f+" ");
      
       System.out.println("After using setDenominator(8);");
       System.out.println("The setDenominator method returned "+f.setDenominator(8)+", and the toString method returned: "+f);
      
      
       System.out.println(" After using setDenominator(0);");
       System.out.println("The setDenominator method returned "+f.setDenominator(0)+", and the toString method returned: "+f);
      
       System.out.println(" TESTING ACCESSOR METHODS:");
       System.out.println("numerator = "+f.getNumerator());
       System.out.println("denominator = "+f.getDenominator());
       System.out.println("fraction = "+f.getDoubleFraction());
       System.out.println("reduced fraction = "+f.getReducedFraction());
      
       System.out.println(" TESTING EQUALS METHOD:");
       Fraction f1 = new Fraction();
       f1.setNumerator(15);
       f1.setDenominator(5);
       System.out.println("f1 = "+f1);
       Fraction f2 = new Fraction();
       f2.setNumerator(6);
       f2.setDenominator(7);
       System.out.println("f2 = "+f2);
       Fraction f3 = new Fraction();
       f3.setNumerator(3);
       f3.setDenominator(1);
       System.out.println("f3 = "+f3);
       Fraction f4 = new Fraction();
       f4.setNumerator(6);
       f4.setDenominator(7);
       System.out.println("f4 = "+f4);
      
       if(f1.equals(f2))
       System.out.println(" f1 is equal to f2");
       else System.out.println(" f1 is NOT equal to f2");
      
       if(f1.equals(f3))
       System.out.println("f1 is equal to f3");
       else System.out.println("f1 is NOT equal to f3");
      
       if(f2.equals(f4))
       System.out.println("f2 is equal to f4");
       else System.out.println("f2 is NOT equal to f4");
      
       if(f3.equals(f4))
       System.out.println("f3 is equal to f4");
       else System.out.println("f3 is NOT equal to f4");
      
   }
   public void setNumerator(int newNumerator){
       this.numerator = newNumerator;
   }
   public boolean setDenominator(int newDenominator){
       boolean success = true;
       if(newDenominator!=0)
       this.denominator = newDenominator;
       else success = false;
       return success;
   }
   public int getNumerator(){
       return numerator;
   }
   public int getDenominator(){
       return denominator;
   }
   public double getDoubleFraction(){
       return (double)numerator/denominator;
   }
   public Fraction getReducedFraction(){
       int min = numerator>denominator?denominator:numerator;
       int max = numerator+denominator-min;
       int gcd = gcd(min,max);
       Fraction f = new Fraction();
       f.setNumerator(numerator/gcd);
       f.setDenominator(denominator/gcd);
       return f;
   }
   public boolean equals(Fraction otherFraction){
       Fraction f1 = getReducedFraction();
       Fraction f2 = otherFraction.getReducedFraction();
       if(f1.getNumerator()==f2.getNumerator() && f1.getDenominator()==f2.getDenominator())
       return true;
       else return false;
   }
   public String toString(){
       return numerator+"/"+denominator;
   }
   private int gcd(int min,int max){
       int div = max/min;
       int rem = max%min;
       if(rem==0) return min;
       else return gcd(rem,min);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote