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

Java programming Create a class called Fraction that works with the client provi

ID: 3573690 • Letter: J

Question

Java programming

Create a class called Fraction that works with the client provided below. Do not modify the client program. Your class must work with the client as is.

the class should have two instance variables (numerator and denominator)

the class should have accessor/mutator methods (but do not allow the denominator to be equal to zero)

the class should have a toString method that returns a string representation (for example, "2/3")

the class should have an equals method that compares the numerator/denominator of each fraction

the class should match the documentation provided below

public class FractionClient
{
public static void main( String [] args )
{
    Fraction r1 = new Fraction( 2, 3 );
    Fraction r2 = new Fraction( 5, 7 );

    System.out.println( "The numerator of fraction #1 is " + r1.getNumerator( ) );
    System.out.println( "The denominator of fraction #1 is " + r1.getDenominator( ) );

    System.out.println( " Fraction #2 is " + r2.toString( ) + " " );

    if ( r1.equals( r2 ) )
      System.out.println( "Original fraction #1 and #2 are identical" );
    else
      System.out.println( "Original fraction #1 and #2 are different" );

    r2.setNumerator( 2 );
    System.out.println( " Setting denominator of fraction #2 to 0 " );
    r2.setDenominator( 0 );
    System.out.println( " Setting denominator of fraction #2 to 3 " );
    r2.setDenominator( 3 );

    if ( r1.equals( r2 ) )
      System.out.println( "Original fraction #1 and modified fraction #2 are identical" );
    else
      System.out.println( "Original fraction #1 and modified fraction #2 are different" );

}
}

Explanation / Answer

Fraction.java

public class Fraction {

   int numerator, denominator;

   public Fraction(int numerator, int denominator) {
       super();
       this.numerator = numerator;
       if (denominator == 0) {
           throw new ArithmeticException();
       } else
           this.denominator = denominator;
   }

   public int getNumerator() {
       return numerator;
   }

   public void setNumerator(int numerator) {
       this.numerator = numerator;
   }

   public int getDenominator() {
       return denominator;
   }

   public void setDenominator(int denominator) {
       if (denominator == 0) {
           throw new ArithmeticException();
       } else
           this.denominator = denominator;
   }

   @Override
   public String toString() {
       return numerator + "/" + denominator;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Fraction other = (Fraction) obj;
       if (this.getNumerator() / this.getDenominator() == other.getNumerator()
               / other.getDenominator()) {
           return true;
       } else
           return 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