my task is Design and implement an abstract base class ArithmeticExpression that represent any
binary (having two arguments) arithmetic expression. The abstract class should include at least
two methods called evaluate and display....


i have done the code but it doesnt output anything.. im really stuck.. does anyone know why? please help. urgent

/**
 * Class representing a binary Arithmetic Expression.
 *
 */
public abstract class ArithmeticExpression {

	//
	// Arithmetic expression given as first operand
	protected ArithmeticExpression ae1;
	//
	// Arithmetic expression given as second operand
	protected ArithmeticExpression ae2;
	//
	// First operand
	protected double operand1;
	//
	// Second operand
	protected double operand2;

	/**
	 * Evaluates the arithmetic expression that the object represents.
	 * @return result of the expression.
	 */
	public abstract double evaluate();
	
	/**
	 * Returns the raw expression
	 */
	public void display() {
		System.out.println(toString());
	}

	@Override
	public String toString() {
		String result = "(" ;
		if (ae1 != null) {
			//
			// First operand is an expression!
			result += ae1;
		} else {
			// Normal operand
			result += operand1;
		}
		// Form the complete string.
		result += ")" + getSymbol() + "("; 
		if (ae2 != null) {
			//
			// Second operand is an expression!
			result += ae2;
		} else {
			// Normal operand
			result += operand2;
		}
		
		result += ")";
		return result;
	}

	/**
	 * Get the operation symbol.
	 * @return symbol
	 */
	public abstract String getSymbol();

}

Edited 5 Years Ago by Narue: Added code tags

It doesn't do anything because its an abstract class - you can't instantiate it. Also there is no "main" method to start the execution.
To test this you need to create a second, non-abstract, class that extends ArithmeticExpression and provides full implementations for the inherited abstract methods. Then you can have a main method that creates an instance of that class and calls its methods to do <whatever it's supposed to do> and print the result.

i have done non a bstract classes

e.g.

/**
 *
 * This class Represents the addition operation.
 */
public class Addition extends ArithmeticExpression {

	/**
	 * Constructor - to be used when both normal operands are present 
	 * @param operand1 First operand
	 * @param operand2 Second operand
	 */
	public Addition(double operand1, double operand2) {
		this (null, operand1, null, operand2);
	}

	/**
	 * Constructor - to be used when first operand is in the form of arithmetic expression.
	 * @param operand1 First operand
	 * @param operand2 Second operand
	 */
	public Addition(ArithmeticExpression exp, double operand2) {
		//
		// Call the other constructor.
		this (exp, exp.evaluate(), null, operand2);
	}

	/**
	 * Constructor - to be used when second operand is in the form of arithmetic expression.
	 * @param operand1 First operand
	 * @param operand2 Second operand
	 */
	public Addition(double operand1, ArithmeticExpression exp) {
		//
		// Call the other constructor.
		this (null, operand1, exp, exp.evaluate());
	}

	/**
	 * Constructor - to be used when both first and second operands are in the form of arithmetic expression.
	 * @param operand1 First operand
	 * @param operand2 Second operand
	 */
	public Addition(ArithmeticExpression exp1, ArithmeticExpression exp2) {
		//
		// Call the other constructor.
		this (exp1, exp1.evaluate(), exp2, exp2.evaluate());
	}

	/**
	 * Constructor
	 * @param exp1 Arithmetic expression for first operand
	 * @param operand1 first operand
	 * @param exp2  Arithmetic expression for second operand
	 * @param operand2 second operand
	 */
	public Addition(ArithmeticExpression exp1, double operand1, ArithmeticExpression exp2, double operand2) {
		this.ae1 = exp1;
		this.ae2 = exp2;
		this.operand1 = operand1;
		this.operand2 = operand2;
	}

	@Override
	public double evaluate() {
		return operand1 + operand2;
	}

	@Override
	public String getSymbol() {
		return "+";
	}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Try, in a non abstract class then...

return result.toString();

Just to test I would have tried:

public void display() {		
   toString();
   System.out.println(result);
}

Hope this helps...

Edited 5 Years Ago by StephNicolaou: n/a

That looks like a reasonable version of a non-abstract class. Now what you need to do is to write a main method that creates an instance of Addition and calls its display method.

the thing is i cant seem to get an output for my code.. i dont understand why.. and how do i write a main method?

Go back to your very first Java program (probably "hello world"?). Every Java program must have a method

public static void main(String[] args) {
   // do something
}

When you try to run your program Java calls that method, which is how things get started. In your main method you can create an instance of Addition and call its display method.

when i put that in there is an error

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        
/**
 * Class representing a binary Arithmetic Expression.
 *
 */
        
public abstract class ArithmeticExpression {

	//
	// Arithmetic expression given as first operand
	protected ArithmeticExpression ae1;
	//
	// Arithmetic expression given as second operand
	protected ArithmeticExpression ae2;
	//
	// First operand
	protected double operand1;
	//
	// Second operand
	protected double operand2;

	/**
	 * Evaluates the arithmetic expression that the object represents.
	 * @return result of the expression.
	 */
	public abstract double evaluate();
	
	/**
	 * Returns the raw expression
	 */
	public void display() {
		System.out.println(toString());
	}

	@Override
	public String toString() {
		String result = "(" ;
		if (ae1 != null) {
			//
			// First operand is an expression!
			result += ae1;
		} else {
			// Normal operand
			result += operand1;
		}
		// Form the complete string.
		result += ")" + getSymbol() + "("; 
		if (ae2 != null) {
			//
			// Second operand is an expression!
			result += ae2;
		} else {
			// Normal operand
			result += operand2;
		}
		
		result += ")";
		return result;
	}

	/**
	 * Get the operation symbol.
	 * @return symbol
	 */
	public abstract String getSymbol();
    }

}

this line of code public abstract class ArithmeticExpression becomes underlined with red a line and the error says illegal start of expression

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

That's because you didn't have a } at the end of you main method.
But anyway, you can't have more than one public class in one .java file because the file name and the public class name must both be the same. You need to either split your code into 3 files, or make all classes except Main not public.

Because you create some test data in your main method - eg create an instance of Addition with values 2 and 3. When you call its display method you should see something like "2+3=5". If you see that, your code works If you see anything else, or you get an error message, you have a bug. .

so i split the code in different files within the same project and then run it
i have done the addtion as u have seem...

Yes. Just try it, you don't need to ask first. If you have a backup copy of your files there's NOTHING you can do that will cause permanent harm!
Anyway, I'm finishing now for the night - see you tomorrow.

i sitll cant rid of the error we spoke about before... } where does that go exactly???
it sill comes up with an error

do u know if there is a simple way of doing this? Design and implement an abstract base class ArithmeticExpression that represent any
binary (having two arguments) arithmetic expression. The abstract class should include at least
two methods called evaluate and display

What you have been doing is the simple way. Nobody said that real-world programming is easy.
Both times you quoted the requirement it was just to design and implement an abstract class. You had done that by the time you posted your first post. Since then you have been moving on to how to use and test an abstract class. Is that something you are supposed to be doing yet? Maybe you will have some more lessons before having to do that? Anyway, last time I looked you were 90% of the way there, with only a missing } as the current problem. Where are you up to now?

Who told you that? Apart from the two kinds of operand* I don't see how it can be simpler without omitting parts of the spec.

* this can be simplified if you define a simple numeric value as a subclass of ArithmeticExpression, but it's a pretty minor point.

// Arithmetic expression given as first operand
protected ArithmeticExpression ae1;
...
// First operand
protected double operand1;

my teacher.. he said read the task again...

Design and implement an abstract base class ArithmeticExpression that represent any
binary (having two arguments) arithmetic expression. In this coursework you will be
implementing various subclasses of this class. The abstract class should include at least
two methods called evaluate and display

OK, I have no idea why your teacher doesn't like what you did. But (s)he's the boss, so there's no point arguing. I honestly don't know how to advise you now. I just hope someone else here understands teachers better than I do. All I can do now is apologise and wish you good luck.
Good luck.
J

This article has been dead for over six months. Start a new discussion instead.