CS 206: Data Structures
Exercise#8
Due on Tuesday, March 4
1) Implement the List ADT as discussed in class using a dynamic array. Use it in your porgram from Exercise#6 (instead of the simple array of BankAccount). Functionally, your program will not change at all. The only changes will be in the use of the List ADT.
2) The implementation discussed in the class is not complete. Complete it. I.E. implement all the remaining methods: remove, set, get, and contains (see page 91 of your text). Make sure that all your List methods are correctly implemented. The versions in the text are slightly different, so be careful. The exercise below is designed to test your implementation.
3) In addition to the Bank Application, we will no use a simple List tester program to test the implementation of all the List operations. The applet below can be used as a possible List Tester program. Try it for all but the above unimplemented operations and see how the list behaves. It is written using the AList1 implementation (i.e. fixed size arrays):
The above applet is produced using the Java program below. You will need to complete the program (for the unmplemented features) to use as a tester for ALL the list operations.
/*
A List Tester program
*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class TestLists extends Applet implements ActionListener
{
// GUI Stuff
TextArea outputArea;
Button clearB, sizeB, emptyB, add1B, add2B, removeB, getB, setB, containsB, displayB;
TextField add1Text, add2ItemText, add2PosText, removeText, getT, setPosText, setItemText, containsText;
// Program variables
ListInterface L;
public void init() {
makeGUI();
L = new AList1();
// print out the current length of the list
outputArea.appendText("The list has " + L.size() + " elements.\n\n");
// add a bunch of numbers to the list
for (int i = 0; i < 10; i++)
L.add(new Integer(i));
// display the contents of the list
displayList();
} // init
public void displayList() {
outputArea.appendText("The list contains the following " + L.size() + " elements...\n");
for (int i = 0; i < L.size(); i++)
outputArea.appendText(L.get(i) + " ");
outputArea.appendText("\n\n");
} // displayList
public void addToList() {
String vs = add1Text.getText();
Integer value = new Integer(vs);
if (L.add(value))
outputArea.appendText("Added " + value + " to the end of list.\n\n");
else
outputArea.appendText("Operation failed.\n\n");
} // addToList
public void addToList2() {
String vs = add2ItemText.getText();
Integer value = new Integer(vs);
String ps = add2PosText.getText();
Integer pI = new Integer(ps);
int position = pI.intValue();
if (L.add(position, value))
outputArea.appendText("Added " + value + " at position " + position + ".\n\n");
else
outputArea.appendText("Operation failed.\n\n");
} // addToList2
public void getFromList() {
String vs = getT.getText();
Integer vI = new Integer(vs);
int value = vI.intValue();
outputArea.appendText("Got item: " + L.get(value) + " from position: " + value + ".\n\n");
} // getFromList
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearB) {
outputArea.appendText("Clearing the list.\n\n");
L.clear();
}
else if (e.getSource() == sizeB)
outputArea.appendText("The list contains " + L.size() + " elements.\n\n");
else if (e.getSource() == displayB)
displayList();
else if (e.getSource() == add1B)
addToList();
else if (e.getSource() == add2B)
addToList2();
else if (e.getSource() == getB)
getFromList();
else if (e.getSource() == emptyB) {
if (L.isEmpty())
outputArea.appendText("The List is empty.\n\n");
else
outputArea.appendText("The List is not empty.\n\n");
}
} // actionPerformed
public void makeGUI() {
this.setLayout(new BorderLayout());
// Big Panel for controls
Panel mainPanel = new Panel();
mainPanel.setLayout(new GridLayout(4,1));
// Make the clear, size, empty, display panel
Panel p1 = new Panel();
p1.setLayout(new GridLayout(1,3));
clearB = new Button("Clear");
clearB.addActionListener(this);
sizeB = new Button("Size");
sizeB.addActionListener(this);
emptyB = new Button("isEmpty?");
emptyB.addActionListener(this);
displayB = new Button("Display List");
displayB.addActionListener(this);
p1.add(clearB);
p1.add(sizeB);
p1.add(emptyB);
p1.add(displayB);
mainPanel.add(p1);
// make the add panel
Panel p2 = new Panel();
p2.setLayout(new GridLayout(2,1));
Panel add1P = new Panel();
add1B = new Button("Add");
add1B.addActionListener(this);
add1Text = new TextField(5);
add1P.add(add1B);
add1P.add(add1Text);
Panel add2P = new Panel();
add2B = new Button("Add");
add2B.addActionListener(this);
add2ItemText = new TextField(5);
Label add2L = new Label("AT");
add2PosText = new TextField(5);
add2P.add(add2B);
add2P.add(add2ItemText);
add2P.add(add2L);
add2P.add(add2PosText);
p2.setBackground(Color.gray);
p2.add(add1P);
p2.add(add2P);
mainPanel.add(p2);
// make the get and contains Panel
Panel p4 = new Panel();
p4.setLayout(new GridLayout(2, 1));
Panel getP = new Panel();
getB = new Button("Get");
getB.addActionListener(this);
getT = new TextField(5);
getP.add(getB);
getP.add(getT);
Panel containsP = new Panel();
containsB = new Button("Contains");
containsB.addActionListener(this);
containsText = new TextField(5);
containsP.add(containsB);
containsP.add(containsText);
p4.add(getP);
p4.add(containsP);
mainPanel.add(p4);
// make the remove and set panel
Panel p3 = new Panel();
p3.setLayout(new GridLayout(2,1));
Panel removeP = new Panel();
removeB = new Button("Remove");
removeB.addActionListener(this);
removeText = new TextField(5);
removeP.add(removeB);
removeP.add(removeText);
Panel setP = new Panel();
setB = new Button("Set");
setB.addActionListener(this);
setItemText = new TextField(5);
Label setL = new Label("AT");
setPosText = new TextField(5);
setP.add(setB);
setP.add(setItemText);
setP.add(setL);
setP.add(setPosText);
p3.setBackground(Color.gray);
p3.add(removeP);
p3.add(setP);
mainPanel.add(p3);
// add the main panel
this.add(mainPanel, "North");
outputArea = new TextArea("Testing the List ADT\n\n", 400, 200, TextArea.SCROLLBARS_BOTH);
this.add(outputArea, "Center");
outputArea.setBackground(Color.lightGray);
} // makeGUI
} // TestLists