Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analytics And Quality Management Fundamental Tools

Authors: Joseph Nguyen

1st Edition

B0CNGG3Y2W, 979-8862833232

More Books

Students also viewed these Databases questions

Question

6. What is adverse impact? How can it be proven?

Answered: 1 week ago

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago