Question text in black, answers in blue.
Just make the class, as it says on the assignment.
I was thinking you would do it with an array, and if you
do you can reuse a lot of code from the ArrayStringLog class in DJW. But you
could do it either of those ways if you like. Note that you should still have
two constructors, one with a capacity and one without, because the user (our
driver program) is expecting that. But since both linked lists and ArrayLists
have unlimited capacity, it's ok that your StringBag never becomes "full", no
matter what its "capacity" is set to when it is created.
Note, though, that we tell you exactly what order the elements
should be in after a series of inserts and remove(k)'s, and your implementation
must respect that. This could take a bit of work to implement remove(k) for
a linked list, but that would be good practice.
Yes -- you should have your spaces and "\n"'s exactly where ArrayStringLog's toString method has them -- the difference is that you have no name. The driver we are about to release does not do a string-match on the output of toString, but the grading driver might do this.
I have a question about project #1. The issue arises here:
My question is about the bolded sentence.
I decided to use a linked list for this project because I would like to dip my toes into unfamiliar territory. Or I just like making my life harder than it needs to be. Anyway, from what I understand about linked lists, removal involves taking the link from the removed node and replacing it with the link of the node before it. ...right? Plucks out the node of interest-- but this process also seems to fill the gap automatically with the nodes that were adjacent to the one removed. Am I right in this assumption? If this is how it works, is that last statement in the project write-up really necessary for linked lists?
Note that this answer is relevant only to those who are attempting
to implement Project 1 with a linked list.
I tried to address this in lecture but I may not have been clear. If
you use a linked list, which
is your choice, this particular step leads to some complications. (But doing it to get practice with
LL's right away is a great idea for your own development.)
If you just removed the k'th element on at remove(k) and linked its successor to its predecessor,
this would have the same effect as sliding all the later entries one step forward.
The problem is that I have told you exactly how I want the other elements in the list
to move when you do a remove(k), and that simple action is not what I want.
So what you need to do is take the k'th element out, save it to return when you are done,
find the last element in the list,
remove it, and then patch the former last element into the gap in the list made by the k'th element.
Math.abs
and
%
to get the random index k to use for remove(
)
.
May we get this k by the simpler method used in Discussion #2, using
the nextInt(int)
method of the Random
object?
Yes, I encourage you to do this, and I have now changed the text on the project assignment page.
remove( )
and the remove(k)
methods behave the same way with respect to
filling the gap made by the missing element? Do they both return the element
that is removed?
Yes and yes. The most natural way to implement
remove( )
is to generate a random k in the correct range with the
Random
object, then call remove(k)
. This keeps
you from having to write the same code twice.
toString
method that printed the appropriate lines to
System.out
.
But the compiler demanded that I put in a return statement that I
didn't need. I changed the type of the toString method to
void
,
but then when I ran the driver (which prints StringBag objects)
I got output like StringBag@b06c6be
, which I don't
understand. Help!
First off, the job of the The reason for your funny output from the driver is worth working
through. The method toString
method is not to print anything, but to return a String
object. The caller of the toString
method may or may not
choose to print this string. That's why the method is typed
String
,
and why the compiler insists that it return a String. Look at the
toString
method at the bottom of page 87 of DJW. It
assembles a String, which appears on multiple lines when
printed
because it has "\n"'s in it, and then returns it.
System.out.println
expects a
String
as its parameter. If it gets an object instead, it runs that object's
toString
method to get a String
, then takes
that
String
. Every object has a toString
method, because that method is defined in the Object
class and every class of objects extends that class. Usually we write
our own toString
method and thus override the one in
Object
.
But when you defined a void toString
method, that
no longer overrode the other method because they had different return
types. So when the driver tried to print your StringBag
object, it called the toString
method of
Object
, which (as you saw) makes a string out of the
object's class and its location in memory, which is given in hex and
so has digits and letters from a through f.
Last modified 23 September 2012