PROGRAM 2 Fraction Class Problem For this programming assig

PROGRAM 2 – Fraction Class




Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are also to develop an appropriate test driver that thoroughly tests the class.


Fraction Class Design Implement the following class design. Make sure to use implement the methods to have the same names as given below, with the same number and types of parameters.


public class Fraction {   

private int numer;    

private int denom;         

// alternate constructor    

public Fraction(int numer, int denom)

throws                                              InvalidDenominatorException     {

}        

// copy constructor    

public Fraction(Fraction otherFrac)     {

}         

// getters    

public int getNumer()     {

}         

public int getDenom()     {

}         

// standard methods   

public String toString()     {

}         

public boolean equals(Fraction rFrac)     {

}         
// fraction operators   

public Fraction add(Fraction rFrac)     {

}         

public Fraction sub(Fraction rFrac)     {

}         

public Fraction mult(Fraction rFrac)     {

}         

public Fraction div(Fraction rFrac)     {

}         

public boolean isSimplestForm()     {

}         

public Fraction reduce()     {

}         

public boolean properFrac()     {

}        

public Fraction abs()     {

}         

public boolean lessThan(Fraction rFrac)     {

}         

public boolean greaterThan(Fraction rFrac)     {

}         

public double convertToDecimal()     {

}         

public Fraction invert()     {

}

}

Note that objects of type Fraction are immutable. Also, the alternate constructor throws an InvalidDenominatorException if passed a denominator value of 0. This exception type is not predefined in Java, so you must define it as follow as another class in your Java project files:

public class InvalidDenominatorException extends RuntimeException {   } // nothing to implement


This creates an exception of type RuntimeException. RuntimeExceptions do not need to be caught when thrown. However, your test driver should have the code for catching this exception where appropriate.

For example, if your test driver excutes the following,

Fraction frac1 = new Fraction(1,2);

then there is no need to catch the possible exception, since it is clear that the denominator is not 0. However, if the value are input by the user, then there is a chance that a denominator vaue of 0 could be entered, and therefore the code for catching the possible exception is needed,

int numer, denom;

Fraction frac1;

Scanner input = new Scanner(System.in);

Boolean valid_input = false;

while (!valid_input) try {

System.out.print(“Enter numerator denominator: “);  

numer = input.nextInt();  

denom = input.nextInt();

frac1 = new Fraction(numer,denom);

valid_input = true;

}

catch (InvalidDenominatorException e) {

System.out.println(“Denominator of Zero found – Please reenter”);

}

catch (InputMismatchException e) {  

System.out.println(“Non-digit character found – Please reenter”);

} etc.  

Considerations
Makes sure the equals method returns true for any two fractions that are arithmetically equal. Make sure that the equals, lessThan and greaterThan methods do not alter the values of the fractions being compared. You may consider the use of private methods that are called by the public methods of the class, but not accessible to the users (“client code”).

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.

public class Fraction {

   private int numer;

   private int denom;

   // alternate constructor

   public Fraction(int numer, int denom) throws InvalidDenominatorException{

       if(denom == 0)

           throw new InvalidDenominatorException();

       this.numer = numer;

       this.denom = denom;

   }

   // copy constructor

   public Fraction(Fraction otherFrac) {

       numer = otherFrac.numer;

       denom = otherFrac.denom;

   }

   // getters

   public int getNumer() {

       return numer;

   }

   public int getDenom() {

       return denom;

   }

   // standard methods

   public String toString() {      

       int n = numer;

       int d = denom;

       if(d < 0){

           n = -n; // converting n as negative

           d = -d; // converting d as positive

       }

       if(d == 1)

           return Integer.toString(n);

                  

       return n + \"/\" + d;

      

   }

   public boolean equals(Fraction rFrac) {

       return convertToDecimal() == rFrac.convertToDecimal();

   }

   // fraction operators

   public Fraction add(Fraction rFrac) {

       int a = this.numer;

       int b = this.denom;

       int c = rFrac.numer;

       int d = rFrac.denom;

       Fraction v = new Fraction(a * d + b * c, b * d);

       return v;

   }

   public Fraction sub(Fraction rFrac) {

       int a = this.numer;

       int b = this.denom;

       int c = rFrac.numer;

       int d = rFrac.denom;

       Fraction v = new Fraction(a * d - b * c, b * d);

       return v;

   }

   public Fraction mult(Fraction rFrac) {

       int a = this.numer;

       int b = this.denom;

       int c = rFrac.numer;

       int d = rFrac.denom;

       Fraction v = new Fraction(a * c, b * d);

       return v;

   }

   public Fraction div(Fraction rFrac) {

       int a = this.numer;

       int b = this.denom;

       int c = rFrac.numer;

       int d = rFrac.denom;

       Fraction v = new Fraction(a * d, b * c);

       return v;

   }

   public boolean isSimplestForm() {

       return gcd(numer, denom) == 1;

   }

   public Fraction reduce() {      

       int i = gcd(numer, denom);

       return new Fraction(numer/i, denom/i);

      

   }

   public boolean properFrac() {

       return gcd(numer, denom) == 1;

   }

   public Fraction abs() {

       int int1 = numer;

       int int2 = denom;

       // if any of two numbers are negative, make it positive

       if(int1 < 0)

           int1 = -int1;

       if(int2 < 0)

           int2 = -int2;

       return new Fraction(int1, int2);

   }

   public boolean lessThan(Fraction rFrac) {

       return convertToDecimal() < rFrac.convertToDecimal();

   }

   public boolean greaterThan(Fraction rFrac) {

       return convertToDecimal() > rFrac.convertToDecimal();

   }

   public double convertToDecimal() {

      

       return (double)numer/(double)denom;

      

   }

   public Fraction invert() {

       return new Fraction(denom, numer);

   }

  

   private int gcd(int int1, int int2) {

       int i = 0;

       int smallest=0;

       // if any of two numbers are negative, make it positive

       if(int1 < 0)

           int1 = -int1;

       if(int2 < 0)

           int2 = -int2;

      

       if (int2>0){

           if (int1 < int2) {

               smallest=int1;

           }

           else{

               smallest= int2;

           }

           for (i = smallest; i > 0; i--) {

               if ((int1 % i == 0) && (int2 % i == 0)) {

                   break;

               }

           }

       }

       return i;

   }

}

import java.util.InputMismatchException;

import java.util.Scanner;

public class FractionDrive {

   public static void main(String[] args) {

       int numer, denom;

       Fraction frac1;

       Scanner input = new Scanner(System.in);

       Boolean valid_input = false;

       while (!valid_input) try {

           System.out.print(\"Enter numerator denominator: \");

           numer = input.nextInt();

           denom = input.nextInt();

           frac1 = new Fraction(numer,denom);

           valid_input = true;

           System.out.println(frac1);

       }

       catch (InvalidDenominatorException e) {

           System.out.println(\"Denominator of Zero found – Please reenter\");

       }

       catch (InputMismatchException e) {

           System.out.println(\"Non-digit character found – Please reenter\");

       }

   }

}

/*

Sample run:

Enter numerator denominator: 1 0

Denominator of Zero found – Please reenter

Enter numerator denominator: 0 2

0/2

*/

PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are
PROGRAM 2 – Fraction Class Problem For this programming assignment, you are to implement the Fraction class design given below (and discussed in class). You are

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site