Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class CalcHelper {
- private void appStart() {
- System.out.println("Press any key to continue...");
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- try {
- reader.readLine();
- }
- catch (IOException e) {
- e.getStackTrace();
- }
- }
- public void printInstructions() {
- System.out.println("Welcome to Simple Java Calculator.");
- try {
- Thread.sleep(1000);
- }
- // Exception handling
- catch (InterruptedException e) {
- e.getStackTrace();
- }
- appStart();
- System.out.println("Enter two numbers and the desired mathematical operation.");
- }
- // Reading doubles
- public double readDouble() {
- double input = 0;
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- // Read the input
- try{
- input = Double.parseDouble(reader.readLine());
- }
- // Handle exceptions
- catch (IOException e) {
- e.getStackTrace();
- }
- catch(NumberFormatException e) {
- System.out.println("Oops, something went wrong. Try again, please.");
- readDouble(); // Restart if things go wrong
- }
- return input;
- }
- // Reading strings
- public String readString() {
- String s = ""; // default operator is plus
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- try {
- s = reader.readLine();
- }
- // Handle exception
- catch (IOException e) {
- e.getStackTrace();
- }
- return s;
- }
- // Selecting operation
- public void operationSelect(String operator, double a, double b) {
- switch(operator) {
- // Summation
- case "+":
- {
- double result = a + b;
- System.out.println(a + " + " + b + " equals " + result);
- break;
- }
- // Subtraction
- case "-":
- {
- double result = a - b;
- System.out.println(a + " - " + b + " equals " + result);
- break;
- }
- // Division
- case "/":
- case ":": {
- double result = 0;
- try {
- result = a / b;
- }
- catch (ArithmeticException e) {
- e.getStackTrace();
- }
- System.out.println(a + " / " + b + " equals " + result);
- break;
- }
- // Multiplication
- case "*": {
- double result = a * b;
- System.out.println(a + " * " + b + " equals " + result);
- break;
- }
- // In case of the error
- default:
- System.out.println("Unknown operation");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement