How to loop this program??
ok guys i need a little help here, i have made this program that inputs a fraction and give the output in its simplest forms, the only problem im having is that i nee to edit this coding to that if the user inserts a char, or 0 in the denominator, so the program should give an error message and return to the start until the user wants to exit the program...
here is my coding,
Code:
public class Fraction {
private int numer = 0;
private int denom = 0;
public Fraction(int numer, int denom) {
if (denom == 0)
throw new NumberFormatException("denominator is zero");
this.numer = numer;
this.denom = denom;
this.reduce();
}
public int getNumerator() {
return this.numer;
}
public int getDenominator() {
return this.denom;
}
public void reduce()
{
int d;
//while (true)
do{
d = this.gcd(this.numer, this.denom);
if (d == 1)
return;
this.numer /= d;
this.denom /= d;
}while(true);
}
private int gcd(int a, int b) {
int t;
while (b != 0) {
t = a;
a = b;
b = t%a;
}
return (a);
}
public String output() {
return ( this.numer + "/" + this.denom );
}
public double doubleValue() {
return (1.0*this.numer/this.denom);
}
}