| 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
- References
- Logs - Actual Time
- Indentation, White space
- Names
- Don't Reinvent the Wheel
- Cite Your References
- Orthogonality
- Node Issues
- Cursor, Enumeration, Accessing Elements
- Dumb Verses Smart Node
- LinkedList- SortedLinked List Inheritance Problem
- Problem 1 What was the Point?
Various student papers
Object-oriented Software Construction, Bertrand Meyer, 1988, Appendix
A.4, A.5
RA | Design | Coding | Debugging | Testing | Total |
1.5 | 25 | 20 | 15 | 15 | 76.5 |
16 | 16 | 22 | 8 | 9 | 71 |
6 | 8 | 35 | 15 | 3 | 67 |
0.5 | 12 | 30 | 15 | 6 | 63.5 |
3 | 15 | 35 | 4 | 3 | 60 |
5 | 5 | 20 | 0 | 30 | 60 |
20 | 15 | 20 | 0 | 0 | 55 |
10 | 10 | 20 | 6 | 6 | 52 |
8 | 11 | 20 | 6 | 6 | 51 |
8 | 11 | 20 | 6 | 6 | 51 |
5 | 9 | 18 | 12 | 4 | 48 |
20 | 8 | 12 | 5 | 3 | 48 |
6 | 5 | 25 | 8 | 1 | 45 |
8 | 7 | 14 | 10 | 3 | 42 |
1 | 10 | 16 | 10 | 4 | 41 |
7 | 7 | 12 | 13 | 1 | 40 |
4 | 6 | 18 | 6 | 4 | 38 |
2 | 8 | 22 | 4 | 2 | 38 |
24 | 2 | 5 | 6 | 1 | 38 |
1 | 4 | 15 | 16 | 2 | 38 |
10 | 12 | 3 | 4 | 3 | 32 |
1 | 10 | 15 | 3 | 1.5 | 30.5 |
7 | 4 | 12 | 5 | 2 | 30 |
2 | 5.5 | 6 | 11 | 5 | 29.5 |
3 | 8 | 9 | 5 | 4.5 | 29.5 |
1 | 7 | 13 | 5 | 3 | 29 |
10 | 10 | 6 | 1 | 2 | 29 |
2 | 12 | 5 | 3 | 5 | 27 |
3 | 5 | 11 | 5 | 3 | 27 |
1 | 5 | 4 | 11 | 6 | 27 |
2 | 4 | 16 | 3 | 1 | 26 |
7 | 4 | 8 | 3.5 | 1 | 23.5 |
1 | 6 | 10 | 4 | 1 | 22 |
5 | 5 | 3 | 5 | 4 | 22 |
4 | 5 | 9 | 2.5 | 1.5 | 22 |
0.5 | 6 | 9 | 4 | 2 | 21.5 |
1 | 6 | 7 | 2 | 4 | 20 |
2 | 2 | 12 | 3 | 1 | 20 |
11 | 5 | 1 | 1 | 1 | 19 |
2 | 4.5 | 6 | 3 | 3 | 18.5 |
5 | 2 | 5 | 3 | 3 | 18 |
2 | 6 | 5 | 2.5 | 0 | 15.5 |
1.5 | 4 | 3 | 3 | 2 | 13.5 |
0.5 | 1.5 | 10 | 0.5 | 1 | 13.5 |
3 | 2 | 3 | 4 | 1 | 13 |
1.5 | 0.5 | 8 | 1.5 | 0.5 | 12 |
1 | 1 | 2 | 4 | 2 | 10 |
1 | 2 | 5 | 0.5 | 1 | 9.5 |
1.5 | 2.5 | 4 | 0.5 | 0.5 | 9 |
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;
}
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
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?
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
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;
}
}
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
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 ) { ... }
}
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
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
}
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);
}
}
Cursor, Enumeration, Accessing Elements
An internal cursor provides the functionality of an enumerator
Why do both?
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. }
}
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 Name | Java Equivalent |
Create | Constructor |
change_value | setData( new value) |
right | getNext() |
change_right | setNext( new value) |
put_between | linkTogether( leftNode, rightNode) |
Eiffel Singlely Linked List
first | return value of first element |
value | return value of element at cursor |
change_value | set value of element at cursor |
start | move cursor to first element |
forth | move cursor to next element |
go_offleft | Move cursor off left edge |
duplicate | clone list |
insert_right | insert element to right of cursor |
insert_left | insert element to left of cursor |
delete | delete element at cursor, move cursor right |
delete_right | delete element to the right of the cursor |
delete_left | delete element to the left of the cursor |
delete_all_occurrences | delete all occurrence of argument from list |
wipe_out | empty the list |
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)