Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JUNIT Test help for a Calculator

843789May 8 2010 — edited May 11 2010
Hi guys,

I am writting a Junit test for a the following class. I have reading tutorials but I think they just cover the basics. I having problems testing the if-else statements. For example the
compute()
in the following class.

public class CalculatorModel
{
	//value computed by executing the last operation 
	protected String lastComputedValue;
	
	//value currently being entered by the user
	protected String currentValue;
	
	//the current operation (e.g. plus, minus)
	protected String op;

    //is the calculator currently in an operation?
	protected boolean inOperation = false;

    public CalculatorModel()
    {
		reset();
    }

    /**
     * Returns the calculator to its initial state.
     * 
     * Sets the current and last computed value to "0" and sets
     * the operation to the default operation of "plus"
     */
    public void reset()
    {
		lastComputedValue = "0";
		op = "plus";
		currentValue  = "0";
	}
    

[...]


	/**
	 * Executes the current operation and stores the result as the last computed value
	 */
	public void compute()
	{
		double d1 = Double.parseDouble(lastComputedValue);
		double d2 = Double.parseDouble(currentValue);

		if(op.equals("plus"))
		{
			lastComputedValue = (new Double(d1+d2)).toString();
		}
		else if(op.equals("minus"))
		{
			lastComputedValue = (new Double(d1-d2)).toString();
		}
		else if(op.equals("times"))
		{
			lastComputedValue = (new Double(d1*d2)).toString();
		}
		else  //must be "divide"
		{
			lastComputedValue = (new Double(d1/d2)).toString();
		}

		inOperation = false;
	}
}
Any help would be appreciated.

Thanks,

Edited by: javis686 on May 8, 2010 4:00 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 7 2010
Added on May 8 2010
5 comments
462 views