Tuesday, 2 November 2010

Chapter 8 8.1-8.6


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, here's the printed terminal:

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 comment part of the CD class is a field, and all the lsit() method does, in the database class, is print all the object fields.
3
Draw an inheritance hierarchy for the people in your place of study.
 Done:

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.

 It won't compile, because the class relies on many of the super-class methods to function. Also, if it would compile, it would still lack all the methods it shares with the CD class that were put into the parent class.
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?

 They work just as they do with the methods inside the class, it's as though there's no difference, only to select and call the method one must right click the object and go to a list of "inherited from Item" methods.
6
Set a breakpoint in the first line of the CD class'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.

 When the instance fields get initialized the process goes into the Item class to find the code needed to initialize the fields, and returns the CD instance with the required information, the field value.

Sunday, 31 October 2010

Chapter 5 35-48


35
Implement the final changes discussed in the text in your own version of the program.

A:
Done it.
36
Add more word/response mappings into your application. You should copy some out of the solution provided and add some yourself.
A:
System.out.println("You might want to just change you computer, it seems to be at the peak of its life");
System.out.println("Sorry sir, but this is not a playground, you may want to use more formal language");
System.out.println("Your insulting, and you expect help from us?");
System.out.println("Do you really think it's my fault this is not working?");
System.out.println("Your computer seems to be disfunctional, I recommend you buy a new one.");
37
Sometimes two words (or variations of a word) are mapped to the same response. Deal with this by mapping synonyms or related expressions to the same string, so that you do not need multiple entries in the reponse map for the same response.
A:
Done it.
38
Identify multiple matching words in the user's input and respond with a more appropriate answer in that case.
A:
Done, I'm still unsure, however, of how many times a user should say the word in order for it to become the more detailed response. In all cases however, I set it to five.
39
When no word is recognized, use other words from the user's input to pick a well-fitting default response: for example words like "who","why, "how"
A:
Done it, I simply threw back a question at the user. For example, "Why is this?" would return the response I made, "Why shouldn't it?".
40
Use BlueJ's Generate Documentation function to generate documentation for your techSupport project.
Examine it. Is it accurate?
Is it complete?
Which parts are useful, which are not?
Can you find any errors in the documentation?
 A:
It's not so complete, because I haven't documented everything, but for the parts that do have a default message from Barnes and Kolling, it seems pretty detailed, and complete. I see the purpose of @param and @version @author now. I'm unable to find any errors however.
41
Find examaples of javadoc key symbols in the source code of the TechSupport project.
How do they influence the formatting of the documentation?
A:
If key symbols like @param, appear in the source code of a project they become categories in which the text following their appearance is the test that appears in the category.

42
Find out about and describe other javadoc key symbols.
One palce you can look is the online documentation of Sun Microsystems' java distribution. It contains a documnt called  javadoc - The Java API Documnentation Generator.
In this document the key symbols are called javadoc tags.
 A:
Although many tags appear, the essential ones in my opinion are:
@link which inserts a link to another file or project.
and @author which describes and includes a section on where the project has come from.
43
Properly document all classes in your version of the TechSupport project.
A:
Done it. I included @param where needed, as well as @return, which explain the parameters of each method and return where I explain the results of the method.
44
Create a BallDemo object and execute the drawDemo() and bounce() methods.
Then read the BallDemo source code.
Describe, in detail, how these methods work.
 A:
Essentially, this project, in both methods, creates a few objects that work together in that they move and wait for each other to move. For example, the rectangle moves, then lets the line be drawn, then moves again. This all happens extremely fast, and together creates a series of movements which combine.
45
Read the documentation of the Canvas class. Then answer the following questions in writing, including fragments of Java code.
How do you create a Canvas?
How do you make it visible?
How do you draw a line?
How can you erase something?
What is the difference between draw() and fill()?
What does wait do?
 A:
a) You initialize it with a title, and/or, and two measurements of width and height, and/or a background color. However, you can omit everything but the title, and start it as a default like that.
b) When you instantiate a Canvas object, the terminal will automatically appear.
c) Call the drawLine() method where two points must be given in order for it to calculate a slope.
d) Use the erase() method, but enter the shape's name if you want to only erase a given object.
e) draw() simply makes the shape visible on the canvas, but fill() fills up the shape with a certain color. For example you can have an "open" circle, and fill it up with the fill() method.
f) wait() makes the shape wait for a specified amount of seconds before continuing.
46
Experiment with Canvas operations by making changes to the drawDemo() method of BallDemo. Draw some more lines, shapes and text.
A:
Done it. There's a whole spectrum of possibilities, but I chose to make a rectangle and move it to +50 on x, make it wait, then move it +300 on x to make it sort of materialize, move, wait, then leave.
47
Draw a frame around the canvas by drawing a rectangle 20 pixels inside the window borders. Put this functionality into a method called drawFrame() in the BallDemo class.
A:
Done, I'm only worrying that maybe the shapes will remove the fill of the rectangle after we call the drawDemo method...
48
Improve your drawFrame() method to adapt automatically to the current canvas's size.
To do this, you need to find out how to make use of an object of class Dimension.
 A:
I'm having trouble getting this one to work, i'll confront you with it during the day.

Monday, 25 October 2010

Chapter 5 29-33


What are the similarities and differences between a HashSet and an ArrayList?
A:
HashSet is a set of elements that have no duplicate, like a function in mathematics, while ArrayList is simply a list of elements.
The split() method is more powerful than it first seems from our example.
How can you define exactly how a string should be split?
Give some examples.
 A:
The split() method simply takes the parameter of a character which the user specifies in order to tell it where to begin and end the substrings that it will put in the array of Strings. For example if I were to say split(i) for "Hello, I'm Mike", the array would consist of the substrings "Hello, " "'m M" "ke". It actually skips over the character which you specify, another important fact.
How would you call the split() method if you wanted to split a string at either space or tab characters?
How might you break up a string in which the words are separated by colon (':') characters?
 A:
a) split( ) or split((tab key))
b) split(;)
What is the difference in result of returning the words in a HashSet compared with returning them in an ArrayList?
A:
In a HashSet the words are returned by using the HashSet key, while in the ArrayList, the words are returned by stating their index.
What happens if there is more than one space between words (for example: two or three spaces?
Is there a problem?
 A:
My hypothesis is that it would function as normal, for example if the string was "Mike  is  awesome" it would still return "Mike" "is" "awesome".

Sunday, 24 October 2010

Chapter Five 18-30

 
19
Implement the random-response solution discussed here in your version of the tech-support system.
Done as shown in the above pictures.
20
What happens when you add more (or fewer) possible responses to the responses list?
Will the selection of a random response still work properly?
Why or why not?
 A:
Of course, no matter the amount of responses, it will always respond. Only if you put more responses then it has a bigger range with which to give you a random reponse.
21
What is a HashMap?
What is its purpose and how do you use it?
Use the AP Java Subset documentation at the Duke Site.
 A:
   It is a type of map stores values with keys. That is, you can find a value using its key. However, it's not possible to find a key using a value, the only purpose of the key is to find that one value. Resembles an array list, just with a key.
22
Create a class MapTester. In it, use a HashMap to implement a phone book similar to the example in the text.
Implement the following two methods of class MapTester
        public void enterNumber(String name, String number)
        public String lookupNumber(String name)
 A:
   Done.  I had to make the second method void, because for some reason it wouldn't compile if I put string, and it works perfectly with void and printing.
23
What happens when you add an entry to a map with a key that already exists in the map?
A:
  The number gets replaced with the new number.
24
What happens when you add an entry to a map with a value that already exists in the map?
A:
  Nothing, there are just two values that are the same but have different keys.
25
How do you check whether a given key is contained in a map?
A:
You must use the containsKey() method from the HashMap class. You enter in a key and it returns true if it is contained. Otherwise it'll return false.
26
What happens when you try to look up a value, and the key doess not exist in the map?
A:
It simply returns null if the key doesn't exist, as it has no assigned value.
27
How do you check how many entries are contained in a map?
A:
To check how many entries are contained in a map simply use the size() method from the HashMap class.
28
Implement the changes discussed in the text in your own version of the TechSupport system.
Test it to get a feel for how well it works.
 A:
I'll complete this later, I don't have the book with me right now, because I thought I didn't need it since I read through the entire chapter already.
29
What are the similarities and differences between a HashSet and an ArrayList?
HashSet is a set of elements that have no duplicate, like a function in mathematics, while ArrayList is simply a list of elements.

Thursday, 21 October 2010

Chapter Five


1
Open and run the project tech-support-complete.
You run it by creating an object of class SupportSystem and calling its smart() method. Enter some questions you might be having with your software to try out the system. See how it behaves. Type "bye" when you are done.
You do not need to examine the source code at this stage. This project is the complete solution that we will have developed by the end of the chapter. The purpose of this exercise is only to give you an idea of what we plan to achieve.

A:


2
Investigate the String documentation. Then look at the documentation for some other classes.
The Duke University site has documentation for the AP Subset and is very useful for this course because you are not overwhelmed with the full Java library, below.
What is the structure of class documentation?
Which sections are common to all class descriptions?
What is their purpose?

A:
1) The structure of class documentation is as follows:
First it gives the class signature
A description of the class as a whole
Then it gives the fields and a summary of each, and how they function
A constructors list and summary of each
A method list and summary
Further field description

2) All the above sections are essentially present in each class descriptions, which makes it simple to follow how each class works when you search for one.

3) The purpose of class documentation is to give the user some guidance on many classes with which he may not have experience, or sections that he hasn't learned fully. For example, I use the string class on a day to day basis, yet I didn't know the string constructors and fields, and especially not ALL the methods. Class documentation is a good method to combat such problems.

3
Look up the startsWith() method in the documentation for String.
Describe in your own words what it does.
A:
    There are two startsWith() methods, one simply searchs if the String begins with the given prefix, and the other checks if it begins with the given prefix at a given index. That is to say, that in a string "Michael" startsWith("c", 3) would return true.
4
Is there a method in the String class that tests whether a string ends with a given suffix?
If so, what is it called?
and what are its parameters and return type

A:
Yes, quite logically, this method is named endsWith() and has the parameters of a String named suffix, where suffix is the ending character(s) of the string.

5
Is there a method in the String class that returns the number of characters in the string?
If so, what is it called?
and what are its parameters?
A:
Yes, it is called length() and takes no parameters.
6
If you found methods for the two tasks above, how did you find them?
Is it easy or hard to find methods you are looking for?
Why?
 I found them by thinking logically, there only a few words for each method, length() could either be that or size(), and endsWith() was obvious, as there was a method named startsWith(). Documentation is done in a very logically coherent order making it easy for its users.
7
Find the trim() method in the String class's documentation.
Write down the signature of that method.
Write down an example call to that method on a String variable called text.
What does the documentation say about the control characters at the begining of the String?

A:
1) public String trim()
2) System.out.println(text.trim())
3) It removes the white space from the beginning and end of the string.
8

9
Improve the code of the SupportSystem class in the tech-support1 project so that the case of the input string is ignored.
Use the String class's toLowerCase() method to do this.
Remember that this method will not actually change the string it is called on, but result in the creation of a new one with slightly different contents.
Why is this so?
 A:
Done. Because the string is a field in the InputReader class, and can only be gained through the SupportSystem class which uses the InputReader class, but can't be changed as it is a private field.
10
Find the equals() method in the documentation for class String.
What is the return type of the method?
 The return type of the equals() method is boolean.
11
Change your implementation to use the equals() method instead of startsWith().
A: Done

12
Find the class Random in the Java library documentation.
Which package is it in?
What does it do?
How do you construct an instance?
How do you generate a random number?
Note that you will probably not understand everything that is stated in the documentation. Just try to find out what you need to know.
 A:
1) It is in the util package
2) You write for example Random Michael = new Random(101)
     It's also possible to construct an instance without giving it any parameters.
3) To generate a random numbers you must call the nextInt() method. which will return a number between 0 and the seed field, unless specified with an actual parameter.
13
Try to write a small code fragment (on paper) that generates a random number using this class.
A:
public int random(int numberRange);
{
 Random Michael = new Random(numberRange);
 return Michael.nextInt();
}
14
Write some code to test the generation of random numbers. To do this create a new class called RandomTester.
In class RandomTester, implement two methods: printOneRandom() (which prints out one random number) and printMultiRandom(int howMany).
 A:
Done,
15
Find the nextInt() method in class Random that allows the target range of random numbers to be specified.
What are the possible random numbers that are generated when you call this method with 100 as its parameter?
A:
Any number between 0 to 99.
16
Write a method in your RandomTester class called throwDice() that returns a random number between 1 and 6 (inclusive).
A:
Done,
17
Write a method called getResponse() that randomly returns one of the strings "yes", "no" or "maybe".
A:
Done,
18
Extend your getResponse() method so that it uses an ArrayList to store an arbitary number of responses, and randomly returns one of them.
Done.