IPL / Mr. Hansen

HW due 2/8/2011


1. Read pp. 29-33. This is not much reading, but it is very dense, and you may need to read it 2 or 3 times to understand it. Reading notes are required, as always.

2. Create a new Java project called Exercise3.2a (commands: File, New, Java Project, and type Exercise3.2a in the box labeled “Project name:”; finally, click the Finish button).

3. Within your new project, create a class wrapper called MethodPractice (commands: click on the “+” sign to the left of Exercise 3.2a in the Package Explorer tree, right-click on src, issue the command New followed by Class, type MethodPractice in the “Name:” box, put a checkmark in the public static void main(String[] args) box, and click the Finish button).

4. In the MethodPractice class, delete everything except the first and last line.

5. Type (or paste) the code from p. 33 into your class wrapper so that all of the code is contained within the class called MethodPractice.

6. DO NOT RUN THE PROGRAM YET. Instead, add line numbers as comments to the beginning of each nonblank line within the class wrapper. In addition to the “//” style of comments, which go at the end of a line, Java allows C-style comments that can go anywhere within a line. The start of a comment is slash-asterisk ( /* ), and the end of a comment is asterisk-slash ( */ ). When you have finished, your code should look exactly like this:

       

7. On your homework paper, write the number 6 followed by a description of what happens on line 6, then the next line with a description of what it does, and so on. When there is a jump to another method, indent your code so that you can tell that you have jumped to somewhere else but will continue with the next line when the other method(s) finish. Follow this style:

       6 -- Start of main.
       7 -- Get ready to print “No, I ” but do not print it yet.
       8 -- Jump to zoop method.
              1 -- Start of zoop.
              2 -- Jump to baffle method.
                     12 -- Start of baffle.
                     13 -- Get ready to print “wug” but do not print it yet. Pending: “No, I wug
                     14 -- Jump to ping.
                            16 -- Start of ping.
                            17 -- Print everything pending, plus a period: “No, I wug.”
                            18 -- End of ping. Return to baffle, which called ping.
                     15. End of baffle. Return to zoop, which called baffle.
              3 -- Get ready to print “You wugga” but do not print it yet.
[etc.]

As you can see, about half of this exercise has already been done for you. Copy the lines above into your homework paper, taking care to check everything for accuracy. Indentation is critical. Then, finish the job by writing the remaining lines documenting the sequence of execution. The last line in your writeup should look like this:

       11 -- End of main. Terminate execution.

8. DO NOT RUT THE PROGRAM YET. Instead, write down all of the predicted output from the program, exactly as you have worked it through in step 7 above. Put your prediction in your homework paper.

9. Now, run the program (Ctrl+F11). How close was your predicted output to the actual output of the program? Write a short paragraph explaining what you learned, especially if your prediction was not correct. Remember, we can learn more from incorrect predictions than from correct predictions!

10. As the final step in today’s homework, modify your program so that the ping method accepts a parameter, called blob, and prints the contents of blob instead of the quoted period that is currently being printed. In other words, you will change

       
public static void ping ()

to

       
public static void ping (String blob)

Also, you must change the line that prints a quoted period from

       
System.out.println (".");

to

       
System.out.println (blob);

Note that ping is called from only one place in your program. You must change the call to ping (which occurs in the baffle method) so that it contains a quoted parameter. For example, if you wanted to pass the word “foobar” as a parameter, you would change the line

       
ping ()

to

       
ping ("foobar")

In your homework paper, describe the output that results when the parameter is set to

(a) your birthdate (surrounded by double quotes, of course)
(b) the word “puke”
(c) a period
(d) a null string (indicated by two double quotes with nothing in between them)
(e) nothing at all, in other words, with the call in baffle being simply
ping ()
    Note: The parentheses with nothing in between indicate that you are passing no parameters.
(f) the word “zoop
(g) the word zoop without any quotes
(h) the Unicode hex constant 00BD (zero zero beleven dirteen), indicated by "\u00bd"