So far, in working with number, we have worked with ints and doubles where we ha
ID: 3682521 • Letter: S
Question
So far, in working with number, we have worked with ints and doubles where we have learned that doubles are inherently inexact. Since we know a rational number can be represented as a pair of integers, our goal is to indicate an object Rational that we can Heat with the same "trust" as int but allows more precision. Download the Num interface Num. Write a class Int that extends Num. This class should base a single nuance variable It should also override definitions for the follow mg methods: In addition, you should hac a constructor and a method get Nil that returns the exact value of the Int as a Java primitive (Int). Write a java class Rational that has two instance variables: This class should override definitions for the same 3 methods as above In addition it should have a constructor that takes two Ints as the following methods (to replace getVal in Int): Finally, write a class InvalidDenomException that extends RuntimelException Modify the constructor in Rational to throw an InvalidDenomiException if anyone tries to set the denominator to zero. Finally: Submit Int, java, Rational java. and InvalidDenomException Java to the drophox Remember that your source code MUST have appropriate headers and MUST contain appropriate comments.Explanation / Answer
file Int.java
public class Int implements Num {
private int number;
public Int(int i){
this.number = i;
}
@Override
public int getApproxVal(){
return Integer.parseInt(this.toString());
}
@Override
public int compare(int i){
if (this.number > i)
return 1;
else
if (this.number < i)
return -1;
return 0;
}
public String toString(){
return String.valueOf(number);
}
public int getVal(){
return this.number;
}
}
interface Num{
/**
* A method that allows getting the approximate value of the
* internal number as a primitive.
*
* Note that we cannot have an equivalent method 'getVal' for
* all number objects, since we may not always be able to retrieve
* the exact result as a Java primitive.
*
* @return The approximate value of this Number as a int.
*/
public int getApproxVal();
/**
* A method that allows a total ordering between number objects.
* @param i The num to compare to.
* @return A negative int if this Number is 'less than' that Number
*/
public int compare(int i);
/**
* A method that allows string converts to an integer.
* @return String of the Number
*/
public String toString();
}
file InvalidDenomException.java
public class InvalidDenomException extends RuntimeException {
public InvalidDenomException(String message){
super(message);
}
}
file Rational.java
public class Rational {
private int numer, denom;
public Rational(){
numer = 0;
denom = 0;
}
public Rational(int numer, int denom) {
if (denom == 0)
throw new InvalidDenomException("Denominator cannot be zero.");
this.numer = numer;
this.denom = denom;
}
public int getNumer(){
return this.numer;
}
public int getDenom(){
return this.denom;
}
public double getApproxVal(){
if (this.denom == 0)
return 0;
else
if (this.denom == 1)
return this.getNumer();
return this.getNumer() / this.getDenom();
}
public int compare(Rational b){
Rational a = this;
int lhs = a.getNumer() * b.getDenom();
int rhs = a.getDenom() * b.getNumer();
if (lhs < rhs) return -1;
if (lhs > rhs) return 1;
return 0;
}
public String toString(){
if (this.denom == 1) return this.numer + "";
return this.numer+"/"+this.denom;
}
public boolean lessThan(Int i){
Int val;
if (this.denom == 0)
val = new Int(0);
else
if (this.denom == 1)
val = new Int(this.getNumer());
else
val = new Int(this.getNumer()/this.getDenom());
if (val.compare(i.getVal()) == -1)
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.