Solutions to other chapters:
Java Methods A & AB:
Object-Oriented Programming and Data Structures
Answers and Solutions to Exercises
Chapter 5
3. |
(a) |
import , public , class ,
extends , implements , private ,
int , super , new ,
this , void , if ,
else , static , false , true |
|
(c) |
MovingDisk , time , clock ,
g , x , y , r , sky , c , e , w ,
args |
4. |
(b) |
style |
|
(g) |
style (Java is case sensitive, so IF
and if are two different words.) |
6. |
|
The Java interpreter "throws an exception": Exception in thread "main" java.lang.NoSuchMethodError: main |
7. |
|
The parentheses are required by the syntax, but the braces are optional, since they contain only
one statement. |
9. |
|
public boolean badIndentation(int maxLines)
{
int lineCount = 3;
while (lineCount < maxLines)
{
System.out.println(lineCount);
lineCount++;
}
return true;
}
|
10. |
(a) |
F -- the compiler ignores indentation and
recognizes blocks through braces. |
|
(c) |
T -- such text represents literal strings. |
11. |
(a) |
The JFrame 's constructor that
sets the title bar is not called. The program runs,
but the title bar is empty. |
|
(b) |
Adding void confuses the
compiler: it now thinks
public void HelloGui()
{
...
}
is a method! Unfortunately, Java allows you to
give the same name to a class and a method in that class.
Since HelloGui 's constructor has been incapacitated,
the default constructor is used, which leaves the window blank.
This kind of bug can be very frustrating! |
|