Saturday, 27 November 2010

Chapter 9

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?

It wont compile. There is a compilation error. It cannot access the fields or the methods of the superclass. Did this in class. 
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.

I predict that Cd and Dvd will be executed when we compile. I was wrong. It executes the print() method of the subclass. 
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?

 Done. 
super.print();
The super constructor tells the constructor  to execute the print() method of the superclass. 
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. We just needed to add the print() method of CD and Video.
5
Look up toString in the library documentation.
What are its parameters?
What is its return type?

 toString does not take any parameters. It is because it is a return type of 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.Class name is returned with a attherate sign which looks like this"@". But why?
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. 
This is the to print() method for the CD:

public void print()
{
System.out.print("CD: " + artist + ": ");
super.print();
System.out.println("tracks: "+ numberOfTracks);
}
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.

 Done. All the fields in Item are protected. But we need to organize how we print out the different items.
9
Implement a transporter room with inheritance in your version of the zuul project.
 Not applicable. Chapter 7? 
10
Discuss how inheritance could be used in thezuul project to implement a player and a monster class.
 NA
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?
 NA
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?

 To successfully compile, the complier gives the static type the same method that it is being called for. 
getName() method. -- This method has to be given to the device class.
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 is in printer will be executed.
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?

 Methods from the object class will be called because  all the objects in primitive inherit or extend to object class. String s will now store the result of st.toString().
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. The toString method is immediately returned. 
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());
}

 toString() of Student will be returned and printed. 
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();

This can only work if D is a subclass of T and directly inheriting from the subclass I think. 

Friday, 26 November 2010

Chapter 8 questions

1
Open the project dome-v1. It contains the classes exactly as they were discussed in the text.
Create some CD objects and some video objects.
Create a database object.
Enter the CDs and videos into the database, and then list the database contents.

 Done. The method lists the objects. 
2
Try the following.
Create a CD object.
Enter it into the database.
List the database.
You see that the CD has no associated comment.
Add a comment to the CD object on the object bench (the one you entered into the database).
When you now list the database again, will the CD listed there have a comment attached?
Try it. Explain the behavior you observe.

 The comments are attached. To print all the new information we have to use the new list() method. 
3
Draw an inheritance hierarchy for the people in your place of study.
Person
-Student (ES, MS, HS)
- Staff (Teacher, Other)
 
4
Open the project dome-v2. This project contains a version of the DoME application rewritten to use inheritance, as described in the text.
*Note that the class diagram displays the inheritance relationship.
Open the source code of the Video class and remove the "extends Item" phrase. Close the editor.
What changes do you observe in the class diagram?
Add the "extends Item" phrase again.

 Done the relationship of the subclass is not shown but when we add it, it goes back to normal.
5
Create a CD object.
Call some of its methods.
Can you call the inherited methods(for examplesetComment())?
What do you observe about the inherited methods?

 We can call the Item() methods as if they inherited from the CD class. 
6
Set a breakpoint in the first line of the CDclass's constructor.
Then create a CD object. When the debugger window pops up, use Step Into to step through the code.
Observe the instance fields and their initialization.
Describe your observations.

 A constructor is just like a method.It can be made to change the field of the CD, but it works like nay other method.
7
Open the dome-v2 project.
Add a class for video games to the project.
Create some video game objects and test that all the methods work as expected.

Done.
8
Order these items into an inheritance hierarchy: apple, ice cream, bread, fruit, food-item, cereal, orange, dessert, chocolate mousse, baguette.
Food item
-Dessert (ice cream, chocolate)
- fruit (apple, orange, cereal)
- bread (baguette)
9
In what inheritance relationship might a touch pad and a mouse be?
 Mouse and touch pad are sublclasses of input device.Also subclass of Cursormover. 
10

Each sqaure is a rectangle but not every rectangle is a square. Like wise, not all objects inherit the same qualities or methods from the subclass. 
11
Assume we have four classes: Person, Student, Teacher & PhDStudent. Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.
Which of the following assignments are legal and why?
Person p1 = new Student();
Person p2 = new PhDStudent();
PhDStudent phd1 = new Student();
Teacher t1 = new Person();
Student s1 = new PhDStudent();
s1 = p1;
s1 = p2;
p1 = s1;
t1 = s1;
s1= phd1;
phd1 = s1;
Person p1 = new Student() because a student is a person.
Person p2 = new PhDStudent(); because a phDStudent is a person.
Student s1 = new PhDStudent();
because a PhDStudent is a Student.
p1 = s1 because student is a person.
s1 = phd1 because a PhD student s a student.
12
Test your answers to the previous question by creating the classes mentioned in that exercise, and trying it out in BlueJ.
 Done. Got it all right.
13
What has to change in the Database class when another item subclass (for example classVideoGame) is added?
Why?

Yes, but we have to extend the VideoGame class.
14
Use the documentation of the standard class libraries to find out about the inheritance hierarchy of the collection classes. Draw a diagram showing the hierarchy.

Something to do with interferences. I am not sure. Will ask. 
15
Go back to the lab-classess project from chapter 1. Add instructors to the project. Use inheritance to avoid code duplication between students and instructors.
 Done. 
16
Draw an inheritance hierarchy representing parts of a computer system (processor, memory, disk drive, CD drive, printer, scanner, keyboard, mouse, etc.)
Computer Device
-Input (mouse, keyboard/touchpad, microphone)
- Output (monitor, printer, speakers)
- Internal (memory, processor)
17
Look at the code below. You have four classes (O,X,T and M) and a variable of each of these.
O o;
X x;
T t;
M m;
The following assignments are all legal.
m = t;
m = x;
o = t;
The following assignments are all illegal.
o = m;
o = x;
x = o;
What can you say about the relationships of these classes?
1)      T is subclass of M.
2)      X is subclass of M.
3)      T is subclass of O.
4)      NA
5)    NA
6)      NA
7)      NA


18
Draw an inheritance hierarchy of AbstractListand all its (direct and indirect) subclasses, as they are defined in the Java standard library.