Hey guys, I am taking my first quarter of java at college and i'm having some difficulties with just 1 method for my current assignment. I wasn't planning on getting help, but I've been stuck on this for hours now, so i've decided to go to the pros.
Here's what the program is supposed to do:
-User enter how many times they want to enter math expressions WITH numbers
-An array of objects are created, and the program calculates the answer based on input
Example of input would be:
Enter the amount of Math expressions: 2
Enter a Math expression: - 123.456 7.8
Enter a Math expression: * - 78 901 + 234 56
Result : (123.456 - 7.8) = 115.656
Result : ((78.0 - 901.0) * (234.0 + 56.0)) = -238670.0
*********I have most of the program finished, and i'm not asking for help with building the WHOLE program. I just want some IDEAS or maybe sniplets of code for my last method******** Here is my code
abstract public class MathExpression {
public abstract double evaluate();
}//end MathExpression
class MathValue extends MathExpression{
public double doubValue;
public MathValue(double dNum){
doubValue = dNum;
}
public double evaluate(){
return doubValue;
}
public String toString(){
return ("" + doubValue);
}
}//end MathValue
abstract class MathBinaryExpression extends MathExpression{
protected MathExpression leftExpression, rightExpression;
public MathBinaryExpression(MathExpression left, MathExpression right){
if(left != null){
leftExpression = left;
}else{
leftExpression = new MathValue(0);
}
if(right != null){
rightExpression = right;
}else{
rightExpression = new MathValue(0);
}
}//end constructor
public MathExpression getLeft(){
return leftExpression;
}
public MathExpression getRight(){
return rightExpression;
}
}//end MathBinaryExpression
class AddExpression extends MathBinaryExpression{
public AddExpression(MathExpression left, MathExpression right){
super(left,right);
}//end constructor
public double evaluate(){
return leftExpression.evaluate() + rightExpression.evaluate();
}
public String toString(){
return ("(" + leftExpression + " + " + rightExpression + ")");
}//end toString
}//end AddExpression
class SubExpression extends MathBinaryExpression{
public SubExpression(MathExpression left, MathExpression right){
super(left,right);
}//end constructor
public double evaluate(){
return leftExpression.evaluate() - rightExpression.evaluate();
}
public String toString(){
return ("(" + leftExpression + " - " + rightExpression + ")");
}//end toString
}//end AddExpression
class MultExpression extends MathBinaryExpression{
public MultExpression(MathExpression left, MathExpression right){
super(left,right);
}//end constructor
public double evaluate(){
return leftExpression.evaluate() * rightExpression.evaluate();
}
public String toString(){
return ("(" + leftExpression + " * " + rightExpression + ")");
}//end toString
}//end AddExpression
class DivExpression extends MathBinaryExpression{
public DivExpression(MathExpression left, MathExpression right){
super(left,right);
}//end constructor
public double evaluate(){
return leftExpression.evaluate() / rightExpression.evaluate();
}
public String toString(){
return ("(" + leftExpression + " / " + rightExpression + ")");
}//end toString
}//end AddExpression
import java.util.Scanner;
import java.util.StringTokenizer;
public class Program5 {
public static void main(String[] args){
//getMathExpresions() is the test method, getValue() is supposed to be there
MathExpression[] mathArray = getMathExpressions();
for(int i = 0; i < mathArray.length; i++){
if(mathArray[i] != null){
printString(mathArray[i]);
}else{
System.out.println("Invalid Expression");
}
}//end display
int count = countBinary(mathArray);
System.out.println("The count is: " + count);
}
//This is a test method to see if the rest of the program works.
public static MathExpression [] getMathExpressions(){
MathExpression [] mathExpArray = new MathExpression[] {
new SubExpression(new MathValue(123.456),
new MathValue(7.8)),
new DivExpression( new MathValue(9.8),
new AddExpression( new MathValue(12.3),
new MathValue(7.6))),
new MathValue(1928.3746),
new AddExpression(new MathValue(3.0),
new MathValue(2.0)),
new MultExpression(new SubExpression(new MathValue(78.),
new MathValue(901.)),
new AddExpression(new MathValue(234.),
new MathValue(56.)))
};
return mathExpArray;
}
public static MathExpression[] getInput(){
Scanner input = new Scanner(System.in);
MathExpression[] myArray;
System.out.println("Enter the amount of Math Expressions: ");
int num = input.nextInt();
if(num <= 0){
myArray = new MathExpression[1];
}else{
myArray = new MathExpression[num];
}//end memory allocation
for(int i = 0; i < myArray.length; i++){
System.out.print("Enter prefix notation expression: ");
String in = input.nextLine();
StringTokenizer tokens = new StringTokenizer(in);
myArray[i] = getValue(tokens);
}
return myArray;
}//end getInput
///////////////////////////////////////////////////////////////
///////Here is where I am stuck.//////////
public static MathExpression getValue(StringTokenizer toks){
if(toks.hasMoreTokens() != true){
return null;
}else{
String temp = "";
String expressions = "+-*/";
temp = toks.nextToken();
if(temp.length() == 1 && expressions.indexOf(temp) >= 0){
switch((char)temp){
case '-' : new SubExpression(getValue(toks), getValue(toks));
case '+' : new AddExpression(getValue(toks), getValue(toks));
case '*' : new MultExpression(getValue(toks), getValue(toks));
case '/' : new DivExpression(getValue(toks), getValue(toks));
}
}
}
}
public static int countBinary(MathExpression[] myArr){
int count = 0;
for(int i = 0; i < myArr.length; i++){
if(myArr[i] instanceof MathBinaryExpression)
if(((MathBinaryExpression)myArr[i]).getLeft() instanceof MathValue && ((MathBinaryExpression)myArr[i]).getRight() instanceof MathValue)
count++;
}//end forloop
return count;
}
public static void printString(MathExpression item){
System.out.println(item.toString() + " = " + item.evaluate());
}
}
-The design is like this because its how the assignment is specified. Also, i've had no experience with recursions, and the method that i'm stuck on deals mainly with it.
Thanks in advance!