Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java - Fraction Class In this assignment, you will implement and create a Fraction class for mixed fraction numbers. Complete the following interface for the
Java - Fraction Class
In this assignment, you will implement and create a Fraction class for mixed fraction numbers.
Complete the following interface for the Fraction class and save it as Fraction.java:
public class Fraction { private int whole; private int numerator; private int denominator; // Create a paramaterless constructor, which sets // the whole number and numerator to 0 and the denominator to 1. public Fraction() { } // Create a constructor that accepts two parameters // for the numerator and denominator. When the constructor // is used, the whole number value is 0. public Fraction(int numerator, int denominator) { } // Create a constructor that accepts three parameters // for the whole number, numerator, and denominator. public Fraction(int whole, int numerator, int denominator) { } // This calculates greatest common divisor (GCD) // I expect that you will find it useful int gcd(int a, int b) { if( b == 0 ) return a; return gcd(b,a%b); } // Reduces the Fraction value to the lowest possible denominator // example 72/20 should be reduced to 18/5, which is also 3 3/5. public void reduce() { } // Add the current Fraction to Fraction A, return the result as a new Fraction (reduced, of course) public Fraction add(Fraction A) { } // Fraction C = this - A; // Subtract the current Fraction Fraction A, return the result as a new Fraction (reduced, of course) public Fraction substract(Fraction A) { } // Multiply the current Fraction to Fraction A, return the result as a new Fraction (reduced, of course) public Fraction multiply(Fraction A) { } // Divide Fractions (this/A), return the result as a new Fraction (reduced, of course) // You may assume that A will be a non-zero value public Fraction divide(Fraction A) { } // raturns a string that contains a fraction in the format: // whole number, a space, the numerator, a slash (/), and a denominator (e.g: 2 3/4). // When the whole number is 0, just display the fraction part (e.g: 1/2 instead of 0 1/2. // When the numerator is 0, just display the whole number (e.g: 2 instead of 2 0/3). // Also, just display 3 instead of 3/1. public string toString() { } }
Notes
Make sure you understand the problem before you start coding (start on paper).
Test your program on mutiple test cases.
You only need to submit Fraction.java.
The following main program is a trivial example of how your class will be tested.
import java.util.*; public class Sample { public static void main(String[] args) { int w, n, d; Scanner sc = new Scanner(System.in); System.out.print("Enter whole number for X: "); w = sc.nextInt(); System.out.print("Enter numerator for X: "); n = sc.nextInt(); System.out.print("Enter denominator for X: "); d = sc.nextInt(); Fraction X = new Fraction(w, n, d); System.out.print("Enter whole number for Y: "); w = sc.nextInt(); System.out.print("Enter numerator for Y: "); n = sc.nextInt(); System.out.print("Enter denominator for Y: "); d = sc.nextInt(); Fraction Y = new Fraction(w, n, d); System.out.println(); System.out.println("X: " + X.toString()); System.out.println("Y: " + Y.toString()); Fraction A = X.add(Y); System.out.println("ADD: " + A.toString()); Fraction S = X.substract(Y); System.out.println("SUB: " + S.toString()); Fraction M = X.multiply(Y); System.out.println("MUL: " + M.toString()); Fraction D = X.divide(Y); System.out.println("DIV: " + D.toString()); } }
Here are two sample runs of the program:
Enter whole number for x: 4 Enter numerator for X: 1 Enter denominator for X 2 Enter whole number for Y 0 Enter numerator for Y 1 Enter denominator for Y 2 x: 4 1/2 Y: 1/2 ADD 5 SUB 4 MUL: 2 1/4 DIV: 9 Enter whole number for X: 3 Enter numerator for X: 2 Enter denominator for X Enter whole number for Y: 0 Enter nurmerator for Y: 3 Enter denominator for Y 1 X: 3 2/3 Y: 3 ADD: 6 2/3 SUB: 2/3 MUL: 11 DIV: 1 2/9Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started