[Page 97 (continued)]Exercises
Note For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.
Exercise 2.1 | Consider the transaction of asking your professor for your grade in your computer science course. Identify the objects in this transaction and the types of messages that would be passed among them. | Exercise 2.2 | Now suppose that the professor in the preceding exercise decides to automate the transaction of looking up student grades and has asked you to design a program to perform this task. The program should let students type in their names and ID numbers and the program then should display their grades for the semester, with a final average. Suppose there are five quiz grades, three exams, and two programming exercise grades. Identify the objects in this program and the type of messages that would be passed among them. (Hint: The grades themselves are just data values, not objects.) | | [Page 98] | Exercise 2.3 | In the RiddleUser class (Fig. 2.15), give two examples of object instantiation and explain what is being done. | Exercise 2.4 | Explain the difference between a method definition and a method call. Give an example of each from the Riddle and RiddleUser examples discussed in this chapter. | Exercise 2.5 | In the RiddleUser class (Fig. 2.15), identify three examples of method calls and explain what is being done. | Exercise 2.6 | Describe how the slogan "define, create, use" applies to the Riddle example. | Exercise 2.7 | An identifier is the name for a_________,_________, or a_________. | Exercise 2.8 | Which of the following would be valid identifiers?
int 74ElmStreet Big_N L$&%# boolean Boolean _number Int public Private Joe j1 2*K big numb
| Exercise 2.9 | Explain the difference between a class variable and an instance variable. | Exercise 2.10 | Identify the syntax error (if any) in each declaration. Remember that some parts of an instance variable declaration are optional.
private boolean even = 0 ; private String s = helloWorld ;
| Exercise 2.11 | Write declarations for each of the following instance variables.
A private boolean variable named bool that has an initial value of true. A public String variable named str that has an initial value of "hello." A private int variable named nEmployees that is not assigned an initial value.
| Exercise 2.12 | Identify the syntax error (if any) in each method header:
[Page 99]public static void Main (String argv[])
| Exercise 2.13 | Identify the syntax error (if any) in each assignment statement. Assume that the following variables have been declared:
public int m; public boolean b; public String s;
| Exercise 2.14 | Given the following definition of the NumberAdder class, add statements to its main() method to create two instances of this class, named adder1 and adder2. Then add statements to set adder1's numbers to 10 and 15, and adder2's numbers to 100 and 200. Then add statements to print their respective sums.
public class NumberAdder { private int num1; private int num2;
public void setNums(int n1, int n2) { num1 = n1; num2 = n2; } public int getSum() { return num1 + num2 ; }
public static void main(String args[]) { } }
| Exercise 2.15 | For the NumberAdder class in the preceding exercise, what are the names of its instance variables and instance methods? Identify three expressions that occur in the program and explain what they do. Identify two assignment statements and explain what they do. | | [Page 100] | Exercise 2.16 | Explain the difference between each of the following pairs of concepts.
A method definition and a method call. Declaring a variable of reference type and creating an instance. A variable of reference type and a variable of primitive type.
| Exercise 2.17 | Define a Java class named NumberCruncher that has a single int variable as its only instance variable. Then define methods that perform the following operations on its number: get, double, triple, square, and cube. Set the initial value of the number with a constructor, as was done with the instance variables in the Riddle class.
| Exercise 2.18 | Write a main() method and add it to the NumberCruncher class defined in the preceding problem. Use it to create a NumberCruncher instance with a certain initial value, and then get it to report its double, triple, square, and cube.
| Exercise 2.19 | Write a Java class definition for a Cube object that has an integer attribute for the length of its side. The object should be capable of reporting its surface area and volume. The surface area of a cube is six times the area of any side. The volume is calculated by cubing the side.
| Exercise 2.20 | Write a Java class definition for a CubeUser object that will use the Cube object defined in the preceding exercise. This class should create three Cube instances, each with a different side, and then report their respective surface areas and volumes. | Exercise 2.21 | Challenge: Modify your solution to the preceding exercise so that it lets the user input the side of the cube. Follow the example shown in this chapter's "From the Java Library" section. | Exercise 2.22 | Challenge: Define a Java class that represents an address book entry, EnTRy, which consists of a name, address, and phone number, all represented as Strings. For the class's interface, define methods to set and get the values of each of its instance variables. Thus, for the name variable, it should have a setName() and a getName() method. |
UML Exercises
Exercise 2.23 | Draw a UML class diagram to represent the following class hierarchy: There are two types of languages, natural languages and programming languages. The natural languages include Chinese, English, French, and German. The programming languages include Java, Smalltalk, and C++, which are object-oriented languages, FORTRAN, COBOL, Pascal, and C, which are imperative languages, Lisp and ML, which are functional languages, and Prolog, which is a logic language. | Exercise 2.24 | Draw a UML class diagram to represent different kinds of automobiles, including trucks, sedans, wagons, SUVs, and the names and manufacturers of some popular models in each category. | | [Page 101] | Exercise 2.25 | Draw a UML object diagram of a triangle with attributes for three sides, containing the values 3, 4, and 5. | Exercise 2.26 | Suppose you are writing a Java program to implement an electronic address book. Your design is to have two classes, one to represent the user interface and one to represent the address book. Draw a UML diagram to depict this relationship. See Figure 2.14. | Exercise 2.27 | Draw an UML object diagram to depict the relationship between an applet that serves as a user interface and three triangles named t1, t2, and t3. |
|
No comments:
Post a Comment