1.Make a class Rational to provide at least following methos and constructors: R
ID: 3644674 • 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 aJFrame 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];
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.
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 aJFrame 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];Explanation / Answer
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; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.