Skip to main content
Logo image

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

Section 2.9 Exercises

Exercises 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 , a , or a . (Answer in alphabetical order)

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
    
  • Big_N
  • Yes, many java style conventions would use Big_N as a class name since the first leter is captialized.
  • Boolean
  • Yes, Boolean is the identifier/class name for the wrapper class of the boolean primitive type, and it can be re-used as a variable identifier.
  • _number
  • Yes, starting with underscore is done in some coding conventions to signify a field name or a private field name.
  • Int
  • Yes, However Int is not the identifier/class name for the wrapper class of the boolean primitive type, instead Integer is used.
  • Private
  • Yes, many java style conventions would use Private as a class name since the first leter is captialized.
  • Joe
  • Yes, many java style conventions would use Joe as a class name since the first leter is captialized.
  • j1
  • Yes, as long as the numerical digit is not the first character, it is allowed as part of an identifier, so j1 is a legal identifier, and most Java coding conventions would have it be a variable, field, or method name.
  • public
  • Try again, public is a reserved access modifier word.
  • L$&%#
  • Try again, the only non-alphanumeric character allowed in identifiers is the underscore, _, so L$&%# is not allowed
  • boolean
  • Try again, boolean is a reserved word
  • int
  • Try again, int is a reserved word
  • 74ElmStreet
  • Try again, identifiers can’t start with a number
  • 2*K
  • Try again, 2*K is an expression, and identifiers can’t start with a number or have any symbol other than _ (underscore) in them.
  • big numb
  • Try again, big numb has a space in it, and identifiers can’t have any non-alphanumeric character other than _ (underscore) in them.

9.

Explain the difference between a class variable and an instance variable.

Declaring fields.

Identify the syntax error (if any) in each declaration. Remember that some parts of an instance variable declaration are optional.
10.
public boolean isEven;
11.
Private boolean isEven;
12.
private boolean isOdd
13.
public boolean is Odd;
14.
string S;
15.
public String boolean;
16.
private boolean even = 0;
17.
private String s = helloWorld;

Writing declarations.

Write declarations for each of the following instance variables.
18.
A private boolean variable named bool that has an initial value of true.
19.
A public String variable named str that has an initial value of "hello".
20.
A private int variable named nEmployees that is not assigned an initial value.

Method syntax.

Identify the syntax error (if any) in each method header:
21.
public String boolean()
22.
private void String ()
23.
private void myMethod
24.
private myMethod()
25.
public static void Main (String argv[])

Variable Assignment.

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;
26.
m = "86";
27.
m = 86;
28.
m = true;
29.
s = 1295;
30.
s = "1295";
31.
b = "true";
32.
b = false

33.

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.

34.

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.

35. Matching Concepts Objects.

Match the Object terms to definitions.
1 method definition the part of the code that contains the exe- cutable statements that the method performs 2 method call the act of calling the method to perform the task it is meant to perform. 3 declaring a variable of reference type Specifying an object type and an identifier for the object 4 creating an instance using the keyword new along with an object’s constructor. 5 primitive type variable an identifier that has memory for the actual value of the variable 6 reference type variable an identifier that has memory for the reference to the object that holds the values of the variable.

36.

Define a Java class named NumberCruncher that has a single int variable as its only instance variable(a.k.a. field). Then define methods that perform the following operations on its number: getVal, doubleVal, tripleVal, squareVal, and cubeVal that returns the result, but doesn’t modify the object. Set the initial value of the number with a constructor as was done with the instance variables in the Riddle class.

37.

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.

38.

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.

39.

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 length, and then report their respective surface areas and volumes.

40.

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. Specifically look at Listing 2.7.2

41.

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 get the values of each of its instance variables. Thus, for the name variable, it should have a getName() method. The constructor should take the name, address, and phone number as arguments.

42.

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.

43.

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.

44.

Draw a UML object diagram of a triangle with attributes for three sides, containing the values 3, 4, and 5.

45.

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.

46.

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.