*Use the syntax from the pics below********************** Write a program in Jav
ID: 3570779 • Letter: #
Question
*Use the syntax from the pics below**********************
Write a program in Java and run it in BlueJ according to the following specifications:
The program first generates three random rational numbers (fractions) and prints them both as rational numbers and as floating point numbers.
Then it finds the rational numbers that reperesent the maximum value and the minimum value and prints them (both as rational numbers and floating point numbers).
Finally it also prints the sum and the average of the three rational numbers as rational numbers reduced to lowest terms as well as floating point numbers.
The output of the program may look like the following:
Rational numbers: 2/5, 4/5, 6/4 (Floating point format: 0.4, 0.8, 1.5)
Maximum: 6 / 4 (1.5)
Minimum: 2 / 5 (0.4)
Sum: 27 / 10 (2.7)
Average: 9 / 10 (0.9)
Requirements and restrictions:
Use the roll() method from Die.java class to generate random numerators and denominators for the rational numbers.
Use the Rational.java class from the course website to represent rational numbers and extend it with:
a method to compare two rational numbers
and a method to divide rational numbers.
a method that returns the floating point value of a rational number.
All calculations, comparisons and printing should be implemented with methdos that work with rational numbers only:
Use the method to compare rational numbers to find the minimum and the maximum.
Use the sum method from the Rational.java class and the method to divide rational numbers to find the average.
Use the reduce method from class Rational to reduce the sum and the average to lowest terms.
For printing use the method that returns the floating point value of a rational number.
When you write your program
use proper names for the variables suggesting their purpose.
format your code accordingly using indentation and spacing.
use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section.
for each line of code add a short comment to explain its meaning.
*Use the syntax from the pics below********************** Write a program in Java and run it in BlueJ according to the following specifications: The program first generates three random rational numbers (fractions) and prints them both as rational numbers and as floating point numbers. Then it finds the rational numbers that reperesent the maximum value and the minimum value and prints them (both as rational numbers and floating point numbers). Finally it also prints the sum and the average of the three rational numbers as rational numbers reduced to lowest terms as well as floating point numbers. The output of the program may look like the following: Rational numbers: 2/5, 4/5, 6/4 (Floating point format: 0.4, 0.8, 1.5) Maximum: 6 / 4 (1.5) Minimum: 2 / 5 (0.4) Sum: 27 / 10 (2.7) Average: 9 / 10 (0.9) Requirements and restrictions: Use the roll() method from Die.java class to generate random numerators and denominators for the rational numbers. Use the Rational.java class from the course website to represent rational numbers and extend it with: a method to compare two rational numbers and a method to divide rational numbers. a method that returns the floating point value of a rational number. All calculations, comparisons and printing should be implemented with methdos that work with rational numbers only: Use the method to compare rational numbers to find the minimum and the maximum. Use the sum method from the Rational.java class and the method to divide rational numbers to find the average. Use the reduce method from class Rational to reduce the sum and the average to lowest terms. For printing use the method that returns the floating point value of a rational number. When you write your program use proper names for the variables suggesting their purpose. format your code accordingly using indentation and spacing. use multiple line comment in the beginning of the code and write your name, e-mail address, class, and section. for each line of code add a short comment to explain its meaning.Explanation / Answer
public class Rational { private int numer, denom; //constructors public Rational(){ int num = 1; int den = 2; reduce(); } public Rational(int num, int den){ numer = num; denom = den; reduce(); } public Rational(Rational x){ numer = x.numer; denom = x.denom; reduce(); } //setters public void setNumer(int num){ numer = num; reduce(); } public void setDenom(int den){ denom = den; reduce(); } public void setRational(int num, int den){ numer = num; denom = den; reduce(); } //getters public int getNumer(){ return numer; } public int getDenom(){ return denom; } //Copy method public void copyFrom(Rational x){ numer = x.numer; denom = x.denom; reduce(); } //Equals method public boolean equals(Rational x){ if (numer / denom == x.numer / x.denom){ return(true); } else { return(false); } } //Compare to method public int compareTo(Rational x){ if (numer / denom == x.numer / x.denom){ return (0); } else if (numer / denom numer){ numer = (x.numer * multx) - (numer * mult); } else { numer = (numer * mult) - (x.numer * multx); } reduce(); } //Multiplication public void times(Rational x){ numer = numer * x.numer; denom = denom * x.denom; reduce(); } //Division public void divBy(Rational x){ numer = numer / x.numer; denom = denom / x.denom; reduce(); } //Fraction simplifier private void reduce(){ int divisor; divisor = Rational.gcd(numer, denom); numer = numer / divisor; denom = denom / divisor; } @Override public String toString(){ if (denom == 1){ return numer + ""; } else{ return numer + " / " + denom; } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.