cwl157
02-25-2005, 09:28 PM
I need to make a calculator in java but in an object oriented way. In the end it is supposed to take a user inputted expression as a string and solve the expression just left to right ignoring any order of operations. I am trying to make the first object which is a number object. I get 2 compiling errors one says identifier expected in line 6 and the other says im missing a curling brace on line 82 ( the end of the class). Can someone help me out and tell me how i am constucting it wrong. And also if you can tell me if this will work when i implement it in another class. I really have little idea of what im doing.
Here is the code:
import java.util.*;
import java.io.*;
public class Number
{
int numExp = Number.getInput();
Number(int numExp)
{
this.doOperation(Exper);
}
//gets a single input from the user, and returns it as a String
static String getInput()
{
java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
java.io.BufferedReader console = new java.io.BufferedReader(input);
try
{
return console.readLine();
}
catch(java.io.IOException e)
{
System.err.println("Bad NEWS!!!");
return "";
}
}
//Uses the StringTokenizer to break up the numeric experession and sends the tokens to the performOperation method
static int doOperation(String Exper)
{
int result=0; int num =0; String operation ="";
StringTokenizer numEx = new StringTokenizer(Exper, "+-/%*", true);
result = Integer.parseInt(numEx.nextToken());
if(!numEx.hasMoreTokens())
{
return result;
}
do
{
operation = numEx.nextToken();
num = Integer.parseInt(numEx.nextToken());
result = Calculator1.performOperation(result, num, operation);
}while(numEx.hasMoreTokens());
return result;
}
//performs the operation op on num1 and num2 returning the result as an int to result int the doOperation class
static Number performOperation(Number num1, Number Exper, Operator op)
{
if(op.equals("+"))
{
return num1 + num2;
}
else if(op.equals("-"))
{
return num1 - num2;
}
else if(op.equals("*"))
{
return num1 * num2;
}
else if(op.equals("/"))
{
return num1 / num2;
}
else if(op.equals("%"))
{
return num1 % num2;
}
else
{
System.err.println("Not an operation: " + op);
return -1;
}
}
static int getValue(number a)
{
int result = (int) a;
return result;
}
}
Here is the code:
import java.util.*;
import java.io.*;
public class Number
{
int numExp = Number.getInput();
Number(int numExp)
{
this.doOperation(Exper);
}
//gets a single input from the user, and returns it as a String
static String getInput()
{
java.io.InputStreamReader input = new java.io.InputStreamReader(System.in);
java.io.BufferedReader console = new java.io.BufferedReader(input);
try
{
return console.readLine();
}
catch(java.io.IOException e)
{
System.err.println("Bad NEWS!!!");
return "";
}
}
//Uses the StringTokenizer to break up the numeric experession and sends the tokens to the performOperation method
static int doOperation(String Exper)
{
int result=0; int num =0; String operation ="";
StringTokenizer numEx = new StringTokenizer(Exper, "+-/%*", true);
result = Integer.parseInt(numEx.nextToken());
if(!numEx.hasMoreTokens())
{
return result;
}
do
{
operation = numEx.nextToken();
num = Integer.parseInt(numEx.nextToken());
result = Calculator1.performOperation(result, num, operation);
}while(numEx.hasMoreTokens());
return result;
}
//performs the operation op on num1 and num2 returning the result as an int to result int the doOperation class
static Number performOperation(Number num1, Number Exper, Operator op)
{
if(op.equals("+"))
{
return num1 + num2;
}
else if(op.equals("-"))
{
return num1 - num2;
}
else if(op.equals("*"))
{
return num1 * num2;
}
else if(op.equals("/"))
{
return num1 / num2;
}
else if(op.equals("%"))
{
return num1 % num2;
}
else
{
System.err.println("Not an operation: " + op);
return -1;
}
}
static int getValue(number a)
{
int result = (int) a;
return result;
}
}