Monday, April 11, 2011

Solutions for labs 6, 7, 8

Solutions: Lab 6, Lab 7, Lab 8

13 comments:

  1. I have been writing methods in my own generic linked list class, however I have encountered an issue: when attempting to implement a generic array to store data in within a method, Java issues a compiler error: Generic array creation. As such, I do not believe generic arrays are allowed in java (as is supported by online forums). Is it inappropriate to create an array of objects, then downcast it to the generic parameter? Or is there perhaps a more "elegant" solution to this problem?

    ReplyDelete
  2. After further tinkering, I have realized that if I define the array to hold node objects without using the generic parameter, the method works fine.

    ReplyDelete
  3. @6:38 You're right about Generic arrays not being allowed. Page 774 in our text mentions it in the pitfall in case you wanted to read about it.

    ReplyDelete
  4. For the Lab 8 (aka LinkedList.java), I was wondering if it's possible to use the following for the copy constructor..

    public LinkedList(LinkedList l)
    {
    this();
    for(int i = 0; i < l.size; i++)
    {
    this.add(i, l.getData(i));
    }
    }

    ReplyDelete
  5. I did almost the same thing as Ian K an I wanted to know if it was valid.


    try
    {
    for (int i = 0; i < l.size(); i++)
    {
    this.add(i, l.getData(i));
    }
    }
    catch (NullPointerException e)
    {
    this.head = null;
    }

    ReplyDelete
  6. Wait sorry I forgot to add in whether the first one was null so it would be...

    public LinkedList(LinkedList l)
    {
    this();
    if(l.head == null)
    {
    this.head = null;
    }
    else
    {
    for(int i = 0; i < l.size; i++)
    {
    this.add(i, l.getData(i));
    }
    }
    }

    ReplyDelete
  7. @Mohammad

    I seem to found a way around it. For instance, if I want to hold an array of Nodes, but nodes are defined in the class for a generic data type, I can still create an array of nodes without a generic specifier and it will hold nodes of any acceptable data type. If you can think of any issues resulting from this use, please share it.

    ReplyDelete
  8. Also sir,

    I have noticed that when you use the Node class as an inner class, you use reference it as LinkedList.Node... this does not work for generic classes. At least from what I can gather I generate a compiler error every time I try to compile "LinkedList.Node head" as a private instance variable for the head of the linked list; whereas "Node head" compiles perfectly fine and presents no issues as I can see. As such, with respect to the lab test, how should we approach these types of compatibility v.s. style issues?

    ReplyDelete
  9. Ian K's 3:01 solution would work. I went a little nuts with my solution.

    @6:40: This is an issue, but we didn't talk about it in class so don't worry about it. Actually, omitting "LinkedList" and just calling it Node is fine with me.

    ReplyDelete
  10. @6:13 Yes that right. Thanks for letting me know ^_^
    @Andrew Sir i was wondering if you expected of us to be able to program the methods in lab 8 that were given to us preprogrammed by you. I was wondering if you count those as too difficult or not.

    ReplyDelete
  11. I'm not going to make any more comments about what is and is not on the exam.

    ReplyDelete
  12. @Anyone Just wanted to get something straight. LinkedLists also start at position 0 right? So the head is 0, the next link is 1 etc... Thanks for the help.

    ReplyDelete
  13. Alright I saw my answer ^_^. "Changes the value of the data at the given position. Returns true if successful, false otherwise. Recall that position=0 refers to the head (first element) of the list."

    ReplyDelete