Assignment 12 - Solution

List Manager with AWT

Write the code for a program that creates, manages and displays a OneWayList.

Note: you should not use BreezyGUI for this assignment.

Here's the code:

import java.awt.*;
import java.awt.event.*;

public class AWTExample2 extends    Frame 
                         implements ActionListener, 
                                    WindowListener {

  private Button up, down, left, right, nu; 
  OneWayList myList = new OneWayList(); 
  int counter = 0; 

  AWTExample2() {
    nu    = new Button("New"    ); 
    up    = new Button("Insert" ); 
    down  = new Button("Next"  );
    right = new Button("First"   ); 
    left  = new Button("Remove" );

    FlowLayout layout = new FlowLayout(); 
    setLayout(layout); 

    add(nu);       nu.addActionListener(this); 
    add(up);       up.addActionListener(this); 
    add(down);   down.addActionListener(this); 
    add(right); right.addActionListener(this); 
    add(left);   left.addActionListener(this); 

    addWindowListener(this); 
  } 
 
  public void actionPerformed(ActionEvent e) {
    Button button = (Button)e.getSource(); 
    if        (button == up)    {
      System.out.println("Up button pressed."); 
      myList.insert(counter++ + ""); 
      System.out.println(myList); 
    } else if (button == down)  {
      System.out.println("Down button pressed."); 
      if (!myList.atEnd())
        myList.next(); 
      System.out.println(myList); 
    } else if (button == right) {
      System.out.println("Right button pressed."); 
      myList.first(); 
      System.out.println(myList); 
    } else if (button == nu) {
      System.out.println("Right button pressed."); 
      myList = new OneWayList(); 
    } else                      {
      System.out.println("Left button pressed."); 
      if (!myList.isEmpty())
        myList.remove(); 
      System.out.println(myList); 
    } 
    repaint(); 
  } 

  public void paint(Graphics g) {
    g.setColor(Color.black); 
    if (myList != null) {
      if (myList.first != null) {
        myList.first.show(g, myList.current, 10, 100); 
      } else {
        g.drawString("The list is empty.", 10, 100); 
      } 
    } else {
        g.drawString("Please start by pressing New.", 10, 100);   
    } 

  } 
 
  public void windowClosing     (WindowEvent e) { 
    System.exit(0); 
  } 
  public void windowActivated   (WindowEvent e) { } 
  public void windowClosed      (WindowEvent e) { } 
  public void windowDeactivated (WindowEvent e) { } 
  public void windowDeiconified (WindowEvent e) { } 
  public void windowIconified   (WindowEvent e) { } 
  public void windowOpened      (WindowEvent e) { } 

  public static void main(String[] args) {
    Frame frm = new AWTExample2(); 
    frm.setSize(250, 250); 
    frm.setVisible(true); 
  }  
}
                                     



import java.awt.*;

class OneWayList {
  List current;
  List previous;
  List first; 
  
  int size; 

  boolean isEmpty() { 
    return first == null;   
  }
  boolean atEnd() { 
    return current == null; 
  }
  int size() { 
    return size;
  }
  void first() { 
    current = first; 
    previous = null;
  } 
  void next() { 
    previous = current; 
    if (current != null) 
      current = current.cdr;  
  }
  Object access() { 
    return current.car; 
  }
  void replace(Object obj) { 
    current.car = obj; 
  }
  void insert(Object obj)  {
    size += 1; 
    if (previous == null && current == null) {
      current = new List(obj, null); 
      first = current;       
    } else if (current == null) {
      previous.cdr = new List(obj, null); 
      current = previous.cdr; 
    } else if (previous == null) {
      current = new List(obj, current); 
      first = current; 
    } else {
      previous.cdr = new List(obj, current); 
      current = previous.cdr; 
    } 
  } 
  Object remove() {
    Object value = null; 
    if (previous != null && current != null) {
      previous.cdr = current.cdr;
      value = current.car;
      current = current.cdr; 
      size -= 1; 
    } else if (previous == null           
                         && current != null) {
      first = current.cdr; 
      value = current.car; 
      current = current.cdr; 
      size -= 1;  
    } 
    return value; 
  } 
  public String toString() {
    if (first == null) { 
      return "null"; 
    } else {
      return first.show(current); 
    } 
  } 
} 
       
class List { 
  Object car;   
  List cdr; 
  List(Object car, List cdr) {
    this.car = car;
    this.cdr = cdr; 
  } 
  public String show(List current) {
    if (cdr == null) {
      if (this == current) { 
        return "[" + car + "]"; 
      } else {
        return (String)car; 
      } 
    } else {
      if (this == current) {
        return "[" + car + "] " + cdr.show(null); 
      } else {
        return car + " " + cdr.show(current); 
      } 
    } 
  } 
  public void show(Graphics g, List current, int x, int y) {
    g.drawRect(x, y, 30, 20); 
    g.drawString((String)car, x + 3, y + 16); 
    if (cdr == null) {
      if (this == current) {
        g.drawLine(x + 15, 25 + y, x + 15, 40 + y); 
        g.drawLine(x + 15, 25 + y, x + 10, 30 + y); 
        g.drawLine(x + 15, 25 + y, x + 20, 30 + y); 
      } else {
        // nothing 
      } 
    } else {
      g.drawLine(x + 30, y + 10, x + 45, y + 10);
      g.drawLine(x + 45, y + 10, x + 40, y + 5);
      g.drawLine(x + 45, y + 10, x + 40, y + 15); 
      if (this == current) {
        g.drawLine(x + 15, 25 + y, x + 15, 40 + y); 
        g.drawLine(x + 15, 25 + y, x + 10, 30 + y); 
        g.drawLine(x + 15, 25 + y, x + 20, 30 + y); 
        cdr.show(g, null, x + 45, y); 
      } else {
        cdr.show(g, current, x + 45, y); 
      } 
    } 
  } 
}