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!

Do you have a specific question or problem with your code?

I just want some IDEAS or maybe sniplets of code for my last method

This is the last method:
public static void printString

What help do you need with that???

Oh, sorry, when i said last method, i meant the last one before i finish the program. The method that I am stuck on is not printString; look back at my code, there should be 2 long comment lines, one saying "Heres where I am stuck" right on top of getValue() method

Please explain what your problem is in an easy way to see it. Hiding it in the code doesn't make it obvious.

Okay, my problem here is to have getValue() generate the appropriate math expression so that it can return the expression and store it into the array.
-The method receives a StringTokenizer which comes from the string that was entered by the user, so something like: * - 78 901 + 234 56
-Now i'm supposed to pull out each token, see if its a math expression (+-*/)
OR a value. Heres where I am confused because I dont know how to recursively call the method again so that it can place the expression between the numbers. The example above is extra confusing because 2 expressions comes before the numbers. So my question is: What do I do once I know the token read is an expression, and how do I place it in the correct order. Here is that part of the assignment:
"Instantiate the appropriate MathExpression (either AddExpression, SubExpression, MultExpression, DivExpression) recursively OR MathValue, then return it. If the StringTokenizer has no more tokens or there are no elements in the array, then return null. Optional: Also check if there should be a number and it isn’t, OR if there are too many numbers, then return null."
-Sorry if i wasnt being clear before.

supposed to pull out each token, see if its a math expression (+-*/)
OR a value

If there is more than one token it could be an expression.
If only one token it could be a value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.