Here is the code: http://homepage.divms.uiowa.edu/~jones/object/notes/24code.txt
ID: 3808439 • Letter: H
Question
Here is the code:
http://homepage.divms.uiowa.edu/~jones/object/notes/24code.txt
Thanks
In the code distributed on March 22, in class Intersection, two new "getter" methods have been added, outgoingsize() and outgoing Get(). There is also a public setter' method, addoutgoing(). These three methods operate on the final private list outgoing a) This means that we have public 'setter' and 'getter' methods to operate on outgoing. That suggests that outgoing might as well be public. Give examples of operations on outgoing that are forbidden to the public as a result of forcing the public to use these methods. b) But the list outgoing is already declared as final. This would seem to imply that outgoing is read-only. Give an example of an operation on outgoing that is forbidden by declaring it to be final, and give some examples of operations that change outgoing that are still permitted despite its being final.Explanation / Answer
Answer:
a) Since outgoing is itself a private variable,, we are forbidden to directly access it from an instance of the Intersection class. So something like this is forbidden on outgoing and gives a compiler error:
Intersection intersection = new Intersection();
intersection.outgoing.add(Road r); //This statement gives a compiler error, because outgoing is private intersection.outgoing.get(int i); //Another example of a forbidden operation on outgoing
b) Declaring the variable outgoing as final, ensures that once defined/instantiated the value of the variable outgoing can no more be changed. But the list which is assigned to outgoing variable, can itself be changed by adding or removing elements from it.
Hence an example of a forbidden operation on outgoing, because of it being final is:
// defined to be a final variable, hence it can't be reinstantiated
Operations that change outgoing even though its final:
-Methods such as addOutgoing(Road r) and removeOutgoing(int element) actually change outgoing by adding or removing elements to the linked list. But they are permitted and work fine if outgoing has been defined already.