Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2

02262012, 08:36 AM #1
 Join Date
 Dec 2011
 Location
 abuja
 Posts
 18
 Thanks
 1
 Thanked 0 Times in 0 Posts
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?

02262012, 08:08 PM #2
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 17,026
 Thanks
 4
 Thanked 2,668 Times in 2,637 Posts
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)); } }
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.