Rational r1= new Rational (3,5); Rational r2= new Rational (4,7); Rational r3=r1
ID: 640396 • Letter: R
Question
Rational r1= new Rational (3,5);
Rational r2= new Rational (4,7);
Rational r3=r1.add(r2);
// also sub,mult,divide methods
Other that four operations you should provid methos that you think is useful for user of your class.
2. 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 in Problem 1 above.
Rational a[], b[];
a = new Rational[10];
b = new Rational[10];
Rational
Numerator
Denominator
.
.
.
.
.
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.
c) Display the contents of both arrays on the screen using JTextArea.
d) Add a Scrollbar to JTextArea for scrolling up or down.
e) For each public method in Rational class, create a corresponding JButton object. When the user clicks on a button, the actionPerformed listener should execute the corresponding Rational method for all the elements in the two arrays and store the results in a new array. You should then display the result array in the JTextArea using the setText method. For example, if the user hits the add button you should loop through all the elements in arrays and execute:
c[i] = a[i].add(b[i]);
f) If the user hits the sort button, uses the JTextArea method append to append the results of sorting.
Rational
Numerator
Denominator
Explanation / Answer
1)
public class Example
{
public static void main(String[] args)
{
Rational r1 = new Rational(2, 5);
Rational r2 = new Rational(3, 5);
Rational result = r1.add(r2);
int a = result.getNumerator();
int b = result.getDenominator();
System.out.println("2/5 + 3/5 = "+ a +"/"+ b);
result = r1.sub(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("2/5 - 3/5 = "+ a +"/"+ b);
result = r1.muti(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("2/5 * 3/5 = "+ a +"/"+ b);
result = r1.div(r2);
a = result.getNumerator();
b = result.getDenominator();
System.out.println("(2/5) / (3/5) = "+ a +"/"+ b);
System.out.println("2/1 + 3/2 + 5/3+ 8/5...10");
int n = 10;
int k = 0;
Rational sum = new Rational(0, 10);
Rational item = new Rational(2, 1);
while (k < n)
{
sum = sum.add(item);
k++;
int numerator = sum.getNumerator();
int denominator = sum.getDenominator();
item.setNumeratorAndDenominator(numerator + denominator, denominator);
}
a = sum.getNumerator();
b = sum.getDenominator();
System.out.println(":");
System.out.println(a +"/"+ b);
double doubleResult = (a*1.0) / b;
System.out.println(":");
System.out.println(doubleResult);
}
}
2)
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);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.