Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Calculator (Please Help)

This my first time doing java. I provide some code, but I don't think that it close to being right. Can somebody please help me. Thanks

Write a calculator class that accepts equations as input, e.g.
1 + 6 * 9 12 / 4 =
Rules:
- an equation is entered as in the example above
- the operators +, -, *, /, and = are to be known by the calculator
- all operands are numbers of type double
- operators and operands are entered separated by spaces
- the equals sign ends the input; pressing return causes the result to be displayed
- calculations are to be done following the rules of operator precedence
- input can be as long as desired as long as it follows the above rules

The program will consist of two parts: a class for the calculator with all methods and data needed to solve the problem, and a main program that uses the calculator class. Your program must follow the OOP paradigm and must implement (several) methods sensibly.

public class Calculator {
public static void main(String[] args)
{
double result;
do {
System.out.println ("Enter 1st number: "); int number1 = Keyboard.readInt ();
System.out.println ("Enter operator (+, -, * or /): ");
char op = Keyboard.readChar ();
System.out.println ("Enter 2nd number: ");
int number2 = Keyboard.readInt (); result = makeCalc (number1, number2, op);
}
while (result == Double.NaN);
System.out.println ("The result is " + (int) result);
}
private static double makeCalc (int number1, int number2, char op) {
double result;
switch (op)
{
case '+':
result = number1 + number2;
break;

case '-':
result = number1 - number2;
break;

case '*':
result = number1 * number2;
break;

case '/':
result = division(number1, number2);
break;
default:

result = Double.NaN;
break;
}
return result;
}
private static double division (int number1, int number2) {
return (number2 == 0) ?
Double.NaN : number1 / number2;
}private static int division(int number1, int number2){
if(number2 == 0) //testing division
{
System.out.println("Error you can not divide by zero");
return 0;
}
else return(number1/number2);
}}
[2405 byte] By [Tmlucky14] at [2007-11-11 9:51:43]