Section 2.9 Exercises
- 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.
- 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.)
- In the
RiddleUser
class (Listing 2.4.7), give two examples of object instantiation and explain what is being done. - Explain the difference between a method definition and a method call. Give an example of each from the
Riddle
andRiddleUser
examples discussed in this chapter. - In the
RiddleUser
class (Listing 2.4.7), identify three examples of method calls and explain what is being done. - Describe how the slogan “define, create, manipulate” applies to the
Riddle
example. - An identifier is the name for a
__________
,__________
, or a__________
. - 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
- Explain the difference between a
class variable
and aninstance variable
. -
Identify the syntax error (if any) in each declaration. Remember that some parts of an instance variable declaration are optional.
public boolean isEven;
Private boolean isEven;
private boolean isOdd
public boolean is Odd;
string S;
public String boolean;
private boolean even = 0;
private String s = helloWorld;
-
Write declarations for each of the following instance variables.
- A
private boolean
variable namedbool
that has an initial value oftrue
. - A
public String
variable namedstr
that has an initial value of "hello". - A
private int
variable namednEmployees
that is not assigned an initial value.
-
Identify the syntax error (if any) in each method header:
public String boolean()
private void String ()
private void myMethod
private myMethod()
public static void Main (String argv[])
-
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;
m = "86";
m = 86;
m = true;
s = 1295;
s = "1295";
b = "true";
b = false
- Given the following definition of the
NumberAdder
class, add statements to itsmain()
method to create two instances of this class, namedadder1
andadder2
. Then add statements to setadder1
’s numbers to 10 and 15, andadder2
’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[]) { } }
- 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. - 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.
- Define a Java class named
NumberCruncher
that has a singleint
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 theRiddle
class. - Write a
main()
method and add it to theNumberCruncher
class defined in the previous problem. Use it to create aNumberCruncher
instance, with a certain initial value, and then get it to report its double, triple, square, and cube. - 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. - Write a Java class definition for a
CubeUser
object that will use theCube
object defined in the previous exercise. This class should create threeCube
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 asString
s. For the class’s interface, define methods to set and get the values of each of its instance variables. Thus, for thename
variable, it should have asetName()
and agetName()
method. \secEXRHone{UML Exercises} - 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.
- 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.
- Draw a UML object diagram of a triangle with attributes for three sides, containing the values 3, 4, and 5.
- 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.
- 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 aTriangle
class namedt1
,t2
, andt3
.
You have attempted of activities on this page.