Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 2.9 Exercises

  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.
  2. Now suppose the professor in the previous exercise decides to automate the transaction of looking up a student’s grade and has asked you to design a program to perform this task. The program should let a student type in his or her name and ID number and the program then should display his or her 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.)
  3. In the RiddleUser class (Listing 2.4.7), give two examples of object instantiation and explain what is being done.
  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.
  5. In the RiddleUser class (Listing 2.4.7), identify three examples of method calls and explain what is being done.
  6. Describe how the slogan “define, create, manipulate” applies to the Riddle example.
  7. An identifier is the name for a __________ , __________ , or a __________ .
  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
  9. Explain the difference between a class variable and an instance variable.
  10. Identify the syntax error (if any) in each declaration. Remember that some parts of an instance variable declaration are optional.
    1. public boolean isEven;
    2. Private boolean isEven;
    3. private boolean isOdd
    4. public boolean is Odd;
    5. string S;
    6. public String boolean;
    7. private boolean even = 0;
    8. private String s = helloWorld;
  11. Write declarations for each of the following instance variables.
    1. A private boolean variable named bool that has an initial value of true.
    2. A public String variable named str that has an initial value of "hello".
    3. A private int variable named nEmployees that is not assigned an initial value.
  12. Identify the syntax error (if any) in each method header:
    1. public String boolean()
    2. private void String ()
    3. private void myMethod
    4. private myMethod()
    5. public static void Main (String argv[])
  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;
    1. m = "86";
    2. m = 86;
    3. m = true;
    4. s = 1295;
    5. s = "1295";
    6. b = "true";
    7. b = false
  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[])
  15. For the NumberAdder class in the previous 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.
  16. Explain the difference between each of the following pairs of concepts.
    1. A method definition and a method call.
    2. Declaring a variable of reference type and creating an instance.
    3. A variable of reference type and a variable of primitive type.
  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.
  18. Write a main() method and add it to the NumberCruncher class defined in the previous 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.
  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.
  20. Write a Java class definition for a CubeUser object that will use the Cube object defined in the previous exercise. This class should create three Cube instances, each with a different side, and then report their respective surface areas and volumes. Challenge: Modify your solution to the previous 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. Challenge: Define a Java class that represents an address book entry, Entry, which consists of a name, address, and phone number, all represented as String s. 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. \secEXRHone{UML Exercises}
  21. 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.
  22. 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.
  23. Draw a UML object diagram of a triangle with attributes for three sides, containing the values 3, 4, and 5.
  24. 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.4.6.
  25. Draw an UML object diagram to depict the relationship between a program that extends Canvas, which serves as a user interface, and which draws three instances of a Triangle class named t1, t2, and t3.
You have attempted of activities on this page.