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

Java 1)Write a JFrame or JApplet (file name: TestRational.java) that performs th

ID: 3845299 • Letter: J

Question

Java

1)Write a JFrame or JApplet (file name: TestRational.java) that performs the following tasks:

a) Define two arrays of size 10. Each element in the array references to a Rational object that you wrote

Rational a[], b[];
a = new Rational[10];
b = new Rational[10];

b) Initialize the arrays in Question a) by creating random Rational numbers. Do this by generating two random numbers between 1 and 9 for each Rational and using the numbers as the numerator and denominator. Denominator should be greater than Numerator. You should generate random numbers with Math.random.

Display the contents of both arrays on the screen using JTextArea.

Given the Following Code:

import java.io.*;
import java.util.Scanner;

public class Rational {

private int numerator;
private int denominator;

//constructors
Rational()
{
numerator = 0;
denominator = 1;
}
Rational(int num, int den)
{
numerator = num;
denominator = den;
}

//getters
public int getNumerator()
{
return numerator;
}

public int getDenominator()
{
return denominator;
}

//modifiers
public void setNumerator(int num)
{
numerator = num;
}

public void setDenominator(int den)
{
denominator = den;
}

public Rational inputRational()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter numerator");
numerator = input.nextInt();
System.out.println ("Enter denominator");
denominator = input.nextInt();
return new Rational (numerator, denominator);
}

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

private int gcd(int num, int den)
{
int r;
while(den != 0)
{
r = num % den;
num = den;
den = r;
}
return num;
}

//add 2 rational numbers
public Rational add(Rational f)
{
int n;
int d;
n = (numerator * f.denominator) + (f.numerator * denominator);
d = denominator * f.denominator;
return new Rational(n,d);
}

//subtract 2 rational numbers
public void sub(Rational f1, Rational f2)
{
this.numerator = (f2.numerator * f1.numerator) - (f1.denominator * f2.denominator);
this.denominator = f1.denominator * f2.denominator;
}

//multiply 2 rational numbers
public Rational mul(Rational f)
{
int n;
int d;
n = numerator * f.numerator;
d = denominator * f.denominator;
return new Rational(n, d);
}

public void div(Rational f1, Rational f2)
{
this.numerator = f1.numerator * f2.denominator;
this.denominator = f1.denominator * f2.numerator;
}

public static double divReal(Rational f1, Rational f2)
{
double value;
value = (f1.numerator/f2.denominator)*(f2.denominator/f2.numerator);
return value;
}
}

Explanation / Answer

test rational.java:

import java.io.*;
import java.util.*;
import java.awt.*;
import java.math.*;

public class TestRational extends Rational
{
public TestRational() {}


Rational r1= new Rational (3,5);
Rational r2= new Rational (4,7);
Rational r3=r1.add(r2);

Rational a[] = new Rational(a[10]);
Rational b[] = new Rational(b[10]);

for(int i=0; i < 11; i++)
{
a[i] = (int)(Math.random() * 8 + 1);
}

for(int j=0; j < 11; j++)
{
b[j] = (int)(Math.random() * 8 + 1);
}

private JTextField field1;
private JTextField field2;
private JTextArea dis;

public TestRational()
{
JButton addition = new JButton("+");
JButton subtract = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton sortt = new JButton("Sort");

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());

addition.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Rational b1 = new Rational();
b1.add(a);
}
});

subtract.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent f)
{
Rational b2 = new Rational();
b2.subtract(a);
}
});

divide.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent g)
{
Rational b3 = new Rational();
b3.divide(a);
}
});

multiply.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent h)
{
Rational b4 = new Rational();
b4.multiply(a);
}
});

sortt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent p)
{
Arrays.sort(a);
/*for(int k=0; k<a.length; i++)
field2.setText("" + a[i] + " "); */

}
});



buttonPanel.add(addition);
buttonPanel.add(subtract);
buttonPanel.add(divide);
buttonPanel.add(multiply);
buttonPanel.add(sort);



//Jpanel for fraction area
JPanel mainDisplay = new JPanel();
field2 = new JTextField(20);
dis = new JTextArea(10,20);
JScrollPane scroll = new JScrollPane(dis);
mainDisplay.setLayout(new BorderLayout());
mainDisplay.add(dis);

setSize(300,300);
setVisible(true);

}
}

rational class:

public class Rational
{
private int numerator;
private int denom;

public Rational()
{
}

public Rational(int n, int d)
{
numerator = n;
denom = d;
}

public void setNumerator (int n)
{
numerator = n;
}
public int getNumerator()
{
return numerator;
}

public void setDenominator (int d)
{
denom = d;
}
public int getDenominator()
{
return denom;
}

public Rational add(Rational frac)
{
int top, bottom;
top = frac.getNumerator() * denom + frac.getDenominator() * numerator;
bottom = frac.getDenominator() * denom;

Rational sum = new Rational(top, bottom);
return sum;

}

public Rational subtract(Rational frac)
{
int top, bottom;
top = frac.getNumerator() * denom - frac.getDenominator() * numerator;
bottom = frac.getDenominator() * denom;


Rational difference = new Rational(top, bottom);

return difference;

}

public Rational multiply(Rational frac)
{
int up, down;

up = numerator * frac.getNumerator();
down = denom * frac.getDenominator();

Rational product = new Rational(up, down);

return product;

}


public Rational divide(Rational frac)
{
int upper, lower;

upper = frac.getDenominator() * numerator;
lower = frac.getNumerator() * denom;

Rational quotient = new Rational(upper, lower);

return quotient;
}


public String toString()
{
String x="";

if(numerator == 0)
x = "0";
else if(denom == 0)
/* --->>>*/System.out.println("Denominator cannot take a value of 0.");
else if(denom == 1)
x = "" + numerator;
else if(denom == numerator)
x = "1";
else
x = numerator + "/" + denom;

return x;
}
}


class Rational
   {
   private int num,den,firstNum,firstDen;
  
  
   public Rational()
   {
   num = firstNum = 0;
   den = firstDen = 1;
   }
  
  
   public Rational(int n, int d)
   {
   num = firstNum = n;
   den = firstDen = d;
   }
  
  
   public int getGCF(int n1,int n2)
   {
   int rem = n1 % n2;
  
   while(rem !=0)
   {
   n1 = n2;
   n2 = rem;
   rem = n1 % n2;
   }
       return n2;
  
   }
  
  
   private void reduce()
   {
   int gcf = getGCF(num,den);
   num/=gcf;
   den/=gcf;
  
   }
  
  
   public double getDecimal()
   {
   double decimal = (double) num / den;
   return decimal;
   }
  
   public String getRational()
   {
   return num + "/" + den;
   }
  
  
   public String getOriginal()
   {
   return num + "/" + den;
   }

  
   public int getNum()
   {
   return num;
   }
  
  
   public int getDen()
   {
   return den;
   }
  
  
   public void multiply(Rational f1,Rational f2)
   {
   num = firstNum = f1.getNum() * f2.getNum();
   den = firstDen = f1.getDen() * f2.getDen();
   reduce();
   }
  
  
   public void divide(Rational f1,Rational f2)
   {
   num = firstNum = f1.getNum() * f2.getDen();
   den = firstDen = f1.getDen() * f2.getNum();
   reduce();
  
  
   }
  
  
   public void add(Rational f1,Rational f2)
   {
   num = firstNum = f1.getNum() + getGCF(num,den);
   den = firstDen = f1.getDen() + getGCF(num,den);
  
   // This is the other problem method.
  
   }
  
  
   public void subtract(Rational f1,Rational f2)
   {
   num = firstNum = f1.getNum() - f2.getNum();
   den = firstDen = f1.getDen() - f2.getDen();
  
  
   }
  
   }

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