SDSU CS 535: Object-Oriented Programming & Design
Fall Semester, 1997
Doc 20 Comments on Assignment 2

To Lecture Notes Index
San Diego State University -- This page last updated 04-Nov-97

Contents of Doc 20 Comments on Assignment 2

  1. References
  2. Logs - Actual Time
  3. Indentation, White space
  4. Names
  5. Don't Reinvent the Wheel
  6. Cite Your References
  7. Orthogonality
  8. Node Issues
  9. Cursor, Enumeration, Accessing Elements
  10. Dumb Verses Smart Node
  11. LinkedList- SortedLinked List Inheritance Problem
  12. Problem 1 What was the Point?

References


Various student papers

Object-oriented Software Construction, Bertrand Meyer, 1988, Appendix A.4, A.5


Doc 20 Comments on Assignment 2 Slide # 1

Logs - Actual Time

RADesignCodingDebuggingTestingTotal
1.52520151576.5
1616228971
683515367
0.5123015663.5
315354360
552003060
2015200055
1010206652
811206651
811206651
591812448
208125348
65258145
871410342
1101610441
771213140
46186438
28224238
24256138
141516238
101234332
1101531.530.5
74125230
25.5611529.5
38954.529.5
17135329
101061229
21253527
35115327
15411627
24163126
7483.5123.5
16104122
5535422
4592.51.522
0.5694221.5
1672420
22123120
11511119
24.563318.5
5253318
2652.5015.5
1.5433213.5
0.51.5100.5113.5
3234113
1.50.581.50.512
1124210
1250.519.5
1.52.540.50.59


Doc 20 Comments on Assignment 2 Slide # 2

Indentation, White space


Better than assignment one, but some still need improvement

Be consistent, don't mix styles
public void foo ()  {
   int x;
   int y;
}

public void bar()
{
   int z;
   int w;
}

Doc 20 Comments on Assignment 2 Slide # 3

Names

Delete or Remove

Does one delete or remove an element from a list?
deleteNodeFromFront()
deleteFirst()
removeFirst()

Try to be consistent as possible with existing API!

What does Vector use?

size or length()
class LinkedList
{
   public void numberOfNodes(){ ...}
}
class LinkedList
{
   public void size(){ ...}
}

class LinkedList
{
   public void length(){ ...}
}

Vector uses size,
String, array use length

Doc 20 Comments on Assignment 2 Slide # 4
Package name

What to call the package containing the linked list?
package myList;
package linkedList;
package mjkLinkedList;
package whitney.util;


What does java library use?


Don't Reinvent the Wheel


java.util.NoSuchElementException exists
no need to reimplement it in your package

sdsu.compare has all the standard comparers
no need to reimplement in your package

Doc 20 Comments on Assignment 2 Slide # 5
Methods as Functions
class Node
   {
   protected Object data;
   protected Node next = null;
   protected Node previous = null;
   

   public Node( Object data, Node previous, Node next )
      {
      this.data = data;
      this.next = next;
      this.previous = previous;
      }
   public Node append( Object data, Node appendHere )
      {
      Node newNode = new Node( data, appendHere, null );
      appendHere.next = newNode;
      return newNode;
      }

   public Node prepend( Object data, Node prependHere )
      {
      Node newNode = new Node( data, null, prependHere );
      prependHere .previous = newNode;
      return newNode;
      }

   }

Doc 20 Comments on Assignment 2 Slide # 6

Cite Your References

Academic World

Not citing your references is considered plagiarism

People get expelled from school and lose jobs for plagiarism

Citing your references is considered part of good scholarship

Programming World

If you use a reference to help solve a problem/implement code then the next person may also benefit from the reference

Let them know the reference


Doc 20 Comments on Assignment 2 Slide # 7

Orthogonality

class LinkedListA
{
   public void insertFront( int data){  
      insertFront( new Integer( data));
   }
   public void insertFront( float data){ ...}
   public void insertFront( long data){ ...}
   public void insertFront( double data){ ...}
   public void insertFront( short data){ ...}
   public void insertFront( char data){ ...}
   public void insertFront( Object data){ ...}

   public void insertTail( int data){ ...}
   public void insertTail( float data){ ...}
   public void insertTail( long data){ ...}
   public void insertTail( double data){ ...}
   public void insertTail( short data){ ...}
   public void insertTail( char data){ ...}
   public void insertTail( Object data){ ...}

   public void insertAt( int data, int location) { ... }
   public void insertAt( float data, int location) { ... }
   etc.

   public Object elementAt( int location ) { ... }
   public int intAt( int location ) { ... }
   public float floatAt( int location ) { ... }
   public long longAt( int location ) { ... }
   public double doubleAt( int location ) { ... }
   public short shortAt( int location ) { ... }
   public char charAt( int location ) { ... }
}


Doc 20 Comments on Assignment 2 Slide # 8
Orthogonality - Continued


class LinkedListB
   {
   public void insertFront( Object data){ ...}

   public void insertTail( int data){ ...}

   public void insertAt( int data, int location) { ... }

   public Object elementAt( int location ) { ... }
   }


LinkedListA and LinkedListB have the same functionality

Java's treatment to primitive types (int, etc) with respect to Object is a pain

Adding methods to automatically wrap primitive types in objects is tempting, but leads to explosion of methods

Doc 20 Comments on Assignment 2 Slide # 9

Node Issues

Information Hiding

A Node is part of the internal representation of a linked list

Hide it from the user of the list!

So put the Node class in a package

Give it package access only

package whitney.util;

class Node
   {
   }


Do not return an node from a Linked List method
class LinkedList
   {
   public Node getNodeAt( int location) { ... } // No
   public Object getElementAt( int location ) { ... } // Yes
   }

Doc 20 Comments on Assignment 2 Slide # 10

Cursor, Enumeration, Accessing Elements

Cursor

Maintain a current pointer in the linked list

Improves efficiency in accessing/inserting/removing as these operations tend to cluster in the same location
class LinkedList
   {
   private Node head;
   private Node tail;
   private Node current;

   public Object nextElement()
      {
      if ( current == tail )
         throw new OutOfBoundsException();

      current = current.next();
      return current.getData();
      }

   public Object nextElement()
      {
      if ( current == head )
         throw new OutOfBoundsException();

      current = current.previous();
      return current.getData();
      }

   public void insert( Object newData)
      {
      current.prepend( newData);
      }
   }

Doc 20 Comments on Assignment 2 Slide # 11
Cursor, Enumeration, Accessing Elements

An internal cursor provides the functionality of an enumerator

Why do both?

Doc 20 Comments on Assignment 2 Slide # 12

Dumb Verses Smart Node

class DumbNode
   {
   Object data;
   DumbNode next;
   
   public DumbNode( Object data, DumbNode next )
      {
      this.data = data;
      this.next = next;
      }
   }

class SmartNode
   {
   protected Object data;
   protected SmartNode next = null;
   protected SmartNode previous = null;

   public SmartNode( Object initialData, 
                     SmartNode previousInList, 
                     SmartNode nextInList )
      { etc. }
      
   public void append( int data) { etc. }

   public void prepend( int data) { etc. }

   public void remove() { etc. }
      
   public int getData() { etc. }
   
   public void setData( int newData ){ etc. }
   }


Doc 20 Comments on Assignment 2 Slide # 13
Dumb Verses Smart Node

People use dumb nodes a lot

One justification is the node is part of the linked list abstraction and is completely hidden inside the linked list class

This leads to bad habits and poor code!

Eiffel View

The Eiffel class library is well crafted and optimized for performance


class Node methods
Eiffel NameJava Equivalent
CreateConstructor
change_valuesetData( new value)
rightgetNext()
change_rightsetNext( new value)
put_betweenlinkTogether( leftNode, rightNode)



Doc 20 Comments on Assignment 2 Slide # 14
Eiffel Singlely Linked List
firstreturn value of first element
valuereturn value of element at cursor
change_valueset value of element at cursor
startmove cursor to first element
forthmove cursor to next element
go_offleftMove cursor off left edge
duplicateclone list
insert_rightinsert element to right of cursor
insert_leftinsert element to left of cursor
deletedelete element at cursor, move cursor right
delete_rightdelete element to the right of the cursor
delete_leftdelete element to the left of the cursor
delete_all_occurrencesdelete all occurrence of argument from list
wipe_outempty the list

Doc 20 Comments on Assignment 2 Slide # 15

LinkedList- SortedLinked List Inheritance Problem


If LinkedList has insertFront, insertTail and insertAt methods

SortedLinkedList can not implement these methods!

But SortedLinkedList is a LinkedList and can use most of the methods in LinkedList!
Solution 1 Fake it

Make SortedLinkedList a subclass of LinkedList

Reimplement insertFront, insertTail and insertAt to insert at proper sorted location
Solution 2 Exceptions

Make SortedLinkedList a subclass of LinkedList

Reimplement insertFront, insertTail and insertAt to throw an exception when called

Java 1.2 will have java.lang.UnsupportedOperationException for this purpose
Solution 3 Common Operations

Create an interface or super class that contains all common operations of both classes

Each class implements (extends ) the common interface (class)

Doc 20 Comments on Assignment 2 Slide # 16

Problem 1 What was the Point?




visitors since 04-Nov-97