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.
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
).
.class
file enabling you to create and use objects of type
DrinksMachine
.
.class
file enabling you to create and use objects of type
Can
.
Can.class
as code in this class uses it, but no direct reference is made to it in this
section. You do not need to be aware of it otherwise, because the exception type it defines is unchecked.
DrinksMachine
object, and calling
methods on it, including one which returns a Can
object.
DrinksMachine
object as an
argument to a static method.
DrinksMachine
object passed as an argument to it.
DrinksMachine
object created inside the method call.
DrinksMachine
object passed to it instead of creating a new one representing the change as
is done in UseDrinksMachine4.java
.
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.
.class
file which provides the same methods as
DrinksMachine.class
, but the methods
pressCoke()
and pressFanta()
throw
an exception rather than return null
when called on an
object representing a machine which is empty of the appropriate drinks.
pressCoke()
and pressFanta()
methods of
DrinksMachineA
objects.
DrinksMachineA
object, and calling
methods on it, including one which returns a Can
object or
may throw an exception, in which case the exception is caught in a
try
...catch
statement.
DrinksMachineA
object as an
argument to a static method, with the method making use of exceptions.
Shows a loop terminated by an exception being thrown and caught.
DrinksMachineA
object passed as an argument to it.
Shows a try
...catch
statement inside a try
...catch
statement.
DrinksMachineA
object created inside the method call.
UseDrinksMachines3A.java
.
getMessage
method called on an
exception object.
throw
to throw an exception,
in this case catching one exception and throwing another.
try
...catch
statement with
more than one catch
part.
.class
file which provides a modified version of
DrinksMachineA
dealing correctly with calling
pressCoke()
and pressFanta()
when the
balance is less than a price of a drink: it throws a separate
exception NotEnoughMoneyException
in this case, rather
than EmptyMachineException
pressCoke()
and pressFanta()
methods of
DrinksMachineB
objects.
DrinksMachineB
object, and calling
methods on it, including one which returns a Can
object or
may throw an exception which could have two types. The exception is caught
in a try
...catch
statement with two separate
catch
parts.
UseDrinksMachines2A.java
in which the
machine used is of type DrinksMachineB
. Shows that
exceptions which may never actually be thrown must still have code to
deal with them if they are declared as being thrown by a method call.
Last modified: 16 July 2019