FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
PROG2: Programmierung II
Sommersemester 2003
Exercise 4: The Calculator revisited
Finger exercises
Dig out your Programming 1 stuff. There was a calculator exercise, wasn't
there? Have a look at how you solved the problem of reading in a digit
followed by an operator followed by a digit, and how you figured out how
to calculate the value when = was pressed.
If you did not do Programming I with me, or you didn't get much working
last semester, I have a nice solution here: Main.java,
ButtonHandler.java, Calculator.java
and CalculatorGUI.java. Beware:
do not expect to read and understand this in the first few minutes of the
lab! Read it at home, discuss it with others! How did it work?
You might want to make sure that your infix-to-postfix and evaluate-postfix
methods are in working order! If not, ask to use one from a friend. Re-use
is good, if you give proper credit in your report.
Lab exercises
Get the Calculator loaded into JBuilder.
Rework it to accept a long String of digits separated by operators. The
bored may use multi-digit numbers and floating-point or scientific notation,
if they please.
Once you get a String input, add in calls to convert this expression to
postfix and evaluate the postfix when = is pressed. Presto, your calculator
now takes care of operator priorities, like magic!
(For the bored) Extend your calculator to include buttons for entering
in the digits 'A' to 'F'. Make the calculator do its calculations and display
in hexadecimal notation. Have a checkbox for switching between decimal
and hexadecimal formats. Or make a whole collection of calculators: binary,
octal, decimal, hexadecimal. Maybe make a CalculatorFactory that takes
the base as a parameter to the constructor, figures out how to layout the
buttons nicely, and calculates in the selected base mode? It looks like
it is going to rain this weekend anyway....
Work in groups of two, and just submit one report, detailing who did what
part of the work.