Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2013 16:59
Show Gist options
  • Save anonymous/4967693 to your computer and use it in GitHub Desktop.
Save anonymous/4967693 to your computer and use it in GitHub Desktop.
import acm.program.*;
public class Calculator4 extends ConsoleProgram {
private double first;
private double second;
private double result;
public void run() {
while (result <= 1337) {
print("First operand: ");
first = readDouble();
print("Second operand: ");
second = readDouble();
print("Operator: ");
String operator = readLine();
performOperation(operator);
println("The result of " + first
+ operator + second + " is "+ result);
}
println("The result was over 1337, so I stopped!");
}
}
private void performOperation(String operator) {
if (operator.equals("+")) {
result = first + second;
} else if (operator.equals("-")) {
result = first - second;
} else if (operator.equals("/")) {
result = first / second;
} else if (operator.equals("*")) {
result = first * second;
} else {
result = 0;
}
}
import acm.program.ConsoleProgram;
public class hei extends ConsoleProgram{
public void run(){
double first=readDouble("First Operand: ");
double second= readDouble("Second Operand: ");
String operator= readLine("Operator: ");
char third= operator.charAt(0);
double answer=
calculatorOperator(first,second,third);
print("the result of "+first+" "+third+" “
+second+" = "+answer );
}
}
}
public double calculatorOperator(double first,double
second, char third){
double answer = 0;
switch(third){
case '+': answer=first + second;
break;
case '/': answer=first/second;
break;
case '*': answer= first*second;
break;
case '-': answer= first-second;
break;
default: print("u fucked up");
break;
}
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment