Question
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators. NOTE: You need only consider POSITIVE
Alter your Fraction class so that the Fractions are reduced whenever necessary - i.e. in the constructor and mutators.
NOTE: You need only consider POSITIVE values for the numerator and denominator
import java.util.Scanner; public class Annoymous{ public static class Fraction { private int numerator; private int denominator; public Fraction() { numerator = 0; denominator=1; } public Fraction(int n, int d) { int g=gcd(n,d); //complete this } public int getNumerator() { return numerator; } public void setNumerator(int n) { int d=denominator; int g=gcd(n,d); //complete this } public int getDenominator() { return denominator; } public void setDenominator(int d) { int n=numerator; int g=gcd(n,d); //complete this } public Fraction add(Fraction g) { int a=this.numerator; int b=this.denominator; int c=g.numerator; int d=g.denominator; Fraction v=new Fraction(a*d+b*c,b*d); return v; } public Fraction subtract(Fraction g) { int a=this.numerator; int b=this.denominator; int c=g.numerator; int d=g.denominator; Fraction v=new Fraction(a*d-b*c,b*d); return v; } public Fraction multiply(Fraction g) { int a=this.numerator; int b=this.denominator; int c=g.numerator; int d=g.denominator; Fraction v=new Fraction(a*c,b*d); return v; } public Fraction divide(Fraction g) { int a=this.numerator; int b=this.denominator; int c=g.numerator; int d=g.denominator; Fraction v=new Fraction(a*d,b*c); return v; } public String toString() { return numerator + "/" + denominator; } private int gcd(int int1, int int2) { } } public static void main(String[] args) { //test constructor Fraction g= new Fraction(4,8); System.out.println(g); Fraction f= new Fraction(); f.setNumerator(60); f.setDenominator(90); System.out.print("60/90 is "); System.out.println(f); Scanner keyboard = new Scanner (System.in); //get the data for the next fraction and construct it int n1=keyboard.nextInt(); int d1=keyboard.nextInt(); Fraction one=new Fraction (n1,d1); //get the data for the next fraction and construct it n1=keyboard.nextInt(); d1=keyboard.nextInt(); Fraction two=new Fraction (n1,d1); //test methods for add, subtract, multiply, divide System.out.println(one + " + " + two + " = " + one.add(two)); System.out.println(one + " - " + two + " = " + one.subtract(two)); System.out.println(one + " * " + two + " = " + one.multiply(two)); System.out.println(one + " divided by " + two + " = " + one.divide(two)); } }
Step 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