Introduction to Programming: Additional work for week 14

The local notes on the drinks machine examples, found here cover a few more examples than were given in the lectures. Inheritance is demonstrated in the class DrinksMachineE which extends class DrinksMachineA. It shows how if we want a drinks machine that serves one extra variety of drinks we don't have to write a class which has all the previous methods repeated again, we can just write a class which inherits those methods and adds an extra one for the new variety of drink.

So this means an object of type DrinksMachineE is also of type DrinksMachineA and hence is also of type DrinksMachine. The same rules about more general and more specific assignment and type casting apply when we consider using variables of type DrinksMachineA to refer to objects of its subtype DrinksMachineE as they do when we were dealing with DrinksMachine and DrinksMachineA

A Java class can only extend one other class (this is known as single inheritance), but it can implement any number of interfaces. The ability to implement more than one interface is a limited form of multiple inheritance. An example is given in the notes which shows this, an interface BeverageMachine is defined, and a class, DrinksMachineF is given which implements both DrinksMachine and BeverageMachine. So DrinksMachineF is a subtype of both DrinksMachine and BeverageMachine though neither is a subtype of the other.

You should read these notes, including the extra examples mentioned above, and also look at your textbook(s) to see how they cover the same topics. We have now covered most of the material that is in the first 14 chapters of the Barnes textbook (Chapter 10 is an exception, and I probably will not cover Collection Classes which that chapter is about as that is a little too Java-specific for this course). A lot of the Horstmann book has been covered, though chapters 4, 10 and 12 haven't and won't be, since these are on the graphical aspects of Java which I have decided not to cover in this course (and which are again too Java specific for what this course is about). The material in the Bishop textbook up to chapter 9 (excluding some of the very Java specific chapter 7) has been covered (you might like to note that the 3rd edition of this textbook has very recently been published). The material in the Pohl and McDowell book has been covered up to chapter 7, and also chapter 11. The material in the King book up to chapter 11, except chapter 6 which is on graphics, has been covered. The Deitel and Deitel textbook covers a lot more Java-specific material than the other textbooks, so less of the material in that has been covered (and less of the material in that book is relevant, however it's useful if you want to look more at what Java does beyond what can be covered in this course).

Exercise

Early design decisions sometimes have to be scrapped, and that's the case with our drinks machine example. The interface DrinksMachine specified methods which meant a machine has to charge the same price for every drink. The use of a separate method for each choice of drink was inflexible (consider our drinksMachineD class where we had to use the pressCoke method to deliver a Pepsi).

So as an exercise, I'd like you to start again and design some code to simulate a more realistic type of drinks machine. Many drinks machines have numbered buttons, so you might for example have a call m.pressButton(31) to represent entering the number 31 into the machine and delivering whatever drink is linked to that number. Each drink should have its own price. So perhaps the internal state of a drinks machine object would have an array of strings representing the drinks available and an array of integers representing their prices.

You would need a way of displaying which button is associated with which drink. A careful design of your code should enable you to have methods that can add new drinks, or delete drinks, or change the drink associated with a button (for example, to change the drink that is supplied from one button from Coke to Pepsi).

Having done this, you could write a simple program which prints text messages telling the user of the availability of drinks and responding to commands. The program would run continuously rather like the DepositAccountMain programs in the directory /import/teaching/BSc/1st/ItP/accounts which represented interaction with a bank.

Matthew Huntbach
26th January 2001