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.

No comments:

Post a Comment