I have to write a reduce method ( as seen below) I have the method all written o
ID: 3688915 • Letter: I
Question
I have to write a reduce method ( as seen below) I have the method all written out, I am just unsure how to call it in the constructor and mutators. I will include a copy of my code.
The last method to write is reduce. This receives no parameters and adjusts this Rational by reducing its numerator and denominator as much as possible. This will be a private method and called by the 2-parameter constructor and the two mutators. You do not need to call it in add for instance because add creates a new Rational and in doing so, that Rational’s constructor calls reduce. Let’s imagine that the Rational has a numerator of 120 and a denominator of 100. This would reduce to 6 / 5. How do we compute this reduction? The easiest way is to start a divisor at 2 and see if we can divide both numerator and denominator by our divisor. If so, we repeat, otherwise we add 1 to the divisor. We continue to do this until the divisor is greater than either the numerator or denominator. Here is the pseudocode:
the numerator and less than the denominator
if numerator % divisor == 0 and denominator % divisor == 0
MY CODE:
public class Rational {
private int numerator, denominator;
public Rational(int a, int b){
numerator = a;
denominator = b;
if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1;
}else if (denominator == 0){
denominator = 1;
numerator = 0;
}
Rational.this.reduce();
}
public Rational(){
denominator = 1;
numerator = 0;
}
public void setNumerator(int a){
numerator = a;
Rational.this.reduce();
}
public int getNumerator(){
return numerator;
}
public void setDenominator(int b){
if (b < 0){
b = b * -1;
denominator = b;
if (numerator < 0){
;
}else{
numerator = getNumerator() * -1;
}
}else if (b == 0){
setNumerator(0);
setDenominator(1);
}else{
denominator = b;
}
Rational.this.reduce();
}
public int getDenominator(){
return denominator;
}
public String toString(){
String stringFormat = numerator + " / " + denominator;
return stringFormat;
}
public int compareTo(Rational a){
int thisRational = numerator * a.getDenominator();
int otherRational = a.getNumerator() * denominator;
if (thisRational < otherRational){
return -1;
}else if (thisRational > otherRational ){
return 1;
}else{
return 0;
}
}
public Rational add(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
public Rational subtract(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = (num1 * denom2) - (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
public Rational multiply(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = num1 * num2;
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
public Rational divide(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = num1 * denom2;
int denom3 = denom1 * num2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
private void reduce(){
int divisor = 2;
while (divisor < Math.abs(numerator) && divisor < denominator){
if (numerator % divisor == 0 && denominator % divisor == 0){
this.numerator = numerator / divisor;
this.denominator = denominator / divisor;
}else {
divisor ++;
}
}
}
}
Explanation / Answer
Rational.java
package org.students;
public class Rational {
private int numerator, denominator;
//Parameterized constructor
public Rational(int a, int b){
numerator = a;
denominator = b;
if (denominator < 0){
denominator = denominator * -1;
numerator = numerator * -1;
}else if (denominator == 0){
denominator = 1;
numerator = 0;
}
Rational.this.reduce();
}
//Default constructor
public Rational(){
denominator = 1;
numerator = 0;
}
//Setters and Getters
public void setNumerator(int a){
numerator = a;
Rational.this.reduce();
}
public int getNumerator(){
return numerator;
}
public void setDenominator(int b){
if (b < 0){
b = b * -1;
denominator = b;
if (numerator < 0){
;
}else{
numerator = getNumerator() * -1;
}
}else if (b == 0){
setNumerator(0);
setDenominator(1);
}else{
denominator = b;
}
Rational.this.reduce();
}
public int getDenominator(){
return denominator;
}
//Method which displays the Rational Number
public String toString(){
String stringFormat = numerator + " / " + denominator;
return stringFormat;
}
//Method which comparing two rational numbers
public int compareTo(Rational a){
int thisRational = numerator * a.getDenominator();
int otherRational = a.getNumerator() * denominator;
if (thisRational < otherRational){
return -1;
}else if (thisRational > otherRational ){
return 1;
}else{
return 0;
}
}
//Method which adding two rational numbers
public Rational add(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
//method which subtracting two rational numbers
public Rational subtract(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = (num1 * denom2) - (num2 * denom1);
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
//Method which multiply two rational numbers
public Rational multiply(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = num1 * num2;
int denom3 = denom1 * denom2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
//Method which divide two rational numbers
public Rational divide(Rational other){
int num1 = numerator;
int denom1 = denominator;
int num2 = other.getNumerator();
int denom2 = other.getDenominator();
int num3 = num1 * denom2;
int denom3 = denom1 * num2;
Rational r1 = new Rational(num3, denom3);
return r1;
}
//Method which reduce the rational number
public String reduce( )
{
// determine the greatest common divisor
int gcd = this.greatestCommonDivisor(numerator, denominator);
// if GCD is negative, change to positive
if (gcd < 0)
{
gcd = -gcd;
}
// divide gcd into both numerator and denominator
numerator = numerator / gcd;
denominator = denominator / gcd;
return numerator+"/"+denominator;
}
//Method which calculate the GCD
private Integer greatestCommonDivisor(Integer a, Integer b)
{
// "private"
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0) {
return b;
}
// recursive case
else {
return greatestCommonDivisor(b, a % b);
}
}
}
_________________________________________________________________________________________
Test.java
package org.students;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
while (true) {
//Getting the First Rational number From the user
System.out.println(":: Enter Rational No 1 ::");
System.out.print("Enter Numerator =");
int num1 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom1 = sc.nextInt();
//Getting the second Rational number From the user
System.out.println(":: Enter Rational No 2 ::");
System.out.print("Enter Numerator =");
int num2 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom2 = sc.nextInt();
//Creating the objects to Rational Class
Rational r = new Rational(num1, denom1);
Rational r1 = new Rational(num2, denom2);
System.out.println(" ");
// Reducing the Rational Numbers
String red = r.reduce();
String red1 = r1.reduce();
System.out.println("(" + r.toString() + ") reduced to :" + red);
System.out.println("(" + r1.toString() + ") reduced to :" + red1);
// Addition of two Rational Numbers
Rational radd = r.add(r1);
System.out.println("(" + r.toString() + ") +" + "(" + r1.toString()
+ ") = " + radd);
// Comparing Two Rational Numbers
int com = r.compareTo(r1);
if (com == -1)
System.out.println("(" + r.toString() + ") < (" + r1.toString()
+ ")");
else
System.out.println("(" + r.toString() + ") > (" + r1.toString()
+ ")");
// Subtracting two rational numbers
Rational rsub = r.subtract(r1);
System.out.println("(" + r.toString() + ") - (" + r1.toString()
+ ") = " + rsub);
// Multiplying two rational numbers
Rational rmul = r.multiply(r1);
System.out.println("(" + r.toString() + ") * (" + r1.toString()
+ ") = " + rmul);
// Dividing two rational numbers
Rational r44 = r.divide(r1);
System.out.println("(" + r.toString() + ")" + " / " + "("
+ r1.toString() + ")" + " = " + r44);
System.out.println(" ");
System.out.print("Do you want to continue(Y/N):");
char c = sc1.next(".").charAt(0);
if (c == 'Y' || c == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
__________________________________________________________________________________________
output:
:: Enter Rational No 1 ::
Enter Numerator =10
Enter Denominator =2
:: Enter Rational No 2 ::
Enter Numerator =25
Enter Denominator =30
(5 / 1) reduced to :5/1
(5 / 6) reduced to :5/6
(5 / 1) +(5 / 6) = 35 / 6
(5 / 1) > (5 / 6)
(5 / 1) - (5 / 6) = 25 / 6
(5 / 1) * (5 / 6) = 25 / 6
(5 / 1) / (5 / 6) = 6 / 1
Do you want to continue(Y/N):y
:: Enter Rational No 1 ::
Enter Numerator =4
Enter Denominator =5
:: Enter Rational No 2 ::
Enter Numerator =9
Enter Denominator =27
(4 / 5) reduced to :4/5
(1 / 3) reduced to :1/3
(4 / 5) +(1 / 3) = 17 / 15
(4 / 5) > (1 / 3)
(4 / 5) - (1 / 3) = 7 / 15
(4 / 5) * (1 / 3) = 4 / 15
(4 / 5) / (1 / 3) = 12 / 5
Do you want to continue(Y/N):n
:: PRogram Exit ::
_____________________________________________________________________________________