Chapter 5 Solutions - Objects First With Java
Chapter 5 Solutions - Objects First With Java
Chapter 5 Solutions - Objects First With Java
boolean
Exercise 5.11
if(input.equals("bye")) {
finished = true;
}
Exercise 5.12
Package: java.util
It generates random numbers
An instance is created by using one of the two constructors:
Random random = Random();
Random random = Random(long seed);
To generate a random integer:
random.nextInt();
Exercise 5.13
public RandomTester()
{
random = new Random();
}
Exercise 5.15
0,1,2,...,99,100
Exercise 5.16
public int throwDice()
{
return random.nextInt(6) + 1;
}
Exercise 5.17
public String getResponse()
{
int answer = random.nextInt(3);
if(answer == 0) {
return "yes";
}
else if(answer == 1) {
return "no";
}
else {
return "maybe";
}
}
Exercise 5.18
private ArrayList responses;
Exercise 5.19
public int getOneRandom(int max)
{
return random.nextInt(max) + 1;
}
Exercise 5.20
public int getOneRandom(int min, int max)
{
return random.nextInt(max - min + 1) + min;
}
Exercise 5.21
When you add more responses these also get shown randomly together with the existing
ones.
This is because the length of the list with responses is used when generating the random
number.
Exercise 5.23
Set<Map.Entry<K,V>> entrySet()
V get(Object key)
Set<K> keySet()
V put(K key, V value)
void putAll(Map<? extends K,? extends V> m)
V remove(Object key)
Collection<V> values()
}
public void enterNumber(String name, String number)
{
phoneBook.put(name, number);
}
public String lookupNumber(String name)
{
return (String) phoneBook.get(name);
}
}
Exercise 5.26
Both values stay in the map. HashMaps only uses the key to distinguish entries - not the
values.
Exercise 5.28
It returns null.
Exercise 5.30
phoneBook.size()
Exercise 5.28
Differences:
In a HashSet each object can only appear once in the set (because it is a Set). In a
ArrayList an Object can appear multiple times.
An ArrayList is ordered a HashSet is not ordered.
Exercise 5.33
You can use regular expression to define how a string should be split. Some
documentation on regular expressions in Java:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html#sum
Exercise 5.34
Using a HashSet guaranties that there are no duplicate elements, but it does not keep the
order of the words.
Exercise 5.36
It creates empty strings - which was probably not the intention. to fix it we could do
this:
See the modified start() method of SupportSystem in: Code 5.6 p. 124
and the modified generateResponses() in Responder in: Code 5.7 p. 124
Exercise 5.40
else {
//check if it is a synonym
Object synonym = synonymMap.get(word);
if(synonym != null) {
return (String) responseMap.get(synonym);
}
}
Keyword examples:
@author
@version
@param
@return
These keywords get special attention so they stand out in the documentation.
Exercise 5.48
You draw a line with the method: drawLine(int x1, int y1, int x2, int y2)
The method draw only draws the outline of a shape, whereas the fill method fills the
internal of the shape with a color.
Exercise 5.52
Exercise 5.53
HashSet is most suitable, because it guaranties that we only have one of each ball in the
collection. The HashMap could be used for this as well, but we do not need a map, so it
would be a bad choice.
Exercise 5.54
Exercise 5.55+5.56
Download: balls-inabox.zip
Exercise 5.58
public static final double TOLERANCE = 0.001;
The constants are used to index the positions of data in a string. It is a good use of
constants.
Exercise 5.60
You would only have to modify the values of the constants which are all defined in one
place. If these numbers had not been placed in constant fields, but instead used directly,
it would have required changes to several different parts of the code.
Exercise 5.61
Exercise 5.62
Strings are immutable and therefore can not be changed. The method that is called does
not change the instance 's' but returns a new object with the string in upper case. The
correct way to do it is:
The variable a and b contains values. When these values are passed as arguments to the
method, the values get copied into the variables i1 and i2. In the method, we then swap
the values of i1 and i2. This has no effect outside the method as i1 and i2 are local
variables in the method. After calling the method the variables a and b will still contain
the same values as before the method call.