CS111 PreLab 11
Task 1: Object Diagrams
Consider the following class definition:
public class ListNode {
public int head;
public ListNode tail;
public ListNode (int h, ListNode x) {
this.head = h;
this.tail = x;
}
}
Following the guidelines given for drawing Object Diagrams in
Problem Set 8, Task 1,
draw the object diagram that results from the following sequence of statements:
ListNode L1 = new ListNode(1,
new ListNode(2,
new ListNode(3,
null);
ListNode L2 = new ListNode(4,
new ListNode(5,
null);
L2.tail.tail = L1.tail;
L1.tail.tail.head = L2.head;
ListNode L3 = L2.tail.tail;
L3.tail.tail = L1;
L2.tail.head = L1.head + 2*L2.tail.tail.tail.tail.head;
Keep in mind that Object Diagrams are not the same as the
Box-and-Pointer diagrams we have been using lately.
Task 2: Understanding Constructors
The core of the Card class being used in
Problem Set 8, Task 2 is shown below.
public class Card {
// Public Instance Variables
public int value;
public int suit;
// Public Constructors
// Creates a blank card with no value and no suit.
public Card () {
this.value = 0;
this.suit = 0;
}
// Creates a card with the specified value and suit.
public Card (int value, int suit) {
this.value = value;
this.suit = suit;
}
}
Here are some things to note about the Card implementation:
- The actual Card implementation also defines the constants and methods
in the Card contract (not shown above). Everything is in the file
Card.java
in the CardsAndHands folder of the problem set.
- Class variables (constants) can be referred to by
<class name>.<variable name>,
for example, Card.QUEEN or Card.HEARTS.
- There are two instance variables in this card implementation, value
and suit. Both instance variables are public.
- public instance variables can be accessed directly.
So, given a Card c, we can get the value of c by evaluating
the expression
c.value
and we can set the value of c by evaluating the statement
c.value = Card.EIGHT
- A default constructor has no parameters.
The default constructor should generally initialize all instance variables
to some valid default value.
- Other constructors may be written which have parameters and
set the value of the instance variables based on the value of the parameters.
Given the above Card implementation (i.e. with two public instance
variables value and suit), there are four possible
ways to write a constructor which lets us set the value and suit
of the Card when we create the card. The four implementations are
shown below:
// Version 1
public Card (int value, int suit) {
this.value = value;
this.suit = suit;
}
// Version 2
public Card (int value, int suit) {
value = value;
suit = suit;
}
// Version 3
public Card (int v, int s) {
this.value = v;
this.suit = s;
}
// Version 4
public Card (int v, int s) {
value = v;
suit = s;
}
Draw a JEM showing the execution of the following testCard method
using each of the implementations of the Card
constructor above.
public static void testCard () {
Card c = new Card(Card.TWO, Card.DIAMONDS);
}
Which constructors work and which don't, and why?