Chapter 4


Exercise 1 Which of the following are legal method names?

  1. $25

  2. 25$

  3. abc_

  4. _ABc

Solution 1 A, C, and D are legal. B is illegal because a method name may not begin with a digit.

Exercise 2 Suppose you want to write a method that returns the diameter of a planet, in millimeters. Since it's your program, you can choose any name you like for the method. Rank the following method names, from worst to best. Use your own judgment as to what makes one method name better or worse than another.

  1. getPlanetDiameter

  2. getSize

  3. getPlanetDiameterMm

  4. getIt

  5. getPlanetSize

Solution 2 Good and bad are subjective. However, a method name that eliminates confusion must be considered better than one that creates confusion, or only eliminates a little confusion. Here are the method names, ranked by order of how much information each name conveys:

getIt getSize getPlanetSize getPlanetDiameter getPlanetDiameterMm

getIt tells you nothing at all about what the method does. It's unfortunate how many programmers use similar names and create code that is difficult to understand and expand. The other names tell increasingly more about the method's return value. "Diameter" is better than "Size", because Size might be diameter or radius or mass. "DiameterMM" tells us not only the quantity but the units.

Of course, there is a limit to how much a method name should say. The goal is not to maximize the information in the name. The goal is to maximize the usefulness of the name. A name that is too long to read easily, or hard to distinguish from a similar name, does not contribute. For example, getPlanetDiameterMMAsMeasuredByHubbleOnApril12003 is too informative, and it's hard to distinguish from getPlanetDiameterMMAsMeasuredByHubbleOnApril112003.

Exercise 3 Suppose a method has the following declaration:

static int abc(int x, short y)

Suppose this method is called as follows:

abc(first, second)

Which of the following are legal types for the variables first and second?

  1. int first, int second

  2. short first, short second

  3. byte first, char second

  4. char first, byte second

Solution 3 B and D are legal. A passed argument may be of any type, provided it is the same as, or narrower than, the type declared by the method. The first argument is declared by the method to be an int, so you can pass a byte, short, char, or int. The second argument is declared by the method to be a short, so you can pass a byte or a short.

Exercise 4 Consider the following method declaration:

xyz(double d)

Which argument types can a caller pass into this method?

Solution 4 A caller can pass any type that is the same as, or shorter than, the declared type. Since the declared type is double, the caller can pass a byte, short, char, int, long, float, or double.

Exercise 5 In Chapter 4, you learned that if method iAmVoid is void, you can't say int z = iAmVoid(); because there is no value to assign to z. What happens if you try? Write a program that does this experiment.

Solution 5 The following application tries to assign a void call to an int:

public class AssignVoid {   static void iAmVoid()   {     System.out.println("Hello");   }   public static void main(String[] args)   {     int z = iAmVoid();   } }

The compilation fails with the following message: "Incompatible types; found, void, required:int at line 10..." (Your actual message may vary, depending on where your compiler came from.)

Exercise 6 In Chapter 4, you saw the following method:

static void print3x(int x) {   x = 3*x;   System.out.println("3 times x = " + x); }

The following code prints out "Now z is 10", not "Now z is 30", because the method modifies its own private copy of the argument:

int z = 10; print3x(z); System.out.println("Now z is " + z);

Write a program that proves this.

Solution 6 The following code proves that the method modifies its own copy, leaving the caller's copy alone:

public class ProveCallByValue {   static void print3x(int x)   {     x = 3*x;     System.out.println("3 times x = " + x);   }   public static void main(String[] args)   {     int z = 10;     print3x(z);     System.out.println("Now z is " + z);   } }




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net