BasicDie
is back, as well as a version of our DieNode
class — the building block for our linked lists of die instances. You'll add code to LabCode
as you work through the exercises this week, and you can use some of the tools in ListBuilder
to help you construct and print some sample lists, but don't look at how those methods are implemented yet — I want you to figure out some of those techniques for yourselves first.
LabCode
. At the moment, it contains code that calls ListBuilder
's build
method to construct a three-node linked list (shown below), then calls printList
to print out the contents of the list (the BasicDie instances it contains). Run the code to see what it does.
ListBuilder
's build
method by clicking on the ListBuilder class. (You can pass it {6,4,8}
as an input, rather than having to construct the array of die sides first.) When the result is displayed, click the "get" button to save it to the object workbench (call it "head" since it'll be a reference to the first node in the list).
Once it's on the workbench, double-click the new DieNode
object and an inspection window will open, showing the internal state of the object. Both of the fields in the DieNode are object references, so they appear as arrows, but double-clicking on those arrows brings up inspector windows for those objects as well. Inspect all of the arrows until there are no more to follow and you'll get something like the display below. (I dragged the windows around a bit so it looked more like the picture above.) The take-away here is that if you've got a linked structure, you can explore all of it via pointing and clicking.
node2
points to the second node in the list as shown below. (You should leave the existing code the way it is, but add a new assignment statement after it such that the variable node2
refers to the second node.) Call printList again and pass it node2
and see what happens. What if you call printList on node1
?
node2
is then moved to point to the third node, as shown below. Call printList again and pass it node2
after this latest update and see what happens.
LabCode
's main method, comment out everything you added below the line that says "Add more code below this line". The goal is to go back to the initial code that built the three-node list as in Fig 1.1. (You can always grab a fresh copy of the lab project if you prefer.)
Once you've got the three-node list being constructed and printed properly, add code at the bottom of main that turns it into the two-node list shown below. That requires setting the second node's next
pointer to null
. Test your change by calling printList on node1
after the change. (What used to be the third node isn't shown in the picture below, but you're not responsible for making it disappear — all you need to do is cut it loose from the list by changing the second node's next
link.)
LabCode
if you want, or comment out the code in main and add your new code there. What will printList
do when passed node1
? Call printList
to test your hypothesis.
LabCode
, write your own static printList
method that works like the one I gave you. (Don't peek in ListBuilder
— try to write this yourself!) It should take a DieNode
reference as its argument, traverse the list, and print out each of the die objects as it goes. You should stop once you hit the null link at the end of the list, but make sure you print out the final die object before stopping.
LabCode
, write a get
method that takes a DieNode reference (the head of a list) and an integer index as its argument, and returns the die instance at the specified position in the list.
lotsOfDice
that takes two parameters, the number of dice required and the number of sides they should each have, and builds a linked list containing the specified number of die instances. (Each die in the list will have the same number of sides, but they should be separate instances — don't just have the list nodes all share a single die instance.)
> DieNode head = LabCode.lotsOfDice(3,8); > LabCode.printList(head); Die has 8 sides and has rolled 0 times Die has 8 sides and has rolled 0 times Die has 8 sides and has rolled 0 times
indexOf
method that takes the head of a list and a number of sides, and returns the first die instance in the list that has the specified number of sides.
printList
and get
methods.
lotsOfDice
method? Come up with a T(n) estimate of the number of computational steps required for a problem of size n. (In this case, what's the measure of the problem size?) Once you've got your T(n), give your Big-O estimate f(n) of the method's performance. Justify your answer: Find a constant c and an n0 such that T(n) <= c*f(n) for all n > n0.