• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For your advice on the Calulator so far developed

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic