Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-26-2012, 07:36 AM   PM User | #1
greeninho
New Coder

 
Join Date: Dec 2011
Location: abuja
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
greeninho is an unknown quantity at this point
presenting a simple calculator, needs help urgently

Happy sunday to all. I have been to write programs on simple arithmetic operations but i encountered a problem trying to put the whole individual operation as one program. What i mean is that want a situation where if (an assumption) both an additional and a subtraction program are written as one program, whereby one is given an option of like 'click here to add or subtract' something of that kind. I need help on how to go about it, hope my thread is clear and understandable?
greeninho is offline   Reply With Quote
Old 02-26-2012, 07:08 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Since we have no idea what you have done, I can only suggest routes in which you can decouple the arithmetic control from the calculator itself. For that, a simple interface, a calculator, and each class written individual would work fine. This one I wrote a calculator with a hashtable to register ops to the calculator. Wrote a simple main test without any error checking in it, and it works fine:
Code:
package calc;

public interface IOperation
{	
	public void registerOperation(Calculator calc);
	public double calculate(double lhs, double rhs);
}

// Add
package calc;

public class Add implements IOperation
{

	public Add(Calculator calc)
	{
		this.registerOperation(calc);
	}

	@Override
	public void registerOperation(Calculator calc)
	{		
		calc.registerOperation("+", this);
	}

	@Override
	public double calculate(double lhs, double rhs)
	{
		return lhs + rhs;
	}

}

// Calculator
package calc;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

public class Calculator
{
	private Hashtable<String, IOperation> htOps = new Hashtable<String, IOperation>();
	public void registerOperation(String sOp, IOperation iOp)
	{
		this.htOps.put(sOp, iOp);
	}
	
	public IOperation getOperation(String sOp)
	{
		return this.htOps.get(sOp);
	}
	
	public double calculate(IOperation op, double lhs, double rhs)
	{
		return op.calculate(lhs, rhs);
	}
	
	public static void main(String... argv)
	{		
		Scanner s = new Scanner(System.in);
		double lhs, rhs;
		String sOp;
		StringBuffer opList = new StringBuffer();
		int i = 0;
	
		Calculator c = new Calculator();
		new Add(c);
		
		Enumeration<String> e = c.htOps.keys();
		while (e.hasMoreElements())
		{
			if (i++ > 0)
			{
				opList.append(", ");
			}
			opList.append(e.nextElement());
		}

		System.out.print("Enter lhs: ");
		lhs = s.nextDouble();
		s.nextLine();
		System.out.print("Enter ops (from list: " + opList + "):");
		sOp = s.nextLine();
		
		System.out.print("Enter rhs: ");
		rhs = s.nextDouble();
		s.nextLine();
		System.out.println(c.calculate(c.getOperation(sOp), lhs, rhs));
		
	}
}
I used the console, but a gui would work fine as well.
Registering on the calculator is optional. The classes are easier written without the registration at all, but I liked the idea of giving a list of what the calculator could do.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:02 PM.


Advertisement
Log in to turn off these ads.