Exercise Sheet B

Last Week's Exercise

We are looking over your submissions for exercise sheet A. On the whole they were good. Luckily, you get the chance to make them even better.

We are writing some comments onto them now and will hand them back to you in the next couple of weeks. I will then notify you by email of the date for resubmission of your improved versions.

Some problems cropped up time and time again. I summarise a few of them here:

Exercise

Your work for this exercise sheet will be automatically collected in (if you put it in the right place) at 5 p.m. on Friday 8th November.

Create a subdirectory of your cs2000 directory. Call this new directory sheetB. Place your answers to this exercise in your cs2000/sheetB directory.

This exercise gives you practice in writing a fairly straightforward class definition (`blueprint'). In particular, you will be writing a class definition called ATM, which represents simplified ATMs (automated teller machines, i.e. hole-in-the-wall cash dispensers) for an international bank. Machines in different countries will be loaded with banknotes of different denominations depending on the currency of the country.

Read the whole exercise before writing any code.

Part 1

Create a file ATM.java in which you will write a class definition (`blueprint') for ATMs. (Do this by copying and editing the template mentioned in the lecture.)

Here's some client code, which will become clearer as you read the rest of the exercise:

int[] euroDenoms = {5, 10, 20, 50}; // denominations
ATM atm = new ATM("euro", euroDenoms); // create the ATM
atm.load(5, 1); // load it with 1 x 5 euro notes
atm.load(20, 3); // load it with 3 x 20 euro notes
atm.load(50, 2); // load it with 2 x 50 euro notes
System.out.println(atm.getQty(20)); // print how many 20 euro notes
atm.display(); // display the contents
atm.dispense(50, 1); // dispense 1 x 50 euro note to a customer
System.out.println(atm.getTotal() + " " + atm.getCurrency()); // print total

Each ATM must know

(So, define three instance variables!)

The interface of your ATM class should consist of the following:

Part 2

If you haven't already done so, you need to test your ATM class definition. I've written some extensive client code to help you do this. Get your copy here: CashFlow.java.

First make sure that you can compile CashFlow.java and ATM.java. (Compiling the former will automatically compile the latter because the latter is client code of the former.) If there are compile-time errors, fix ATM.java. (All such errors are caused by your file; do not change CashFlow.java.)

Then, when there are no more compile-time errors, try to execute CashFlow. If your program is perfect, the following will be displayed on the screen:

a b c d e f g h i j k l m n o p q r s t u v w x y z

atm1.display()
==============
euro5 x 1
euro10 x 1
euro20 x 0
euro50 x 1

atm2.display()
==============
peso500 x 0
peso1000 x 5
peso2000 x 10
peso5000 x 0
peso10000 x 0

Check your output carefully against the above. If there are differences, then you have a bug.

Why does it print the alphabet? CashFlow.java carries out a sequence of tests on your program. After each test it prints a letter of the alphabet. This is so you can see how far through the tests your program gets. If it prints a-z, then it got through all the tests. But if it stops printing before it gets to z, then there is a problem, e.g.:

a b c d e f There's an error in your ATM definition.

or

a b c Exception in thread "main" java.lang.NullPointerException
        at ATM.getTotal(ATM.java:48)
        at CashFlow.main(CashFlow.java:20)

Look at the last letter it prints. Find the line in CashFlow.java that prints that letter. The problem arose when trying to execute whatever statement comes after that line.

Part 3

Check all your files. Are they in the right directory? Are they correctly named? Does your name appear in an @author comment at the start of each file? Did you lay them out using the template? Are your instance variables private? Are your variable names meaningful? Are curly braces aligned? Etc. etc.

Elegance, conciseness, clarity, consistency of layout, good commenting, and meaningful variable names are all as important in the marking criteria as whether the program works or not.

Challenge Exercise

Sheet A's Challenge Exercise was reasonably demanding, so here's a short and much much easier challenge for Sheet B. Remember, challenge exercises are optional. They never count toward your final mark.

Create a subdirectory of your sheetB directory, calling this new directory challenge. Put a copy of your ATM.java class into this new directory.

The task is to add a second but more realistic version of dispense to ATM.java, as follows:

public int[] dispense(int theAmount)

This method is used to dispense a specified amount of money from this ATM, if possible. It should use the minimum number of notes by making use of as many higher denomination notes as possible before using lower denomination notes.

If it can dispense the requested amount of money exactly, it updates the third instance variable and returns an array: each value in this array is the number of notes of the corresponding denomination that is being dispensed.

If the given amount cannot be exactly dispensed using the notes that are currently in this ATM, the contents of the ATM are not changed, and the array that is returned contains all zeros.

E.g. if the machine contains 2 x 5 euro note, 3 x 10 euro notes, 3 x 20 euro notes and 2 x 50 euro notes, then after executing the following

int[] cashInHand = atm.dispense(75);

the array cashInHand would contain {1, 0, 1, 1}, which shows that to dispense 75 euro with the fewest notes, it used 1 x 5, 0 x 10, 1 x 20 and 1 x 50. (The contents of the machine will be decremented also.)

But after executing the following

int[] anotherWad = atm.dispense(76);

the array anotherWad would contain {0, 0, 0, 0}, which shows that this amount cannot be dispensed with the available notes. (The contents of the machine remain unchanged.)