Question
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
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