Algorithms and Data Structures in an Object-Oriented Framework (“ADSOOF”)

Code for “Using Objects” section

The .class files provide the objects we are using in these examples, DrinksMachine objects and Can. The .java files provide “front-end” code. This is code which provides short scripts where DrinksMachine objects are set up and methods called on them. In these examples, we are concentrating on writing such front-end code which uses objects rather than writing code which defines objects. Maintaining a clear distinction between the code which defines object of a particular class and the code which uses objects of that class is vital in being able to manage the complexity of large scale programs. The distinction is clear here because you only have the front-end code. You are expected just to use the .class files which define the objects, at this stage you should not be concerned with writing code yourself to define these objects. To keep it simple, our front-end code has a text-based interface.

The signatures for the public methods and constructors for class DrinksMachine are given in the notes here.

Code which does not use exceptions

The version of DrinksMachine here does not throw any checked exceptions. So you can use it to write code which does not involve the additional issue of catching exceptions. It can throw an unchecked exception of a type which is not one of Java's pre-defined exceptions, which is why you need the file that provides that exception (EmptyCanException.class).

Code which uses exceptions

Here are files which define modified versions of the DrinksMachine class where some of the methods do throw checked exceptions. There is front-end code which makes use of objects of these modified versions, you will see it includes sections to catch the exceptions that could be thrown. As the checked exceptions are of types which are not one of Java's predefined exceptions, you need the .class files which define them.


Matthew Huntbach

Last modified: 16 July 2019