Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.io.*; class Rational { private int nu,dr; Rational() { int nu = 1; int dr = 2; reduce(); } Rational(int nu, int dr){ this.nu

import java.io.*; class Rational {

private int nu,dr; Rational() { int nu = 1; int dr = 2; reduce(); }

Rational(int nu, int dr){ this.nu = nu; this.dr = dr; reduce(); }

void reduce() { int div=Rational.gcd(nu,dr); nu=nu/div; dr=dr/div; }

static int gcd(int a, int b) { int c; while (b != 0) { c = a % b; a = b; b = c; } return b;

} public void display() { System.out.println(nu+"/"+dr); double res=nu/dr; System.out.println(res); }

public void add(Rational x) { //int lcm = dr * x.dr; dr = dr * x.dr; nu=nu*x.dr+x.nu*dr; reduce(); display(); }

public void sub(Rational x) { int lcm = dr * x.dr; dr = dr * x.dr; nu=nu*(lcm/dr)-x.nu*(lcm/x.dr); reduce(); display(); }

public void mul(Rational x) { nu = nu * x.nu; dr = dr * x.dr; reduce();

display(); }

public void div(Rational x) { nu = nu * x.dr; dr = dr * x.nu; reduce();

display(); } } ---------------------------------------------------------------------------

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

class Demo { public static void main(String args[]) throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter numerator 1:"); int n1=Integer.parseInt(br.readLine()); System.out.println("Enter denominator 1:"); int d1=Integer.parseInt(br.readLine());

System.out.println("Enter numerator 2:"); int n2=Integer.parseInt(br.readLine()); System.out.println("Enter denominator 2:"); int d2=Integer.parseInt(br.readLine());

Rational obj1=new Rational(n1,d1); Rational obj2=new Rational(n2,d2);

while(true) { System.out.println(" 1. Add 2. Subtract 3. Multiply 4. Divide 5. Exit"); System.out.println("Enter Choice: "); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: obj1.add(obj2); break;

case 2: obj1.sub(obj2); break;

case 3: obj1.mul(obj2); break;

case 4: obj1.div(obj2); break; case 5: System.exit(0);

default: System.exit(0); } } } }

--------------------------------------------------

Output:-

Enter numerator 1:1

Enter denominator 1:3

Enter numerator 2:7

Enter denominator 2:8

1. Add

2. Subtract

3. Multiply

4. Divide

5. Exit

Enter Choice:

1

29/24

1.208333

1. Add

2. Subtract

3. Multiply

4. Divide

5. Exit

Enter Choice:

2

-13/24

-0.541666

1. Add

2. Subtract

3. Multiply

4. Divide

5. Exit

Enter Choice:

3

7/24

0.291666

1. Add

2. Subtract

3. Multiply

4. Divide

5. Exit

Enter Choice:

4

8/21

0.38095

1. Add

2. Subtract

3. Multiply

4. Divide

5. Exit

Enter Choice:

5

----------------------------------------------------------------------------------------

Everytime I try to input the same output, I get these errors.

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at hw1.bs.main(bs.java:62)

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

Describe the Indian constitution and political system.

Answered: 1 week ago

Question

Explain in detail the developing and developed economy of India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = X 52+7 - 1)(x+2) dx

Answered: 1 week ago

Question

What is gravity?

Answered: 1 week ago

Question

What is the Big Bang Theory?

Answered: 1 week ago