Skip to content

Instantly share code, notes, and snippets.

@KatrinaHoffert
Created January 11, 2014 23:16
Show Gist options
  • Save KatrinaHoffert/8378280 to your computer and use it in GitHub Desktop.
Save KatrinaHoffert/8378280 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class Calculator
{
private double firstNumber = 0;
private double secondNumber = 0;
private String operation = null;
private String expression = null;
public double stepByStep() throws NumberFormatException
{
double answer = 0;
Scanner userScanner = new Scanner(System.in);
System.out.println("Please enter your first number");
firstNumber = userScanner.nextDouble();
System.out.println("Please enter your second number");
secondNumber = userScanner.nextDouble();
System.out.println("Please enter an operation");
operation = userScanner.next();
switch(operation)
{
case "+":
answer = (firstNumber + secondNumber);
break;
case "-":
answer = (firstNumber - secondNumber);
break;
case "/":
answer = (firstNumber / secondNumber);
break;
case "*":
answer = (firstNumber * secondNumber);
break;
case "x":
answer = (firstNumber * secondNumber);
}
userScanner.close();
return answer;
}
public double expression() throws NumberFormatException
{
double answer = 0;
Scanner userScanner = new Scanner(System.in);
System.out.println("Please enter your expression");
expression = userScanner.nextLine();
if(expression.contains("/"))
{
String[] sarr = expression.split("/");
firstNumber = Integer.parseInt(sarr[0]);
secondNumber = Integer.parseInt(sarr[1]);
answer = firstNumber / secondNumber;
}
else if(expression.contains("-"))
{
String[] sarr = expression.split("-");
firstNumber = Integer.parseInt(sarr[0]);
secondNumber = Integer.parseInt(sarr[1]);
answer = firstNumber - secondNumber;
}
else if(expression.contains("*"))
{
String[] sarr = expression.split("\\*");
firstNumber = Integer.parseInt(sarr[0]);
secondNumber = Integer.parseInt(sarr[1]);
answer = firstNumber * secondNumber;
}
else if(expression.contains("+"))
{
String[] sarr = expression.split("\\+");
firstNumber = Integer.parseInt(sarr[0]);
secondNumber = Integer.parseInt(sarr[1]);
answer = firstNumber + secondNumber;
}
userScanner.close();
return answer;
}
public static void main(String[] args)
{
try
{
System.out.println("Hello what would you like to do?");
System.out.println("1)Use basic calculator (Step by step)");
System.out.println("2)Use basic calculator (Enter an expression)");
int userChoice;
double answer = 0;
Scanner userScanner = new Scanner(System.in);
userChoice = userScanner.nextInt();
Calculator calc = new Calculator();
if(userChoice == 1)
{
answer = calc.stepByStep();
}
else if(userChoice == 2)
{
answer = calc.expression();
}
userScanner.close();
System.out.println("Answer is " + answer);
}
catch(NumberFormatException e)
{
System.out.println("That is not valid, please restart and enter something valid.");
System.exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment