1. Make a class Rational to provide at least following methos and constructors:
ID: 3539725 • Letter: 1
Question
1. Make a class Rational to provide at least following methos and constructors:
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.
Explanation / Answer
Just place the values and you will get the answer. 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; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.