Commit 6e6407e4 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

Improved example

added FractionParsingException which is thrown if string is not parsable
added ctor with fraction string
added several examples of what could happen
parent 8f404730
package java_exercises4_1;
public class Fraction implements AutoCloseable {
private long mNumerator;
private long mDenominator;
public Fraction(long num, long den) throws ZeroDenominatorException {
if (den == 0) {
throw new ZeroDenominatorException();
public class Fraction {
private long numerator;
private long denominator;
/**
* Ctor with a fraction String.
*
* @param fraction
* @throws FractionParsingException
*/
public Fraction(String fraction) throws FractionParsingException {
try {
String split[] = fraction.split("/");
numerator = Long.parseLong(split[0]);
denominator = Long.parseLong(split[1]);
} catch (Exception e) {
throw new FractionParsingException(
"Can't parse fraction string: " + fraction + ", reason: " + e.getMessage());
}
this.mNumerator = num;
this.mDenominator = den;
}
/**
* Ctor with numerator and denominator provided seperately.
*
* @param numerator
* @param denominator
* @throws ZeroDenominatorException
*/
public Fraction(long numerator, long denominator) throws ZeroDenominatorException {
if (denominator == 0) {
throw new ZeroDenominatorException("Denominator of fraction is 0");
}
this.numerator = numerator;
this.denominator = denominator;
}
public long getNumerator() {
return this.mNumerator;
return this.numerator;
}
public long getDenominator() {
return this.mDenominator;
return this.denominator;
}
public long calculate() {
return this.mNumerator/this.mDenominator;
/**
* Calculates the fraction.
*
* @return
*/
public double calculate() {
return this.numerator / this.denominator;
}
@Override
public void close() throws Exception {
System.out.println("Nothing to close... :)");
public String toString() {
return "(" + numerator + "/" + denominator + ")";
}
}
package java_exercises4_1;
/**
* This is a user-defined exception class which inherits from class
* {@link Exception}.
*
* @author mad
*
*/
public class FractionParsingException extends Exception {
private static final long serialVersionUID = 1L;
/**
* Constructor with error message delegates to super ctor to initialize member
* variable of super ctor.
*
* @param message
*/
public FractionParsingException(String message) {
super(message);
}
}
package java_exercises4_1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ZeroDenominatorDemo {
public static void main(String[] args) {
long num, den;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(;;) {
try {
System.out.println("Please enter a Numerator: ");
num = Long.parseLong(br.readLine());
System.out.println("Please enter a Denuminator: ");
den = Long.parseLong(br.readLine());
try ( Fraction f = new Fraction(num,den))
{
System.out.printf("The result of the calculation %d : %d is %d \n",
f.getNumerator(), f.getDenominator(), f.calculate());
} catch (ZeroDenominatorException e) {
e.printStackTrace();
break;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
public static void main(String[] args) throws FractionParsingException {
// Example of ZeroDenominatorException and calculate
try {
Fraction f = new Fraction(2, 3);
System.out.println("Calculated result of fraction " + f + ": " + f.calculate());
Fraction f1 = new Fraction(3, 2);
System.out.println("Calculated result of fraction " + f1 + ": " + f1.calculate());
Fraction f2 = new Fraction(4, 0);
// won't be reached in case of an exception.
System.out.println("Calculated result of fraction " + f2 + ": " + f2.calculate());
} catch (ZeroDenominatorException e) {
// TODO Auto-generated catch block
System.out.println("Exception: " + e.getMessage());
// e.printStackTrace();
}
// Example of FractionParsingException
try {
Fraction f = new Fraction("2/3");
Fraction f1 = new Fraction("2/kl");
} catch (FractionParsingException e) {
// TODO Auto-generated catch block
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}
// If the exception is not caught it will be delegated to the caller (in this
// case the OS)
Fraction f = new Fraction("6,3");
// Not reached any more
Fraction f1 = new Fraction("5/4");
}
}
package java_exercises4_1;
/**
* This is a user-defined exception class which inherits from class
* {@link Exception}.
*
* @author mad
*
*/
public class ZeroDenominatorException extends Exception {
private static final long serialVersionUID = 1L;
/**
*
* Empty constructor delegates to super ctor with a predefined error message.
*/
private static final long serialVersionUID = 1L;
public ZeroDenominatorException() {
super("Division by 0");
}
@Override
public String getMessage() {
return "Haha.. Division by 0";
/**
* Constructor with error message delegates to super ctor to initialize member
* variable of super ctor.
*
* @param message
*/
public ZeroDenominatorException(String message) {
super(message);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment