CS10, 09S, UCSB
H02: Homework 2 (Based on Horstman, Chapter 2) Total points: ?
Accepted: on paper, in lecture (1-1:50pm) on Friday, April 3rd only.
No email submission allowed.
Name: (2 pts)_________________________________ UCSBNetID (2 pts) ________________________________
- (10 pts) For each of the following, if it is a legal identifier, write ok. If not, indicate what rule of Java identifiers is violated.
| is identifier ok? | write ok, or explain why its not valid |
|---|
| |
| isString? |
|
| x |
|
| SpaceShip |
|
| 4mat |
|
| mat4drawing |
|
| _height_of_tower |
|
| height-of-tower |
|
| heightOfTower |
|
| sales tax |
|
| my.drawing |
|
- (8 pts) Every variable in Java has a name, a type and a value.
Write a Java statement that declares a variable with the name student, a type of String, and a value that is your name (as a java String).
For full credit, you must have everything in the right order, with proper capitalization and punctuation.
- (4 pts) Suppose you have a variable called total, of type int.
Write an Java assignment statement that changes the value of that variable. The new value should be 2 more than the old value.
(There are more questions on the next page, or the back of this one.)
- (4 pts) In Java, we often speak of situations where a class provides a method. What does this mean in the context of Java?
Explain in your own words.
In your explanation include a specific example of some method provided by either the PrintStream class, or the String class, and explain what it means that that class provides that method.
- (4 pts) In question 2, you declared a variable called name that contains your name, represented as a Java String object—that is, as an instance of the Java String class. Write a Java statement that declares a variable of type int, with the name len, and sets the value of the variable len to the length of your name, by invoking the length method on the name object.
- (3 pts) In Section 2.7, Horstman speaks of Accessor methods and Mutator methods.
Consider the translate method of the class java.awt.Rectangle, described in Section 2.8. Is this an accessor method or a mutator method?
- (4 pts) Section 2.10 speaks of object references. For example, the statement:
Rectangle box = new Rectangle(5, 10, 20, 30);
constructs a new Rectangle object, and makes the variable box a reference to that object.
What is actually stored inside the variable called box?
- (3 pts) In the code listing on p. 61, we see some import statements, indicating that "Graphics and Graphics2D are classes that are part of the java.awt package".
Based on this, what package would you infer that the class JComponent is a part of?
- This question will test whether you've understood a few basic concepts of Java up to this point (either from a previous course, or from careful reading of Chapters 1 and 2).
In the first code listing on p. 62, we see the line of code (line 24):
g2.draw(box);
In the context of this line of code:
- (3 pts) What class is draw a method of?
- (3 pts) What is the type of the parameter to the draw method?