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

Here is what my program needs to do: Write a RationalNumber class having public

ID: 3625745 • Letter: H

Question

Here is what my program needs to do:

Write a RationalNumber class having public methods for:

public RationalNumber() // Initializes to 0/1
public RationalNumber(int n, int d) // Initializes to n/d
public RationalNumber(RationalNumber x) // Initializes to x’s values

public void add(RationalNumber other) // this = this + other
public void subtract(RationalNumber other) // this = this - other
public void multiply(RationalNumber other) // this = this * other
public void divide(RationalNumber other) // this = this / other

public String toString() // Creates string representation
public boolean equals(RationalNumber other) // Is this == other?

- Any methods not listed above and all data fields should be private.
- r.equals(s) should return true if r and s have the same value.
- toString should create a String representation of the rational, in reduced form. If a number is negative the string should begin with -. If it is a whole number (including zero) it should be shown as over 1. For example. If I initialize with n=4 and d=-2, toString should return "-2/1".
- If d=0 is passed to the constructor, throw an IllegalArgumentException.

Here is my code:


public class RationalNumber{
private int num;
private int den;
public RationalNumber(){ //Initializes to 0/1
num = 1;
den = 1;
}
public RationalNumber(int num, int den){ //Initializes to n/d
try{
if(den==0){
throw new IllegalArgumentException("Zero value at constructor");
}
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
reduce();
}
}

//public RationalNumber(RationalNumber x){//Initializes to x's values

public void add(RationalNumber other){ // this = this + other
int resultDen = den * other.den;
int resultNum = num * other.den + other.num * den;
toString (RationalNumber( resultNum, resultDen ));
}

public void subtract(RationalNumber other){ // this = this - other
int resultDen = den * other.den;
int resultNum = num * other.den - other.num * den;
toString (RationalNumber( resultNum, resultDen ));
}

public void multiply(RationalNumber other){ // this = this * other
toString (RationalNumber( num * other.num,den * other.den ));
}
public void divide(RationalNumber other){ // this = this / other
toString (RationalNumber( num / other.den),
(den / other.num ));
}
private void reduce(){
int yes = 0;
int smaller;
if ( num < den ){
smaller = num;
}else{
smaller = den;
for ( int div = smaller; div >= 2; div-- ){
if ( num % div == 0 && den % div == 0 ){
yes = div;
break;
}
}
if ( yes != 0 ){
num /= yes;
den /= yes;
}
}
}
public String toString(){ // Creates string representation
return num + "/" + den;
}
public boolean equals(RationalNumber other){ // Is this == other?
if( num == other.num && den == other.den){
return true;
}else{
return false;
}
}
}

And as an added bonus, my professor wants us to not use a try-catch in our program.
Some one please help me, I will rate lifesaver if the program functions correctly after the help.

Explanation / Answer

public class RationalNumber { private int num; private int den; public RationalNumber(){ //Initializes to 0/1 num = 1; den = 1; } public RationalNumber(int num, int den){ //Initializes to n/d try{this.den=den; this.num=num; if(den==0){ throw new IllegalArgumentException("Zero value at constructor"); } }catch(IllegalArgumentException e){ System.out.println(e.getMessage()); } this.reduce(); } //public RationalNumber(RationalNumber x){//Initializes to x's values public void add(RationalNumber other){ // this = this + other this.den = this.den * other.den; this.num = this.num * other.den + other.num * this.den; this.reduce(); //toString(); } public void subtract(RationalNumber other){ // this = this - other this.den = this.den * other.den; this.num = this.num * other.den - other.num * this.den; this.reduce(); //toString (); } public void multiply(RationalNumber other){ // this = this * other this.den = this.den * other.den; this.num = this.num * other.num; this.reduce(); //toString (); } public void divide(RationalNumber other){ // this = this / other this.den = this.den * other.num; this.num = this.num * other.den; this.reduce(); //toString (); } private void reduce(){ int yes = 0; int smaller; int num1=this.num,den1=this.den; if(num