Question

Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...

Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to a string.

Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improper—that is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms.

Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method.

To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers:

Algorithm gcd(integerOne, integerTwo)

if (integerOne % integerTwo == 0)

result = integerTwo

else

result = gcd(integerTwo, integerOne % integerTwo) return result

It will be easier to determine the correct sign of a fraction if you force the fraction’s denominator to be positive. However, your implementation must handle negative denominators that the client might provide.

Write a program that adequately demonstrates your class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Fraction

Responsibilities

Set the fraction

Add two fractions

Subtract two fractions

Multiply two fractions

Divide two fractions

Reciprocal of a fraction

Compare two fractions

Convert fraction to string

Minimize the fraction

Display the fraction

Collaborations

/* Code */

class Fraction
{
   /*Instance variables n=numerator and d=Denominator*/
   int n;
   int d;
  
   /* Parameterless constructor */
   Fraction()
   {
       n=0;
       d=1;
   }
   /* Parameterized constructor */
   Fraction(int n, int d)
   {
       try{
       if(d==0) throw new ArithmeticException(); // checking denominator is zero or not. If zero then throw ArithmeticException

/* checking denominator is negative or not. If denominator is positive then make sure it to be positive*/

if(d<0) {
           d=-d;
           n=-n;
       }
  
       /* Minimize the fraction */
       int g=gcd(n,d);
       this.n=n/g;
       this.d=d/g;
       }
       catch(ArithmeticException e)
       {
           System.out.println("Denominator is zoro");
       }
   }
  
   /* Method to display the fraction */  
   public void show()
   {
       System.out.println("Fraction is:"+n+"/"+d);
   }
  
   /* Method to add two fractions */  
   public Fraction add(Fraction f)
   {
       Fraction temp = new Fraction();
       temp.n = this.n * f.d + f.n * this.d;
       temp.d = this.d * f.d;
       temp.minimize();
       return temp;
   }
  
   /* Method to subtract two fractions */  
   public Fraction subtract(Fraction f)
   {
       Fraction temp = new Fraction();
       temp.n = this.n * f.d - f.n * this.d;
       temp.d = this.d * f.d;
       temp.minimize();
       return temp;
   }
  
   /* Method to multiplication two fractions */  
   public Fraction mult(Fraction f)
   {
       Fraction temp = new Fraction();
       temp.n = f.n * this.n;
       temp.d = f.d * this.d;
       temp.minimize();
       return temp;
   }
  
   /* Method to division two fractions */  
   public Fraction div(Fraction f)
   {
       Fraction temp = new Fraction();
       temp.n = f.d * this.n;
       temp.d = f.n * this.d;
       temp.minimize();
       return temp;
   }
  
   /* Method to find reciprocal of a fraction */  
   public Fraction reciprocal()
   {
       Fraction temp = new Fraction();
       temp.n = this.d;
       temp.d = this.n;

       return temp;
   }
  
   /* Method to compare two fractions */  
   public boolean compareTo(Fraction f)
   {
       if(this.n == f.n && this.d==f.d) return true;
       else return false;
   }
  
   /* Method to minimize a fraction */  
   public void minimize()
   {
       int g=gcd(this.n,this.d);
       this.n /= g;
       this.d /= g;
   }
  
   /* Method to find our gcd of two numbers */  
   static int gcd(int a, int b)
   {
       if(a==0) return b;
       return gcd(b%a,a);
   }
  
   /* Method to convert from fraction to string */  
   public String toString()
   {
       return "Fraction is:"+n+"/"+d;
   }
  
}

class Demo{
   public static void main (String[] args) {
      
       /* create two fraction objects */
       Fraction f1 = new Fraction(2,4);
Fraction f2 = new Fraction(1,2);
      
       /* display the fraction objects */
       f1.show();
       f2.show();
      
      
       System.out.println("Result of addition");
       Fraction f3 = f1.add(f2);
       f3.show();
      
       System.out.println("Result of subtraction");
       Fraction f4 = f1.subtract(f2);
       f4.show();
      
       System.out.println("Result of multiplication");
       Fraction f5 = f1.mult(f2);
       f5.show();
      
       System.out.println("Result of division");
       Fraction f6 = f1.div(f2);
       f6.show();
      
       System.out.println("Result of reciprocal");
      Fraction f7 = f1.reciprocal();
       f7.show();
      
       System.out.println("Result of fraction comparison");
   boolean b = f1.compareTo(f2);
   System.out.println(b);
  
   System.out.println("Convert to string");
      
       System.out.println("F1="+f1.toString());
  
}
}


/* Snapshot of output screen */

Add a comment
Know the answer?
Add Answer to:
Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • Design a class named Fraction. This class is used to represent a ratio of two integers,...

    Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • Must write in Java - ignore the Junit tests Write a program that works with fractions....

    Must write in Java - ignore the Junit tests Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-element array...

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

    This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Consider the following code for a Fraction class: self. numerator = 0 self tsn return self. denominator # Greatest Common Divisor def GCD (self, m, n): while n != 0: m=t return m def reduce(self...

    Consider the following code for a Fraction class: self. numerator = 0 self tsn return self. denominator # Greatest Common Divisor def GCD (self, m, n): while n != 0: m=t return m def reduce(self): gcd - self.GCD(self._numerator, self._denominator) self. numerator int(self,_numerator / gcd) self'-denominator înt (self-denominator, gcd) def_str (self): 프 str(self-numerator). "ItDenominator, return "Numerator : str(self,-deno 프, . minator) Write a Unittest framework class to test the GCD method presented in the Fraction class Upload your answer code to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT