Question: Java Write a Fraction class that implements these methods: add This method receives a Fraction parameter and adds the parameter fraction to the calling object

Java

Write a Fraction class that implements these methods:

  • add This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.

  • multiply This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.

  • divide This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction.

  • print This method prints the fraction using fraction notation (1/4, 21/14, etc.)

  • printAsDouble This method prints the fraction as a double (0.25, 1.5, etc.)

    In addition, complete the "divide" test in the provided FractionDriver.java. Sample session (your test should demonstrate the divide method as well):

    Enter numerator; then denominator. 5 8 5/8

    Enter numerator; then denominator. 4 10 4/10

    Sum: 82/80 1.025 Product: 20/80 0.25 Enter numerator; then denominator. 6

    0

infinity

--------------------

Code Provided:

-------------------

Fraction Class public class Fraction {

}

--------------------

FractionDriver Class

import java.util.Scanner;

public class FractionDriver { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction c, d, x, y; // Fraction objects

System.out.println("Enter numerator; then denominator."); c = new Fraction(stdIn.nextInt(), stdIn.nextInt()); c.print();

System.out.println("Enter numerator; then denominator."); d = new Fraction(stdIn.nextInt(), stdIn.nextInt()); d.print();

x = new Fraction(); // create a fraction for number 0

System.out.println("Sum:"); x.add(c).add(d); x.print(); x.printAsDouble();

x = new Fraction(1, 1); // create a fraction for number 1

System.out.println("Product:"); x.multiply(c).multiply(d); x.print(); x.printAsDouble();

// TODO: write your divide method test here ...

System.out.println("Enter numerator; then denominator."); x = new Fraction(stdIn.nextInt(), stdIn.nextInt()); x.printAsDouble(); } // end main } // end FractionDriver class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!