Monday, 15 November 2010

CS Chapter 9.5-END

5
Look up toString in the library documentation.
What are its parameters?
What is its return type?

 Without being overriden, the toString() method does not take any methods and returns a string consisting of a representation of the object. This can be called by almost any instance, given it's a subclass of the Object class.
6
.... You can easily try this out.
Create an object of class Video in your project, and then invoke the toString() method from the Object submenu in the object's popup menu.

 Done:
 I can't upload pictures for some reason, the buttons to upload just won't work, I'll show you the issue tomorrow.
7
The version of print() shown in Code 9.1 produces the output shown in text figure 9.9.
Reorder the staements in the method in your version of the DoME project so that it prints the details as shown in text Figure 9.10.

Done:  
8
Having to use a superclass call in print() is somewhat restrictive in the ways we can format the output, because it is dependent on the way the superclass formats its fields.
Make any necessary changes to the Item class and to the print() method of CD so that it produces the output shown in text Figure 9.11.
Any changes you make to the Item class should be visible only to its subclasses.
Hint: You used to use protected fields to do this.

 I had an issue with this, I received an error after the method got overused. I'll talk to you about it next class:
9
Implement a transporter room with inheritance in your version of the zuul project.
 N/A Skipped Ch.7
10
Discuss how inheritance could be used in the zuul project to implement a player and a monster class.
N/A
11
Could (or should) inheritance be used to create an inheritance relationship (super-, sub-, or sibling class) between a character in the game and an item?
 N/A
12
Assume you see the following lines of code:
Device dev = new Printer();
dev.getName();
Printer is a subclass of Device. Which of these classes must have a definition of method getName() for this code to compile?

 The Device class must have a definition of the method getName(), however, the Printer class may have overrided the method, in which case the definition of the getName() method that will be used is the one in the Printer class.
13
In the same situation as in the previous exercise, if both classes have an implementation of method getName(), which one will be executed?
 As explained above, the Printer's version of the getName() method will be executed if it was overrided.
14
Assume you write a class Student, which does not have a declared superclass. You do not write a toString() method.
Consider the following lines of code.
Student st = new Student();
String s = st.toString();
Will these lines compile?
What exactly will happen when you try to execute?

 There is no reason why it should not work, as the first instantiation line is of the same type, and the toString method, as defined in the Object class, is one of the inherited methods of the Student class as it is not a primitive class. These lines of code will assign the details of the st Student object to a string, s.
15
In the same situation as the previous exercise (class Student, no toString() method), will the following lines compile?
Why?
Student st = new Student();
System.out.println(st);


16
Assume your class Student overrides toString() so that it returns the student's name. You now have a list of students. Will the following code compile?
If not, why not?
If yes, what will it print?
Explain in detail what happens.
Iterator it = myList.iterator();
while (it.hasNext())
{
        System.out.println(it.next());
}

 Yes, it will  compile, my only worry is that it's not calling the toString() method, and instead is simply printing a list of Student's names. This is obvious as there is no reference here whatsoever to any toString() method.
17
Write a few lines of code that result in a situation where a variable x has the static type T and the dynamic type D.
 T x = new D();
D y = new D();
ArrayList list = new ArrayList <T> ();
list.add(x);
list.add(y);
return list;

No comments:

Post a Comment