FHTW Berlin
Fachbereich 4
Internationale Medieninformatik
Info2: Informatik II
Sommersemester 2006
Exercise 4: Fun with Calculators
Finger exercises
Dig out your Informatics 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 Informatics 1 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 again 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.
We are then going to make a calculator for a different data type that just ints. Choose either a rational number calculator or a date calculator. Of course, the bored are welcome to implement both or find fascinating new data types to use a calculator with!
If you are going to do a rational number calculator, then you will want to prepare by either polishing your Ratio class or selecting a nice one from the solution gallery. Do remember to give credit to the authors! Make sure that you can read and write Ratios,as well as add, subtract, multiply and divide them. This might be a good time to get the normal form working using the ggT Algorithm you had in Discrete Mathematics.
If you are going to do a date calculator, then either polish your extended JulianDate class or see if one of the bored has made something useful. Make sure that you can read and write dates in a specific locale. Add methods such as public JulianDate addDays (JulianDate base, int days); if necessary. Decide how you are going to map functions such as adding or subtracting a number of days to a date, determining the number of days between a date, and determining the weekday to the symbols +, -, *, and /.
Lab exercises
Make a copy of your Calculator and get it loaded into Eclipse. Make sure that it works before you begin!
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! Just a little bit of mathematical thought, and you can introduce lots of new functionality!
Maybe have lunch or a cup of coffee before you sontinue.
Make another new copy of the Calculator in Eclipse in a new project (don't wreck the version you just got working).
Work on the ButtonHandler to get the calculator to accept input of your chosen data type and to display output of the chosen type. If you are doing the date calculator get the calculator to display a String for the weekday in the window. Rationals should always be displayed in proper reduced form (1/2 instead of 2/4), dates can be displayed either in European, American, or ISO form.
Implement one function, for example the addition of two rationals displaying the result as a rational, or subtracting two dates from each other to determine the number of days inbetween them. This is the minimum you need to get working.
But since it really is very easy, if you have been working object-orientedly and not stuffing everything into classes called Martian or MeToo, you could go on and implement the rest of the functions for an extra star.
For the bored:
Extend your integer 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?
Add buttons to your fancy calculator for displaying the decimal value for rational numbers or for switching locale for the dates. Now include a button that, when pressed, speaks the number or word currently on the display of your calculator. Blinking lights are not necessary. 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. Your reports are due by 9.30 of the morning of your next exercise section.