Solutions to other chapters:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  
Java Methods Home Page Skylight Publishing



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!

Copyright © 2006 by Skylight Publishing