CS10, 09S, UCSB
H03: Homework 3 (Based on Horstman, Chapter 3) Total points: ?
Accepted: on paper, in lecture (1-1:50pm) on Wednesday, April 8th only.
No email submission allowed.
Name: (2 pts)______________________________ UCSBNetID (2 pts) _____________________
Section (2 pts) Circle one: Thu 4pm Fri 10am Fri 11am Fri noon
- (3 pts) Throughout Chapter 3, the textbook refers to the concept of encapsulation and talks about it being similar to the idea of a black box.
This notion of a black box is one that comes up frequently in discussions of Computer Science and Engineering. In your own words, describe what the author means by black box, and how that relates to the concept of encapsulation.
(Note: a couple of sentences is enough—I just need to see that you have the main idea. I don't need an entire "essay".)
- Java allows us to make methods either public or private. Similarly instance variables (also known as instance fields, or attributes) can be either public or private. But based on the priniciple of encapsulation (fill in the blanks with either public or private):
- (4 pts) Methods are most commonly _________________ (though in some cases, we may have _____________ methods).
- (2 pts) Instance variables should always be declared ______________ to conform with the principle of encapsulation.
- Section 3.3 describes
javadoc comments. Here is an example of a javadoc comment.
/**
Gets the current balance of the bank account.
@ return the current balance
*/
- (3 pts) Would this be a comment for an individual method, or for an overall class?
- (3 pts) How can you tell?
- The textbook (and your instructor) make the case that learning to write
javadoc comments—and developing the habit of putting them in your code—is an important step on the road to becoming a professional level software developer.
Here are some questions—keep your answers short and to the point.
- (3 pts) What can the javadoc utiltity do with all the
javadoc formatted comments you put in your code?
- (3 pts) What is one reason that—in a "real world" software development project—your co-workers and your boss will be happier with the code you write if it includes complete javadoc comments?
- (3 pts) What is one reason that it is helpful to include these comments for a personal project that you might want to release as "open source" (e.g. in a forum such as SourceForge, or the Apache software project?)
- (3 pts) Based on what you've read in the textbook, heard in lecture, and your answers to the questions above, what is an argument in favor of developing the habit of putting javadoc comments in all of your Java code, regardless of the context—i.e. even for individual assignments for CS10, CS20, etc?
- Section 3.7 discusses the important concept of the lifetime of variables in Java.
- (2 pts) The Java virtual machine includes a feature that recycles memory when the memory allocated for an object is no longer needed. What is the term used to refer to that feature?
- (3 pts) Describe the lifetime of variables that are local to a method, or are parameters of a method.
In other words:
When are such variables born (i.e. memory is allocated), and when do such variables die (i.e. memory is deallocated)?
- Section 3.7 also describes how variables are initialized in Java. How are instance variables initalized? Describe two ways in which this can happen:
- (3 pts) One way instance variables can get an initial value:
- (3 pts) A different way instance variables can get an initial value:
- Section 3.8 describes implicit and explicit method parameters
- (3 pts) Every method in Java (except static methods) has one implicit parameter. What is the name of that implicit parameter?
- (3 pts) Not every method in Java has explicit parameters. On page 97-98, you'll find the complete code listing for a class called
BankAccount.java. What method of this class is not a constructor, and has no explicit parameters?
End of H03