Java Reference
In-Depth Information
15.1 Simple ArrayIntList
In this section we will develop an initial version of the ArrayIntList class that contains
appropriate fields for storing a list and a minimal set of operations for manipulating it.
Recall from Chapter 8 that there are two important perspectives of any class. The
first is the external view that a client of the class will have. Clients in general don't
want to understand all of the internal details of the class. They just want to know
what it does. As a result, we want to have a clear specification for the client of what
the ArrayIntList class is supposed to do. We think of this as a contract that we
have with the client.
Contract
A clear specification that tells a client how an object behaves without
describing the details of how it is implemented.
The second view is the internal view of the implementer. We have to figure out
how to make the object work—how to satisfy the contract. When we are thinking as
an implementer, we will consider the nitty-gritty details of the innards of an object.
It is sometimes confusing to switch between these two different perspectives, but
you will get better at it as you practice.
Adding and Printing
Let's start by developing a version of the class which has a single mutator method
that will append values to the end of the list and a single accessor method that will
display the contents of the list. Assume that the client code will look something like
the following:
1 public class Client1 {
2 public static void main(String[] args) {
3 // construct two lists
4 ArrayIntList list1 = new ArrayIntList();
5 ArrayIntList list2 = new ArrayIntList();
6
7 // add 1, 82, 97 to list1
8 list1.add(1);
9 list1.add(82);
10 list1.add(97);
11
12 // add 7, -8 to list2
13 list2.add(7);
14 list2.add(-8);
15
16 // report results
 
Search WWH ::




Custom Search