In 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. • 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.) • Separate accessor methods for each instance variable (numerator , denominator ) in the Fraction class Provide a driver class (FractionDemo) that demonstrates this Fraction class. The driver class should contain this main method : public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction c, d, x; // 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(); System.out.println("Enter numerator; then denominator."); x = new Fraction(stdIn.nextInt(), stdIn.nextInt()); x.printAsDouble(); } // end main Please study this driver carefully. Note that this driver does not call the accessor methods--that’s OK. Accessor methods are often implemented regardless of whether there is an immediate need; they are handy methods in general, and providing them means that future code can use them when needed. Sample session: 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
In 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.
• 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.)
• Separate accessor methods for each instance variable (numerator , denominator ) in the
Fraction class
Provide a driver class (FractionDemo) that demonstrates this Fraction class. The driver class should
contain this main method :
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Fraction c, d, x; // 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();
System.out.println("Enter numerator; then denominator.");
x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
x.printAsDouble();
} // end main
Please study this driver carefully. Note that this driver does not call the accessor methods--that’s OK.
Accessor methods are often implemented regardless of whether there is an immediate need; they are
handy methods in general, and providing them means that future code can use them when needed.
Sample session:
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
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)