## Section1.9Exercises

1. Fill in the blanks in each of the following statements.

1. A Java class definition contains an object's __________ and __________.

2. A method definition contains two parts, a __________ and a __________.

2. Explain the difference between each of the following pairs of concepts.

1. Single-line and multiline comment.

2. Compiling and running a program.

3. Source code file and bytecode file.

4. Syntax and semantics.

5. Syntax error and semantic error.

6. Data and methods.

7. Variable and method.

8. Algorithm and method.

9. Pseudocode and Java code.

10. Method definition and method invocation.

3. For each of the following, identify it as either a syntax error or a semantic error. Justify your answers.

1. Write a class header as public Class MyClass.

2. Define the init() header as public vid init().

3. Print a string of five asterisks by System.out.println("***");.

4. Forget the semicolon at the end of a println() statement.

5. Calculate the sum of two numbers as N - M.

4. Suppose you have a Java program stored in a file named Test.java. Describe the compilation and execution process for this program, naming any other files that would be created.

5. Suppose N is 15. What numbers would be output by the following pseudocode algorithm? Suppose N is 6. What would be output by the algorithm in that case?  0. Print N. 1. If N equals 1, stop. 2. If N is even, divide it by 2. 3. If N is odd, triple it and add 1. 4. Go to step 0. 

6. Suppose N is 5 and M is 3. What value would be reported by the following pseudocode algorithm? In general, what quantity does this algorithm calculate?

 0. Write 0 on a piece of paper. 1. If M equals 0, report what's on the paper and stop. 2. Add N to the quantity written on the paper. 3. Subtract 1 from M. 4. Go to step 1. 
7. Puzzle Problem: You are given two different length ropes that have the characteristic that they both take exactly one hour to burn. However, neither rope burns at a constant rate. Some sections of the ropes burn very fast; other sections burn very slowly. All you have to work with is a box of matches and the two ropes. Describe an algorithm that uses the ropes and the matches to calculate when exactly 45 minutes have elapsed.

8. Puzzle Problem: A polar bear that lives right at the North Pole can walk due south for one hour, due east for one hour, and due north for one hour, and end up right back where it started. Is it possible to do this anywhere else on earth? Explain.

9. Puzzle Problem: Lewis Carroll, the author of Alice in Wonderland, used the following puzzle to entertain his guests: A captive queen weighing 195 pounds, her son weighing 90 pounds, and her daughter weighing 165 pounds, were trapped in a very high tower. Outside their window was a pulley and rope with a basket fastened on each end. They managed to escape by using the baskets and a 75-pound weight they found in the tower. How did they do it? The problem is that anytime the difference in weight between the two baskets is more than 15 pounds, someone might get hurt. Describe an algorithm that gets them down safely.

10. Puzzle Problem: Here's another Carroll favorite: A farmer needs to cross a river with his fox, goose, and a bag of corn. There's a rowboat that will hold the farmer and one other passenger. The problem is that the fox will eat the goose if they are left alone on the river bank, and the goose will eat the corn if they are left alone on the river bank. Write an algorithm that describes how he got across without losing any of his possessions.

11. Puzzle Problem: Have you heard this one? A farmer lent the mechanic next door a 40-pound weight. Unfortunately, the mechanic dropped the weight and it broke into four pieces. The good news is that, according to the mechanic, it is still possible to use the four pieces to weigh any quantity between one 40 pounds on a balance scale. How much did each of the four pieces weigh? (Hint: You can weigh a 4-pound object on a balance by putting a 5-pound weight on one side and a 1-pound weight on the other.)

12. Suppose your little sister asks you to show her how to use a pocket calculator so that she can calculate her homework average in her science course. Describe an algorithm that she can use to find the average of 10 homework grades.

13. A Caesar cipher is a secret code in which each letter of the alphabet is shifted by N letters to the right, with the letters at the end of the alphabet wrapping around to the beginning. For example, if N is 1, when we shift each letter to the right, the word daze would be written as ebaf. Note that the z has wrapped around to the beginning of the alphabet. Describe an algorithm that can be used to create a Caesar encoded message with a shift of 5.

14. Suppose you received the message, “sxccohv duh ixq,” which you know to be a Caesar cipher. Figure out what it says and then describe an algorithm that will always find what the message said regardless of the size of the shift that was used.

15. Suppose you're talking to your little brother on the phone and he wants you to calculate his homework average. All you have to work with is a piece of chalk and a very small chalkboard—big enough to write one four-digit number. What's more, although your little brother knows how to read numbers, he doesn't know how to count very well so he can't tell you how many grades there are. All he can do is read the numbers to you. Describe an algorithm that will calculate the correct average under these conditions.

16. Write a header for a public applet named SampleApplet.

17. Write a header for a public method named getName.

18. Design a class to represent a geometric rectangle with a given length and width, such that it is capable of calculating the area and the perimeter of the rectangle.

19. Modify the OldMacDonald class to “sing” either “Mary Had a Little Lamb” or your favorite nursery rhyme.

20. Define a Java class, called Patterns, modeled after OldMacDonald, that will print the following patterns of asterisks, one after the other heading down the page:  ***** ***** ***** **** * * * * * *** * * * * ** * * * * * * ***** ***** 

21. Write a Java class that prints your initials as block letters, as shown here:

 ****** * * * * ** ** * * * * * * ****** * * * * ** * * * * * * * * * * * * * * * 
22. Challenge: Define a class that represents a Temperature object. It should store the current temperature in an instance variable of type double, and it should have two public methods, setTemp(double t), which assigns t to the instance variable, and getTemp(), which return s the value of the instance variable. Use the Riddle class as a model.

23. Challenge: Define a class named TaxWhiz that computes the sales tax for a purchase. It should store the current tax rate as an instance variable. Following the model of the Riddle class, you can initialize the rate using a TaxWhiz() constructor. This class should have one public method, calcTax(double purchase), which will return a double, whose value is purchases times the tax rate. For example, if the tax rate is 4 percent, 0.04, and the purchase is \$100, then calcTax() should return 4.0.

24. What is stored in the variables num1 and num2 after the following statements are executed?  int num1 = 5; int num2 = 8; num1 = num1 + num2; num2 = nmm1 + num2; 

25. Write a series of statements that will declare a variable of type int called num and store in it the difference between 61 and 51.

26. Modify the UML diagram of the Riddle class to contain a method named getRiddle() that would return both the riddle's question and answer.

27. Draw a UML class diagram representing the following class: The name of the class is Circle. It has one attribute, a radius that is represented by a double value. It has one operation, calculateArea(), which returns a double. Its attributes should be designated as private and its method as public.

28. To represent a triangle we need attributes for each of its three sides and operations to create a triangle, calculate its area, and calculate its perimeter. Draw a UML diagram to represent this triangle.

29. Try to give the Java class definition for the class described in UML diagram shown here.