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

Define 3 constructors and a toString method, setters, and getters Define arithme

ID: 3632904 • Letter: D

Question

Define 3 constructors and a toString method, setters, and getters
Define arithmetic methods for add, subtract, multiply, divide, and negate. The negate method will return the negative of the calling Rational object – return new Rational (-num,denom);
Define logical methods for equals, notEquals, lessThan, lessThanOrEqual, greaterThan, and greaterThanOrEqual.

All negative values should be represented with the negative sign in the numerator – do this in the setDenom method and call it in the constructor.

Extra Credit: add a greatestCommonDivisor method to reduce a Rational number to its lowest terms. If you write this method, add the reduced values to your output. For example:
½ + ¾ = 10/8 reduced to 5/4

Run three times with the following data: (read in the data then use one of the constructors or the setter methods to fill objects with data)

Input 1: 1/2, 3/4 - entered as 1 2 3 4

Input 2: 1/2, 5/10 – entered as 1 2 5 10

Input 3: 3/-5, 2 – entered as 3 -5 2 1


Output (using the first set of numbers):

Rational 1 –>1/2 reduced to _______
Rational 2 -> 3/4 reduced to_______
½ + ¾ ______ reduced to_______
½ - ¾ ______ reduced to_______
½ * ¾ ______ reduced to_______
½ / ¾ ______ reduced to_______
- ½ ______ reduced to_______
½ == ¾ ______ print true or false
½ != ¾ ________ print true or false
½ > ¾ ______ print true or false
½ >= ¾ ______ print true or false
½ < ¾ ______ print true or false
½ <= ¾ ______ print true or false

Explanation / Answer

/**

Test class that exercises the methods of Rational

*/

public class RationalDemo {


public void test() {

// Test constructor, setters, toString, and normalize

Rational Rational();

oneHalf.setNumerator(1);

oneHalf.setDenominator(2);

System.out.println("One half: " + oneHalf);


Rational twoQuarters = new Rational();

twoQuarters.setNumerator(-2);

twoQuarters.setDenominator(-4);


Rational negativeThree = new Rational(-3);

System.out.println("Negative three: " + negativeThree);

Rational twoThirds = new Rational(2, 3);

System.out.println("Two thirds: " + twoThirds);


// Test the getter methods

System.out.println("Numerator of " + twoThirds + ": " + twoThirds.getNumerator());

System.out.println("Denominator of " + twoThirds + ": " + twoThirds.getDenominator());

System.out.println("Value of " + twoThirds + ": " + twoThirds.getDoubleValue());


// Test equality

System.out.println(oneHalf + " = " + twoThirds + " ? " + oneHalf.equals(twoThirds));

System.out.println(oneHalf +" = " + twoQuarters + " ? " + oneHalf.equals(twoQuarters));


// Test the arithmetic operations

System.out.println(oneHalf + " + " + twoThirds + " = " +

Rational.add(oneHalf, twoThirds));

System.out.println(oneHalf + " - " + twoThirds + " = " +

Rational.subtract(oneHalf, twoThirds));

System.out.println(oneHalf + " * " + twoThirds + " = " +

Rational.multiply(oneHalf, twoThirds));

System.out.println(oneHalf + " / " + twoThirds + " = " +

Rational.divide(oneHalf, twoThirds));

}


}

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