Thursday, 4 November 2010

Barnes and Kolling Chapter Eight Questions

Barnes and Kolling Chapter Eight 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
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: Death Magnetic (70 mins)
    Metallica
    tracks: 12
    <no comment>

CD: Death Magnetic (70 mins)
    Metallica
    tracks: 12
    Great album


it seems to add the comment string to the database class, we may infer that database class responds to other class changes



3
Draw an inheritance hierarchy for the people in your place of study.
 Jonathan --> Daly--> Jackover--> WBAIS board--> Lord Almighty
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 is gone!

I added it back on, thank god!
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?

 The methods are inherited from other classes. 
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.

 The debugger took me to the item source code.  
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 are subclass of fruit

cereal, baguette are subclass of class bread

chocolate mousse, ice cream are subclasses of class dessert

all of these all subclasses of super classes food-item

9
In what inheritance relationship might a touch pad and a mouse be?
 touch pad-->mouse
10

 square -> rectangle

(every square is a rectangle but not every rectangle is a square)
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;

12
Test your answers to the previous question by creating the classes mentioned in that exercise, and trying it out in BlueJ.

13
What has to change in the Database class when another item subclass (for example classVideoGame) is added?
Why?


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.


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.

16
Draw an inheritance hierarchy representing parts of a computer system (processor, memory, disk drive, CD drive, printer, scanner, keyboard, mouse, etc.)

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?

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





















Sunday, 31 October 2010

Chapter 5 TEST

Part 1:


a&b) I have read the clients specifications and I believe I have a clear understanding of the clients requirements.
I am suppose to create a word guessing game in which a word is randomly chosen and the objective is to figure out the chosen word. The user will be given the length of the word and will have to input words of the same length to discover the word. After inputing a word the user will be told how many letters were correct.  Ex: if the word is PIG and the user puts in POT the output will show 1. In order to quit ot give up the user must type x for every character of the word.Ex: if the word is MAN the user must input XXX.

c) To make this we must have a Responder class which generates a response according to the users input. The responder class represents a response generator object. It is used to generate an automatic response. Since we must read the users input in order to respond, we must also create an InputReader Class. InputReader reads typed text input from the standard text terminal. The text typed by a user is then dividedinto words, and a set of words is provided. We also need a class called WordGuessingGame which implements and puts to use the other classe in order to create the game.

public class WordGuessingGame:

/** This class is implements a Word Guessing Game. The game communicates with the user via input/output messages in the terminal window.
*
* This class uses an object of class InputReader to read input from the user, and an
* object of class Responder to generate responses. It repeatedly
* reads input and generates output until the users wants to leave.


@ version 1.0
@author Jonathan The Beast Schory


public class Responder:



/**
 * The responder class represents a response generator object.
 * It is used to generate an response according to the InputReader class. the response will be generated according to the requirements listed in section a&b.
 * 
 * @author     Jonathan The Beast Schory
 * @version    1.0
 */




public class InputReader:

/**
 * InputReader reads typed text input from the standard text terminal. 
 * the reader will test the input and respond to it according to the requirements listed in section a&b.
 * 
 * @author     Jonathan The Beast Schory
 * @version    1.0
 */




public class InputReader




Part 2:



Discuss (in your blog) the possible uses of each of the following classes in the implementation of the project. You can refer to the class documentation at the Duke site for details of the classes.

a. HashMap

containsKey

public boolean containsKey(java.lang.Object key)

Description copied from interface: Map
Returns true if there is a value associated with key in this map, otherwise returns false.
Specified by:
containsKey in interface Map

Parameters:
key - is the key for which the map is queried as to the existence of an associated value
Returns:
true if there is a (key,value) pair in the map for the specified key, otherwise return false
Perhaps we can use hashmap to determine if the input contains characters from the word the user needs to guess.
*** I am still having trouble clearly understanding the methods and the independent use of hashmaps. Please talk to me next class so we can discuss and you could clarify some of my confusions.***



b. Random

the random class must be used in this project in order to generate a random word which the user must guess. 
For example:
Text File: "There was a young man from Gondwana, who developed a severe case of Pangea, try as he would, he never quite could, find his way back to good old Gondwana."
Word chosen at random (#5) man

c. HashSet

d String
Very important for this project since the whole game is based on guessing a certain word(string) by inputing other words(strings) in the input. We use strings and test them to determine how many characters of the users input are correct. We also use strings for the whole communication with the user. A welcome or Goodbye message are actually strings.