Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java and run it in BlueJ according to the following specifica

ID: 3817304 • Letter: W

Question

Write a program in Java and run it in BlueJ according to the following specifications:

The program first generates at random three positive proper fractions (rational numbers) with positive denominators less than 10 and prints them.

Then it finds the rational numbers that represent the maximum value and the minimum value and prints them both as rational numbers and as 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:

You can't use arrays. I need to create the main method that produces those above things.

Here is the class with all of the data needed to help create the method:

class Rational {

private int numerator;
private int denominator;

public Rational (int a, int b)
{
numerator = a;
denominator = b;
}

public Rational sum (Rational r)
{
int num, den;
den = denominator * r.getDenominator();
num = numerator * r.getDenominator() + r.getNumerator() * denominator;
return new Rational (num, den);
}

public int getNumerator() { return numerator; }

public int getDenominator() { return denominator; }

public String toString () { return numerator + "/" + denominator; }

public void reduce() {

int factor;
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}

private int gcd (int x, int y) {

int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}

}

Explanation / Answer

class Rational {
private int numerator;
private int denominator;
public Rational (int a, int b)
{
numerator = a;
denominator = b;
}
public Rational sum (Rational r)
{
int num, den;
den = denominator * r.getDenominator();
num = numerator * r.getDenominator() + r.getNumerator() * denominator;
return new Rational (num, den);
}
public int getNumerator() { return numerator; }
public int getDenominator() { return denominator; }
public String toString () { return numerator + "/" + denominator; }
public void reduce() {
int factor;
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}
private int gcd (int x, int y) {
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}

//this function return true if the given rational is smaller.
public boolean isGreater(Rational r){
int den = r.getDenominator();
int num = r.getNumerator();
int lcm = (this.denominator*den)/gcd(this.denominator, den);

num *= lcm/den;
int myNum = this.numerator*(lcm/this.denominator);
return myNum > num;
}

//this function return true if the given rational is greater.
public boolean isSmaller(Rational r){
int den = r.getDenominator();
int num = r.getNumerator();
int lcm = (this.denominator*den)/gcd(this.denominator, den);

num *= lcm/den;
int myNum = this.numerator*(lcm/this.denominator);
return myNum < num;
}

//this fraction converts the number into a fraction
public float fraction(){
return (float)numerator/denominator;
}
}

public class Solution {
public static void main(String[] args){
Rational[] r = new Rational[3]; //array of three rationals
for(int i=0;i<3;i++){
int num = (int)(Math.random()*9 + 1);
int den = (int)(Math.random()*9 + 1);

r[i] = new Rational(num, den);
}
System.out.println("Rational numbers: " + r[0].toString() + "," + r[1].toString() +
"," + r[2].toString());
  
if(r[0].isGreater(r[1]))
{
if(r[0].isGreater(r[2]))
{
System.out.println("Maximum: " + r[0].toString() + " (" + r[0].fraction() + ")");
}
else
{
System.out.println("Maximum: " + r[2].toString() + " " + r[2].fraction() + ")");
}
}
else {
if (r[1].isGreater(r[2])) {
System.out.println("Maximum: " + r[1].toString() + " (" + r[1].fraction() + ")");
} else {
System.out.println("Maximum: " + r[2].toString() + " (" + r[2].fraction() + ")");
}
}

if(r[0].isSmaller(r[1])) {
if(r[0].isSmaller(r[2]))
{
System.out.println("Minimum: " + r[0].toString() + " (" + r[0].fraction() + ")");
}
else
{
System.out.println("Minimum: " + r[2].toString() + " " + r[2].fraction() + ")");
}
}
else {
if(r[1].isSmaller(r[2]))
{
System.out.println("Minimum: " + r[1].toString() + " (" + r[1].fraction() + ")");
}
else
{
System.out.println("Minimum: " + r[2].toString() + " (" + r[2].fraction() + ")");
}
}
  
Rational sumt = r[0].sum(r[1]);
sumt = sumt.sum(r[2]);
sumt.reduce();
System.out.println("Sum: " + sumt.toString()+ " (" + sumt.fraction()+")" );
  
Rational avg = new Rational(sumt.getNumerator(), sumt.getDenominator()*3);
avg.reduce();
System.out.println("Average: " + avg.toString()+ " (" + avg.fraction()+")" );
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote