Question

Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program.

HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a

This is the sample output provided by my professor.

Java_Rational_output ар ald ex ter run: Enter numerator 1: 1 Enter denominator 1: 3 Enter numerator 2: 7 Enter denominator

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any query/ find any error kindly tell us, we here to help u. Thank you

Code:

import java.util.*;
class Solution{
public static void main (String[] args){
Scanner scan=new Scanner(System.in);
int num1,num2,den1,den2,prec;
System.out.print("Enter numerator 1: ");
num1=scan.nextInt();
System.out.print("Enter denominator 1: ");
den1=scan.nextInt();
System.out.print("Enter numerator 2: ");
num2=scan.nextInt();
System.out.print("Enter denominator 2: ");
den2=scan.nextInt();
System.out.print("Enter precision: ");
prec=scan.nextInt();
Rational r1=new Rational(num1,den1);
Rational r2=new Rational(num2,den2);
Rational r3;
int choice;
while(true){
System.out.println("1. Add\n2. Subtract\n3. Multiply");
System.out.print("4. Division\n5. Exit\nChoice: ");
choice=scan.nextInt();
switch(choice){
case 1: r3=r1.addition(r2);
System.out.print("a + b = "+r3.toString());
System.out.println(" = "+r3.toString(prec));
break;
case 2: r3=r1.subtract(r2);
System.out.print("a - b = "+r3.toString());
System.out.println(" = "+r3.toString(prec));
break;
case 3: r3=r1.multiply(r2);
System.out.print("a * b = "+r3.toString());
System.out.println(" = "+r3.toString(prec));
break;
case 4: r3=r1.divide(r2);
System.out.print("a / b = "+r3.toString());
System.out.println(" = "+r3.toString(prec));
break;
case 5: System.exit(0);
default: System.out.println("Invalid Choice! Choose 1-5");
}
}
}
}

class Rational{
private int numerator,denominator;
public Rational(){
this.numerator=0;
this.denominator=0;
}
public Rational(int a,int b){
this.numerator=a;
this.denominator=b;
}
public int getNumerator(){
return this.numerator;
}
public void setNumerator(int a){
this.numerator=a;
}
public int getDenominator(){
return this.denominator;
}
public void setDenominator(int b){
this.denominator=b;
}
public int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
public Rational addition(Rational r){
Rational temp=new Rational();
int num=numerator*r.getDenominator()+denominator*r.getNumerator();
int den=denominator*r.getDenominator();
int hcf=gcd(num,den);
temp.setNumerator(num/hcf);
temp.setDenominator(den/hcf);
return temp;
}
public Rational subtract(Rational r){
Rational temp=new Rational();
int num=numerator*r.getDenominator()-denominator*r.getNumerator();
int den=denominator*r.getDenominator();
int hcf=gcd(num,den);
temp.setNumerator(num/hcf);
temp.setDenominator(den/hcf);
return temp;
}
public Rational multiply(Rational r){
Rational temp=new Rational();
int num=numerator*r.getNumerator();
int den=denominator*r.getDenominator();
int hcf=gcd(num,den);
temp.setNumerator(num/hcf);
temp.setDenominator(den/hcf);
return temp;
}
public Rational divide(Rational r){
Rational temp=new Rational();
int num=numerator*r.getDenominator();
int den=denominator*r.getNumerator();
int hcf=gcd(num,den);
temp.setNumerator(num/hcf);
temp.setDenominator(den/hcf);
return temp;
}
public String toString(){
return (""+numerator+"/"+denominator);
}
public String toString(int precision){
String format="%."+precision+"f";
return String.format (format, (1.0)*numerator/denominator);
}
}

Output:

Enter numerator 1: 1
Enter denominator 1: 3
Enter numerator 2: 7
Enter denominator 2: 8
Enter precision: 6
1. Add
2. Subtract
3. Multiply
4. Division
5. Exit
Choice: 1
a + b = 29/24 = 1.208333
1. Add
2. Subtract
3. Multiply
4. Division
5. Exit
Choice: 2
a - b = -13/24 = -0.541667
1. Add
2. Subtract
3. Multiply
4. Division
5. Exit
Choice: 3
a * b = 7/24 = 0.291667
1. Add
2. Subtract
3. Multiply
4. Division
5. Exit
Choice: 4
a / b = 8/21 = 0.380952
1. Add
2. Subtract
3. Multiply
4. Division
5. Exit
Choice: 5

Add a comment
Know the answer?
Add Answer to:
Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) 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 it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) 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 it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app 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...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

    Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...

  • In mathematics, a rational number is any number that can be expressed as the quotient or...

    In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. Define a class that can represent for a rational number. Use the class in a C++ program that can perform all of the following operations with any two valid rational numbers entered at the keyboard...

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT