Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For your advice on the Calulator so far developed

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is the class which contains Arithmetic Logic and the Test class. Please advice me on the development and the areas I should improve when designing an application



The below is the Test class that I have written so far for Addition and subtraction
operations


 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this for a school assignment or something? Why not just implement methods like add(int i), subtract(int i), etc. You'd need a single instance field -- value -- and you'd add to it or subtract from it in those methods. Much simpler. Something like:


And your test routine would be like:

 
Varuna Seneviratna
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Max., I want be able to implement the calculator interface via this class, if I use your class is it possible.For ex:- 1 + 4 + 6 - 9=2, now in a calculator interface how can I invoke the add operation when + is pressed - operation when "- "is pressed using your class design?.
This is a exercise from the book Objects First With Java
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varuna: Oh, it's from a book. Since the person who made it up defined certain requirements, I'd have to see what he those are to comment on your implementation. Can you copy those here? Off hand, I'd avoid passing chars, such as + and -, to methods and have the code interpret them, since those are part of the user interface. In other words, you probably still need methods like add() and subtract(). If you wanted to get very OO, then you might code an Operator class, with subclasses like Add and Subtract. These could have a do(int operand1, int operand2), method that would do subtraction or addition or whatever. In this case, your calculator, or ALU class, would have an applyOperator(Operator o) method where'd you pass that in.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic