Define 3 constructors and a toString method, setters, and getters Define arithme
ID: 3631633 • Letter: D
Question
Define 3 constructors and a toString method, setters, and gettersDefine 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.
Input 1: 1/2, 3/4 - entered as 1 2 3 4
Explanation / Answer
There is also a reduce method that should be there if you're going to keep displaying the rational and its reduced equivalent. I ook the liberty of adding that. Also, I declared the arithmetic operations as static methods that can be called without having an instance of type Rational to call it from since you essentially pass both values to it. But if that's not how you want it, then just pass r2 to it and r1 values can be replaced by this.getNumerator and this.getdenominator :)
If this is what you needed then please rate ! If you have questions about the code just PM me or post a comment.
We have 2 classes here, the Rational class, and the Test class that has the main execution method for testing.
Rational.java is this one:
public class Rational {
private int numerator;
private int denominator;
// Constructors
public Rational() {
numerator = 1;
setDenominator(1);
}
public Rational(int num, int denom) {
numerator = num;
setDenominator(denom);
}
public Rational(Rational r) {
numerator = r.getNumerator();
setDenominator(r.getDenominator());
}
// getter for numerator
public int getNumerator() {
return this.numerator;
}
//setter for numerator
public void setNumerator(int n) {
numerator = n;
}
//getter for denominator
public int getDenominator() {
return this.denominator;
}
//setter for denominator
public void setDenominator(int d) {
if (d > 0) {
denominator = d;
} else if (d < 0) {
denominator = -d;
numerator = -numerator;
} else {
System.out.println("Denominator cannot be Zero!");
}
}
@Override
public String toString() {
return getNumerator() + "/" + getDenominator();
}
public static Rational add(Rational r1, Rational r2) {
int num = r1.getNumerator() * r2.getDenominator()
+ r2.getNumerator() * r1.getDenominator();
int denom = r1.getDenominator() * r2.getDenominator();
return new Rational(num, denom);
}
// this is for R1 - R2
public static Rational subtract(Rational r1, Rational r2) {
int num = r1.getNumerator() * r2.getDenominator()
- r2.getNumerator() * r1.getDenominator();
int denom = r1.getDenominator() * r2.getDenominator();
return new Rational(num, denom);
}
public static Rational multiply(Rational r1, Rational r2) {
int num = r1.getNumerator() * r2.getNumerator();
int denom = r1.getDenominator() * r2.getDenominator();
return new Rational(num, denom);
}
// Dividing R1 by R2
public static Rational divide(Rational r1, Rational r2) {
int num = r1.getNumerator() * r2.getDenominator();
int denom = r1.getDenominator() * r2.getNumerator();
return new Rational(num, denom);
}
public static Rational negate(Rational r) {
return new Rational(-1 * r.getNumerator(), r.getDenominator());
}
public boolean equals(Rational r) {
Rational temp = subtract(this, r);
return (temp.getNumerator() == 0);
}
public boolean notEquals(Rational r) {
Rational temp = subtract(this, r);
return (temp.getNumerator() != 0);
}
public boolean lessThan(Rational r) {
Rational temp = subtract(this, r);
return (temp.getNumerator() < 0);
}
public boolean lessThanOrEqual(Rational r) {
return (lessThan(r) || equals(r));
}
public boolean greaterThan(Rational r) {
Rational temp = subtract(this, r);
return (temp.getNumerator() > 0);
}
public boolean greaterThanOrEqual(Rational r) {
return (greaterThan(r) || equals(r));
}
public Rational reduce() {
// reduce the fraction using the Euclidean Algorithm
int num1 = 0, num2 = 0;
if (this.getNumerator() >= this.getDenominator()) {
num1 = getNumerator();
num2 = getDenominator();
} else {
num1 = getDenominator();
num2 = getNumerator();
}
int remainder = num2;
while (remainder > 0) {
remainder = num1 % num2;
if (remainder == 0) {
break;
}
num1 = num2;
num2 = remainder;
}
return new Rational(getNumerator() / num2, getDenominator() / num2);
}
}
The test class RationalTest.java:
import java.util.Scanner;
public class RationalTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Rational r1 = new Rational();
System.out.print(" Enter 2 rational numbers a/b and c/d as a b c d : ");
r1.setNumerator(scanner.nextInt());
r1.setDenominator(scanner.nextInt());
Rational r2 = new Rational(scanner.nextInt(), scanner.nextInt());
Rational r3 = new Rational();
System.out.print(" Enter 2 rational numbers a/b and c/d as a b c d : ");
r3.setNumerator(scanner.nextInt());
r3.setDenominator(scanner.nextInt());
Rational r4 = new Rational(scanner.nextInt(), scanner.nextInt());
Rational r5 = new Rational();
Rational r6 = new Rational();
System.out.print(" Enter 2 rational numbers a/b and c/d as a b c d : ");
r5.setNumerator(scanner.nextInt());
r5.setDenominator(scanner.nextInt());
r6.setNumerator(scanner.nextInt());
r6.setDenominator(scanner.nextInt());
System.out.println("Rational 1 -> " + r1.toString() + " reduced to " + r1.reduce().toString());
System.out.println("Rational 2 -> " + r2.toString() + " reduced to " + r2.reduce().toString());
System.out.println(r1.toString() + " + " + r2.toString() + " = " + Rational.add(r1, r2).toString() + " reduced to " + Rational.add(r1, r2).reduce().toString());
System.out.println(r1.toString() + " - " + r2.toString() + " = " + Rational.subtract(r1, r2).toString() + " reduced to " + Rational.subtract(r1, r2).reduce().toString());
System.out.println(r1.toString() + " * " + r2.toString() + " = " + Rational.multiply(r1, r2).toString() + " reduced to " + Rational.multiply(r1, r2).reduce().toString());
System.out.println(r1.toString() + " / " + r2.toString() + " = " + Rational.divide(r1, r2).toString() + " reduced to " + Rational.divide(r1, r2).reduce().toString());
System.out.println("Negated - " + r1.toString() + " is " + Rational.negate(r1).toString() + " reduced to " + Rational.negate(r1).reduce().toString());
System.out.println(r1.toString() + " == " + r2.toString() + " is " + r1.equals(r2));
System.out.println(r1.toString() + " != " + r2.toString() + " is " + r1.notEquals(r2));
System.out.println(r1.toString() + " > " + r2.toString() + " is " + r1.greaterThan(r2));
System.out.println(r1.toString() + " >= " + r2.toString() + " is " + r1.greaterThanOrEqual(r2));
System.out.println(r1.toString() + " < " + r2.toString() + " is " + r1.lessThan(r2));
System.out.println(r1.toString() + " <= " + r2.toString() + " is " + r1.lessThanOrEqual(r2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.