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

Chapter 5 Exercises



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

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?

3
Look up the startsWith() method in the documentation for String.
Describe in your own words what it does.

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?

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?

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?

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?

8

9
Improve the code of the SupportSystem class in the tech-support1 project so that thecase 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?

10
Find the equals() method in the documentation for class String.
What is the return type of the method?

11
Change your implementation to use the equals() method instead of startsWith().
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.

13
Try to write a small code fragment (on paper) that generates a random number using this class.
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).

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?

16
Write a method in your RandomTester class called throwDice() that returns a random number between 1 and 6 (inclusive).
17
Write a method called getResponse() that randomly returns one of the strings "yes", "no" or "maybe".
18
Extend your getResponse() method so that it uses an ArrayList to store an arbitary number of responses, and randomly returns one of them.
19
Implement the random-response solution discussed here in your version of the tech-support system.
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?

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.

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)

23
What happens when you add an entry to a map with a key that already exists in the map?
24
What happens when you add an entry to a map with a value that already exists in the map?
25
How do you check whether a given key is contained in a map?
26
What happens when you try to look up a value, and the key doess not exist in the map?
27
How do you check how many entries are contained in a map?
28
Implement the changes discussed in the text in your own version of the TechSupportsystem.
Test it to get a feel for how well it works.

29
What are the similarities and differences between a HashSet and an ArrayList?
30
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.

31
How would you call the split() method if you wanted to split a string at either space or tabcharacters?
How might you break up a string in which the words are separated by colon (':') characters?

32
What is the difference in result of returning the words in a HashSet compared with returning them in an ArrayList?
33
What happens if there is more than one space between words (for example: two or three spaces?
Is there a problem?

34
Read the footnote about the Arrays.asList() method. Find and read the sections in this book about class variables and class methods.
Explain in your own words how this works.
What are examples of other methods the Arrays class provides?
Create a class called SortingTest. In it, create a method that accepts an array of int values as a parameter, and prints out the elements sorted  (smallest element first) to the terminal.

35
Implement the final changes discussed in the text in your own version of the program.
36
Add more word/response mappings into your application. You should copy some out of the solution provided and add some yourself.
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.
38
Identify multiple matching words in the user's input and respond with a more appropriate answer in that case.
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"
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?

41
Find examaples of javadoc key symbols in the source code of the TechSupport project.
How do they influence the formatting of the documentation?

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.

43
Properly document all classes in your version of the TechSupport project.
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.

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?

46
Experiment with Canvas operations by making changes to the drawDemo() method ofBallDemo. Draw some more lines, shapes and text.
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.
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.

49
Change the method bounce() to let the user choose how many balls should be bouncing.
50
Which type of collection (ArrayList, HashMap or HashSet) is most suitable for storing the balls for the new bounce() method?
Discuss in writing, and justify your choice.

51
Change the bounce() method to place the balls randomly anywhere in the top half of the screen.
52
Write a new method named boxBounce(). This method draws a rectangle (the "box") on screen, and one or more balls inside the box. For the balls do not use BouncingBall, but create a new class BoxBall that moves around inside the box, bouncing off the walls of the box so that it always stays inside.
The initial position and speed of the ball should be random.
The boxBounce() method should have a parameter that specifies how many balls are in the box.

53
Give the balls in boxBounce() random colors.
54
In class BouncingBall, you will find a definition of gravity. Increase or decrease the gravity value, compile and run the bouncing ball demo again.
Do you observe a change?

55
There is a rumor circulating on the Internet that George Lucas uses a formula to create the names for the characters in his stories:
Your Star Wars first name:
1. Take the first 3 letters of you last name.
2. Add to that the first 2 letters of your first name.
Your Star Wars last name:
1. Take the first 2 letters of you mother's maiden name.
2. Add to this the first 3 letters of the name of the town or city that you were born in.
Create a new BlueJ project named star-wars. In it create a class named NameGenerator. This class should have a method named generateStarWarsName() that generates a Star Wars name using the method described above.
You will need to find out about a method of the String class that generates a substring.

56
The following code fragment attempts to print out a string in upper-case letters:
public void printUpper (String s)
{
        s.toUpperCase();
        System.out.println(s);
}
This code however does not work.
Find out why and explain.
How should it be written properly?

57
Assume we want to swap the values of two integer variables, a and b.
To do this we need to write a method.
public void swap (int i1, int i2)
{
        int tmp = i1;
        i1 = i2;
        i2 = tmp;
}
Then we can call this method with our a and b variables:
        swap(a,b);
Are a and b swapped after this call?
If you test you will notice that they are not!
Why does this not work?
Explain in detail.

אין תגובות:

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