Commit 8f404730 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

Initial commit

parents
/.project
/.settings
/bin
/.classpath
/sum
2
3
5
10
\ No newline at end of file
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();
}
this.mNumerator = num;
this.mDenominator = den;
}
public long getNumerator() {
return this.mNumerator;
}
public long getDenominator() {
return this.mDenominator;
}
public long calculate() {
return this.mNumerator/this.mDenominator;
}
@Override
public void close() throws Exception {
System.out.println("Nothing to close... :)");
}
}
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();
}
}
}
}
package java_exercises4_1;
public class ZeroDenominatorException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String getMessage() {
return "Haha.. Division by 0";
}
}
package java_exercises4_2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class MoneyDemo {
private static final String inputFileName = "money";
private static final String outputFileName = "sum";
public static void main(String[] args) {
try (
BufferedReader in = new BufferedReader(Files.newBufferedReader(Paths.get(inputFileName), Charset.forName("UTF-8")));
PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(outputFileName), Charset.forName("UTF-8"), StandardOpenOption.CREATE))
) {
double sum = 0;
String input;
while((input = in.readLine()) != null) {
try {
sum += Double.parseDouble(input);
} catch(NumberFormatException e) {
System.out.println("NumberFormatException: For input string: " + input);
continue;
}
}
out.write("sum = " + sum);
System.out.println("Sum = " + sum);
} catch (IOException e) {
e.printStackTrace();
}
}
}
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