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

1.Make a class Rational to provide at least following methods and constructors:

ID: 3692089 • Letter: 1

Question

1.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 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];

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

import java.io.*;

import java.util.*;

//Rational class

public class Rational

{

//declaring variables as private

private int numerator;

private int denom;

//default constrctor

public Rational()

{

}

//parameterized constructor for fraction

public Rational(int n, int d)

{

numerator = n;

denom = d;

}

//setter and getter methods for numerator and denominators

public void setNumerator (int n)

{

numerator = n;

}

public int getNumerator()

{

return numerator;

}

public void setDenominator (int d)

{

denom = d;

}

public int getDenominator()

{

return denom;

}

//add method taking frac rational object and add it and returns another Rational object

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;

}

//subtract method taking frac rational object and subtract it and returns another Rational object

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;

}

//multiply method taking frac rational object and multiply it and returns another Rational object

public Rational multiply(Rational frac)

{

int up, down;

up = numerator * frac.getNumerator();

down = denom * frac.getDenominator();

Rational product = new Rational(up, down);

return product;

}

//divide method taking frac rational object and divide it and returns another Rational object

public Rational divide(Rational frac)

{

int upper, lower;

upper = frac.getDenominator() * numerator;

lower = frac.getNumerator() * denom;

Rational quotient = new Rational(upper, lower);

return quotient;

}

//toString() method returns String bject

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;

}

}

//Test class for implementing methods in Rational class

import java.io.*;

import java.util.*;

import java.awt.*;

import java.math.*;

public class Test extends Rational

{

//default constructor

//creating objects for Rational class

Rational r1= new Rational (3,5);

Rational r2= new Rational (4,7);

Rational r3=r1.add(r2);

Rational r4=r1.subtract(r2);

Rational r5=r1.multiply(r2);

Rational r6=r1.divide(r2);

//creating array objects for Rational class

Rational a[] = new Rational(a[10]);

Rational b[] = new Rational(b[10]);

//generating 10 random numbers between 1 t0 9 for a[]

for(int i=0; i < 11; i++)

{

a[i] = (int)(Math.random() * 8 + 1);

}

//generating 10 random numbers between 1 t0 9 for b[]

for(int j=0; j < 11; j++)

{

b[j] = (int)(Math.random() * 8 + 1);

}

//creating JTextField objects and JTextArea object as private

private JTextField field1;

private JTextField field2;

private JTextArea dis;

//initializing default constructor

public Test()

{

//adding buttons for addition,subtaction,multiply and division opeartions

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

//adding Action Listeners for performing arithmetic operations and sorting operation

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

}

});

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

}

}