In: Computer Science
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator = 0; System.out.println(); try { Rational rad = new Rational(-1, 3); if (denominator == 0) throw new ZeroDenominatorException(); else { System.out.println(rad); } } catch(ZeroDenominatorException e){ System.err.println(e.getMessage()); } } }
import java.util.Objects; public class Rational { private int numerator; private int denominator; public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public int getNumerator() { return numerator; } public void setNumerator(int numerator) { this.numerator = numerator; } public int getDenominator() { return denominator; } public void setDenominator(int denominator) { this.denominator = denominator; } @Override public String toString() { boolean negative = (numerator < 0 || denominator < 0) && (numerator >= 0 || denominator > 0); int num = numerator, denum = denominator; if (num < 0) num = -num; if (denum < 0) denum = -denum; if (negative) return "-" + num + "/" + denum; else return num + "/" + denum; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Rational rational = (Rational) o; return numerator == rational.numerator && denominator == rational.denominator; } @Override public int hashCode() { return Objects.hash(numerator, denominator); } }
public class ZeroDenominatorException extends Exception { public ZeroDenominatorException(){ super("The denominator cannot be zero!"); } }
/**
* The main class is the driver for my Rational project.
*/
public class Main {
/**
* Main method is the entry point to my code.
* @param args The command line arguments.
*/
public static void main(String[] args) {
int numerator=5;
int denominator = 0;
System.out.println();
try {
Rational rad = new
Rational(numerator,denominator);
System.out.println(rad);
}
catch(ZeroDenominatorException e){
System.err.println(e.getMessage());
}
}
}
import java.util.Objects;
public class Rational {
private int numerator;
private int denominator;
public Rational(int numerator, int denominator) throws
ZeroDenominatorException {
this.numerator = numerator;
this.setDenominator(denominator);
}
public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) throws
ZeroDenominatorException {
if(denominator==0){
throw new
ZeroDenominatorException();
}
else{
this.denominator = denominator;
}
}
@Override
public String toString() {
boolean negative = (numerator < 0 || denominator < 0)
&& (numerator >= 0 || denominator > 0);
int num = numerator, denum = denominator;
if (num < 0)
num = -num;
if (denum < 0)
denum = -denum;
if (negative)
return "-" + num + "/" + denum;
else
return num + "/" + denum;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rational rational = (Rational) o;
return numerator == rational.numerator &&
denominator == rational.denominator;
}
@Override
public int hashCode() {
return Objects.hash(numerator, denominator);
}
}
public class ZeroDenominatorException extends Exception {
public ZeroDenominatorException(){
super("The denominator cannot be zero!");
}
}
Expected output:
when numerator=5 and denominator =0
The denominator cannot be zero!
when numerator=5 and denominator=2
5/2