Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance

Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class - the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when its declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations: a) Add two Rational numbers: The result of the addition should be stored in reduced form. b) Subtract two Rational numbers: The result of subtraction should be stored in reduced form. c) Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. d) Divide two Rational numbers: The result of the division should be stored in reduced form. e) Return a String representation of Rational number in the form ??/??, where ?? is the numerator and ?? is the denominator.

This is the instructions but I do not know how to reduce the fractions for steps a - d

This is my code:

import java.util.Scanner; public class Rational { private int numerator; private int denominator; public Rational() { numerator = 1; denominator = 2; } public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public int getNumerator() { return numerator; } public void setNumerator(int numerator) { this.numerator = numerator; } public int getDenominator() { return denominator; } public void setDenominator(int denominator) { this.denominator = denominator; } public Rational add(Rational r) { int num = numerator * r.denominator + r.numerator * denominator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational subtract(Rational r) { int num = numerator * r.denominator - r.numerator * denominator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational multiply(Rational r) { int num = numerator * r.numerator; int denom = denominator * r.denominator; return new Rational(num, denom); } public Rational divide(Rational r) { int num = numerator / r.numerator; int denom = denominator / r.denominator; return new Rational(num, denom); } public String toString() { return "(" + numerator + "/" + denominator + ")"; } } class RationalTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); Rational a; Rational b; System.out.println("Enter numerator for the first rational number: "); int numa = input.nextInt(); System.out.println("Enter a non-zero denominator for the first rational number: "); int denoma = input.nextInt(); System.out.println("Enter a numerator for the second rational number: "); int numb = input.nextInt(); System.out.println("Enter a non-zero denominator for the second rational number: "); int denomb = input.nextInt(); a = new Rational(numa, denoma); b = new Rational(numb, denomb); System.out.println("First rational number is: " + a); System.out.println("Second rational number is: " + b); System.out.println("Addition of the rational number is: " + a.add(b)); System.out.println("Subtraction of the rational number is: " + a.subtract(b)); System.out.println("Multiplication of the rational number is: " + a.multiply(b)); System.out.println("Division of the rational number is: " + a.divide(b)); } }

And this is my output:

Enter numerator for the first rational number: 4 Enter a non-zero denominator for the first rational number: 6 Enter a numerator for the second rational number: 1 Enter a non-zero denominator for the second rational number: 4 First rational number is: (4/6) Second rational number is: (1/4) Addition of the rational number is: (22/24) Subtraction of the rational number is: (10/24) Multiplication of the rational number is: (4/24) Division of the rational number is: (4/1)

Process finished with exit code 0

I need the fractions to be in their lowest form and I do not know how to implement that into my code.

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

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago

Question

Are my points each supported by at least two subpoints?

Answered: 1 week ago