Comp Sci AB question: Error in sample free response question?

<p>Was doing a sample free response test and I hit a question where it seems impossible to fulfill all the required postconditions. </p>

<p>Problem 3. (b), on pages 9-12 here:
<a href="http://www.collegeboard.com/prod_downloads/student/testing/ap/compsci_ab/ap06_frq_computer_sci_ab.pdf"&gt;http://www.collegeboard.com/prod_downloads/student/testing/ap/compsci_ab/ap06_frq_computer_sci_ab.pdf&lt;/a> </p>

<p>I'd copy and paste the text but my adobe reader does not want to seem to cooperate.</p>

<p>Anyways, basically the writer needs to implement two methods of a WaitingList which is a singly linked list class. The second (b) not yet made method when given another linked list and a number of nodes to copy, cuts the last n nodes to the current list.</p>

<p>The problems are that
a) the sizes of the two WaitingLists (as stored in the numNode variable) after moving the nodes must be kept correct.</p>

<p>There is no way I can find to modify the private numNode variable of the passed list. I ended up creating a method that allows the list to be resized but I was only told to make one method.</p>

<p>b) if one is told to move all the nodes from the second WaitingList the list must be made empty</p>

<p>once again there is no way to do this. The WaitingLists keep the references to their front nodes as private variables and there is no way to change these instance fields to null without creating a third method</p>

<p>Is there something I am missing, or is the problem flawed? Cause since this was on the 2006 test one would think that it would have a proper solution. Or are you allowed to create three methods in problems that only call on you to make one?</p>

<p>I think I remember my teacher saying that if you had private variables from the same class, you can still access them. In this case, I think you can just do other.numNodes and other.front to modify/access them.</p>

<p>Well, it’s theoretically possible to do both with just one method, although it’s not too pretty. </p>

<p>For example, you could define transferNodesFromEnd( WaitingList other, int num ) so that if num is positive, the method proceeds as planned and num is added to the size of the current array. After that is done, you call the method again, this time using other.transferNodesFromEnd( this, -num ). Implement the method so that if num is negative, the size is decremented by that amount; also, if abs(num) equals size(), then set the value of front to null. Essentially, it’s a very messy way to utilize one method to do the job of two methods. I bet you were supposed to “assume” there were other methods like setNull() and setSize(), though.</p>

<p>Edit: never mind, the poster above me is correct. That would seem to defeat the entire purpose of encapsulation, but it’s right. [Controlling</a> Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)](<a href=“JDK 24 Documentation - Home”>Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects))</p>

<p>That’s funny. Thanks both of you. You’re right about private, I thought that it meant that only methods of the same instance of a class could modify.</p>

<p>



class TheClassCaller {
    public static void main(String[] args) {
        TheClass classA = new TheClass(4);
        TheClass classB = new TheClass(5);
        classB.setOtherValue(classA, 10);
        System.out.println(classA.getValue());
    }
}</p>

<p>class TheClass {
    private int value;</p>

<pre><code>public TheClass(int aValue) {
    value = aValue;
}

public void setOtherValue(TheClass a, int num) {
    a.value = num;
}

public int getValue() {
    return value;
}
</code></pre>

<p>}


Returns 10. So the problem is possible just requires pretty ugly programming practices that I was unaware were allowed. Greatly appreciated.</p>