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. |
Thursday, 21 October 2010
Chapter Five
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment