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

public class Fraction { private int numerator; private int denominator;

public Fraction() { numerator = 0; denominator=1; } public Fraction(int n, int d) { } public int getNumerator() { return numerator; }

public void setNumerator(int n) { }

public int getDenominator() { return denominator; }

public void setDenominator(int d) { } public Fraction add(Fraction g) { } public Fraction subtract(Fraction g) { } public Fraction multiply(Fraction g) { } public Fraction divide(Fraction g) { } public String toString() { return numerator + "/" + denominator; } private int gcd(int int1, int int2) { } }

import java.util.Scanner; public class Lab7a { 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_2

Step: 3

blur-text-image_3

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions