Monday, November 15, 2010

Chapter 9 questions

1

Open your last version of the DoME project.

Remove the print() method from class Item and move it into the Video and CD classes.

Compile. What do you observe?

Won't compile. That's because it cannot access the fields in superclass.

2

In your DoME project, add a print() method in class Item again. For now write the method body with a single statement that prints out only the title. Then modify the print() methods in CD andVideo so that the CD version prints out only the artist and the Video version prints only the director. This removes the other errors ecountered above.

You should now have a situation coressponding to figure 9.4 in the text, with print() methods in three classes. Compile your project. This design should work, if there are errors remove them.

Before executing, predict which of the print()methods will get called if you execute theDatabase list() method.

Try it out: Enter a CD and a video into the database and call the Database list() method. Which print() methods were executed?

Was your prediction correct?

Try to explain your observations.

Predicting - CD and DVD will be called when we execute the method.


I was right.



3

Modify your latest version of the DoME project to include the super call in the print() method.

Test it.

Does it behave as expected?

Do you see any problems with this solution?

super.print();

Works.


4

Change the format of the output so that it prints the string "CD: " or Video: " (depending on the type of item) in front of the details.

Done, all i had to do was have it print "CD:" and then tab before all the other prints.

5

Look up toString in the library documentation.

What are its parameters?

What is its return type?

No parameters and return type is string.

6

.... You can easily try this out.

Create an object of class Video in your project, and then invoke the toString() method from theObject submenu in the object's popup menu.

Done!

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.

To print method in CD:


public void print()
{
System.out.print("CD: " + artist + ": ");
super.print();
System.out.println("tracks: "+ numberOfTracks);
}


To print method in Item:

System.out.print(title);
if (gotIt) {
System.out.println("*");
}
else {
System.out.println("");
}
System.out.println(playingTime + " minutes");
System.out.println(comment);

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.

Fields in item are protected now.

9

Implement a transporter room with inheritance in your version of the zuul project.

10

Discuss how inheritance could be used in thezuul project to implement a player and a monster class.

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?

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 methodgetName() for this code to compile?

Either of those.

13

In the same situation as in the previous exercise, if both classes have an implementation of methodgetName(), which one will be executed?

The one that's in printer.

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?

Yes! Method from object class will be called.

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);

Yes of course because toString method is called automatically.

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());

}

17

Write a few lines of code that result in a situation where a variable x has the static type T and thedynamic type D.

T x = new D();





No comments:

Post a Comment