Wednesday 10 November 2010

Chapter 5 Questions

1
Open and run the project tech-support-complete.
You run it by creating an object of class SupportSystem and calling its start() 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.

Done it. 
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?

They first describe the class, then thy list the fields followed by the constructors and then the methods. They all follow this similar structure. 

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

It has a parameter and it returns true when the parameter is the same as the starting of the string.
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?

endsWith()
this parameter is a string.
it returns a boolean.
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?

length()
It has no parameters.
It returns ints.
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?

You have to read the method names and their descriptions. 
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?

public String trim()
It takes out any spaces before and after 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?

Done. 
input= input.toLowerCase();
10
Find the equals() method in the documentation for class String.
What is the return type of the method?

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

java.util.Random 
It is used to generate random numbers. 
To use it, you create an object Random, then call the next() method.



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

They are randomly generated numbers. 
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?

The nextInt() method is overloaded. 
16
Write a method in your RandomTester class called throwDice() that returns a random number between 1 and 6 (inclusive).
Done. 
Public int throwDice()
{
    Random r = new Random();
    return r.nextInt(5)+1;
}
17
Write a method called getResponse() that randomly returns one of the strings "yes", "no" or "maybe".
public void getResponse()
    {
        int number = generator.nextInt(3);
        if (number == 0)
        System.out.println("yes");
        
        if (number == 1)
        System.out.println("no");
        
        if (number == 2)
        System.out.println("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.
public void getResponse()
    {
        ArrayList list = new ArrayList();
        list.add("yes");
        list.add("no");
        list.add("maybe");
        
        int number = generator.nextInt(list.size());
        System.out.println(list.get(number));
    }
19
Implement the random-response solution discussed here in your version of the tech-support system.
Done it.
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?

It will work like right now.
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 map's function is to match a key to its value. 
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)

Done
23
What happens when you add an entry to a map with a key that already exists in the map?
It allows us to create it. It returns the last number. 
24
What happens when you add an entry to a map with a value that already exists in the map?
It allows us to create it.
25
How do you check whether a given key is contained in a map?
if (phoneBook.get(name) != null)
{
System.out.println("This name already exists. Chose another name.");
}
26
What happens when you try to look up a value, and the key does not exist in the map?
It returns Null. 
27
How do you check how many entries are contained in a map?
method size()
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.

Done
29
What are the similarities and differences between a HashSet and an ArrayList?
Set can not have duplicates while Array Lists can. Set does not keep items in a specific order. 
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.

Not done. 
31
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?

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

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

It is accurate because it was written by the actual creators. I did not find any source of errors.  
41
Find examaples of javadoc key symbols in the source code of the TechSupport project.
How do they influence the formatting of the documentation?

@ version 1.0
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.

@exception. Dont know. 
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.



Create object using constructors and move it around. 
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?


Firstly call it constructor.
Call the setVisible() method.
Do the drawLine() method.
Call eraseRectangle method.
draw()
wait()
46
Experiment with Canvas operations by making changes to the drawDemo() method ofBallDemo. Draw some more lines, shapes and text.
Done. 
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.
Done. 
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.

Done. 
49
Change the method bounce() to let the user choose how many balls should be bouncing.
Done. It is easy. You create a For loop and store it in ArrayLists. 
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.
Done.
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.

Done.
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?

Done it. 
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.
Done. 

No comments:

Post a Comment