...

View Full Version : objects in java



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;
}
}

cwl157
02-25-2005, 09:37 PM
ok so i looked more closely at the code i posted and realized a lot of stupid mistakes. So now i really have no idea where to start. I have a plan that is correct but i have no idea where to start the implementation. I have a program that does this but its all in one class and i have no clue how to break it up. Can someone please give me some direction? Thanks

cwl157
02-26-2005, 05:22 PM
I think what im really confused about is building the constructors. I completed the first class which is called number all except for the constructor. What do i put in the constructor to make it create all those methods or whatever? Here is the number class without a constructor can someone help me built a contructor for it?


import java.io.*;
import java.util.*;

public class Number
{

//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 = Number.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 int performOperation(int num1, int num2, String 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;
}
}
}

cwl157
02-26-2005, 09:57 PM
o so now i think i have all the objects written. However, when i run it in NetBeans IDE it compiles and runs fine. But if i try to run it in the command line it gives a nosuchelement exception. Does anyone know why it runs ok in netbeans but not in the command line? Here are the classes

Number class


import java.io.*;
import java.util.*;

public class Number
{
Number()
{
}

//gets a single input from the user, and returns it as a String
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 "";
}
}

int getValue(String result)
{
Operator o = new Operator();
int num = 0;
String check = "";
StringTokenizer tok = new StringTokenizer(this.getInput());
do
{
check = tok.nextToken();
if(!check.equals("+") || !check.equals("-") ||!check.equals("*") || !check.equals("/") || !check.equals("%"))
num = Integer.parseInt(check);
else
o.getOp(result);
}while(tok.hasMoreTokens());

return num;
}


//Uses the StringTokenizer to break up the numeric experession and sends the tokens to the performOperation method
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 = Number.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 int performOperation(int num1, int num2, String 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;
}
}
}


Operator class


import java.io.*;
import java.util.*;

public class Operator
{
Number t = new Number();
String result = t.getInput();

Operator()
{
this.getOp(result);
}

char getOp(String result)
{
char op = '+';
String check = "";
StringTokenizer tok = new StringTokenizer(t.getInput());
do
{
check = tok.nextToken();
if(check.equals("+"))
op = '+';
if(check.equals("-"))
op = '-';
if(check.equals("*"))
op = '*';
if(check.equals("/"))
op = '/';
if(check.equals("%"))
op = '%';
}while(tok.hasMoreTokens());

return op;
}
}


CalculatorOO class with main method


import java.io.*;
import java.util.*;

public class CalculatorOO
{
public static void main(String args[])
{
String cont = "";
Number n = new Number();
StringTokenizer result = new StringTokenizer(n.getInput());
Operator op = new Operator();

do
{
String exp = n.getInput();
System.out.println(op.getOp(exp));
System.out.println(n.getValue(exp));
}while(result.hasMoreTokens());
do
{
System.out.println("Please enter a numeric expression: ");
System.out.print("Do you want to enter another expression (y/n): ");
cont = n.getInput();
}while(cont.equalsIgnoreCase("y"));
System.out.println("Goodbye");
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum