יום ראשון, 7 בנובמבר 2010

Chapter 8 Exercises



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.
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.

CD: I love APCS
   (84 minutes)
    FDaly
    tracks: 32
    Computers are the future
3
Draw an inheritance hierarchy for the people in your place of study.
 Jackover --> appartmet heads --> teachers --> students
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.

 The arrow connecting DVD to item was gone
5
Create a CD object.
Call some of its methods.
Can you call the inherited methods(for example setComment())?
What do you observe about the inherited methods?

 The inherit properly.
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.

 It went to the item class then back to the CD class. It inherited.
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.

  public class VideoGame extends Item
{
    private String gameName;
    private int numberOfPlayers;
    
    public VideoGame(String theTitle, int time)
    {
        super(theTitle, time);
        gameName = theTitle;
        numberOfPlayers = time;
    }

    /**
     * @return The artist for this CD.
     */
    public String getgameName()
    {
        return gameName;
    }

    /**
     * @return The number of tracks on this CD.
     */
    public int getNumberOfPlayers()
    {
        return numberOfPlayers;
    }
}
8
Order these items into an inheritance hierarchy: apple, ice cream, bread, fruit, food-item, cereal, orange, dessert, chocolate mousse, baguette.
 (apple, orange)--> fruit

(cereal, baguette)--> bread

(Chocolate mousse, ice cream)--> dessert

(Fruit, bread, dessert)--> food
9
In what inheritance relationship might a touch pad and a mouse be?
 (touch pad --> mouse)
10

 The square is a subclassof rectangle because of the fact that every square is a rectangle but not the vise versa.
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;
 In the third declaration
p1 = s1 student is a person

In the fifth declaration
s1 = phd1 PhDStudent is a student
12
Test your answers to the previous question by creating the classes mentioned in that exercise, and trying it out in BlueJ.
 It worked.
13
What has to change in the Database class when another item subclass (for example classVideoGame) is added?
Why?

 Nothing? it shouldn't affect anything.
14
Use the documentation of the standard class libraries to find out about the inheritance hierarchyof the collection classes. Draw a diagram showing the hierarchy.

collection --> Abstract collection--> AbstractList, AbstractSet

AbstractList --> ArrayList, Vector, AbstractSequentialList

AbstractSet --> HashSet, TreeSet

Hashset --> JobStateReasons, LinkedHashSet

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.
 Partially complete, had a few mistakes, 
16
Draw an inheritance hierarchy representing parts of a computer system (processor, memory, disk drive, CD drive, printer, scanner, keyboard, mouse, etc.)
 All are subclasses of processor I believe. They work independently being controlled by the 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?
 M is the the highest in the hierarchy, then its subclasses are O and X and finally underneath that is T.
18
Draw an inheritance hierarchy of AbstractListand all its (direct and indirect) subclasses, as they are defined in the Java standard library.

AbstractList --> AbstractSequentialList, Vector, Arraylist

AbstractSEquentialList --> LinkedList

Vector --> Stack

אין תגובות:

הוסף רשומת תגובה