Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java 1)Add a Scrollbar to JTextArea for scrolling up or down. 2) For each public method in Rational class, create a corresponding JButton object. When

Java

1)Add a Scrollbar to JTextArea for scrolling up or down.

2) 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]);

3) If the user hits the sort button, uses the JTextArea method append to append the results of sorting.

//please add this to the following code//

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

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(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago