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

Java Make a class Rational to provide at least following methods and constructor

ID: 3840978 • Letter: J

Question

Java

Make a class Rational to provide at least following methods 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 provide methods 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

/**
*
* @author Sam
*/
public class Rational {
    private int num;
    private int din;

    public Rational(int num, int din) {
        this.num = num;
        this.din = din;
    }
  
    public Rational add(Rational other){
        int newDin, newNum;
        newDin = din*other.din;
        newNum = num*other.din + other.num*din;
        Rational out = new Rational(newNum, newDin);
        out.reduce();
        return out;
    }
  
    public Rational sub(Rational other){
        int newDin, newNum;
        newDin = din*other.din;
        newNum = num*other.din - other.num*din;
        Rational out = new Rational(newNum, newDin);
        out.reduce();
        return out;
    }
  
    public Rational mult(Rational other){
        int newDin, newNum;
        newDin = din*other.din;
        newNum = num*other.num;
        Rational out = new Rational(newNum, newDin);
        out.reduce();
        return out;
    }
  
    public Rational div(Rational other){
        int newDin, newNum;
        newDin = din*other.num;
        newNum = num*other.din;
        Rational out = new Rational(newNum, newDin);
        out.reduce();
        return out;
    }
  
    private void reduce() {
        int a = num;
        int b = din;
              
        while (a!=b) {
           if (a > b)
               a = a - b;
           else
               b = b - a;
        }
      
        num /= a;
        din /= a;
    }
}

You can use this metod to generate 10 Random rational numbers


    public static Rational[] generate() {
        Rational[] arr = new Rational[10];
        for (int i = 0; i < 10; i++) {
            int num = (int) Math.round(Math.random()*9);
            int din = (int) Math.round(Math.random()*9);
            arr[i] = new Rational(num, din);
        }
        return arr;
    }

NOTE: We are allowed to answer only one quesion at a time. So am really sorry for not being able to answer question 2.

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