Exercises


Note

The solutions to these exercises are in Appendix B.

  1. Which of the following are legal method names?

    1. $25

    2. 25$

    3. abc_

    4. _ABc

  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

  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

  4. Consider the following method declaration:

    xyz(double d)

    Which argument types can a caller pass into this method?

  5. Earlier in this chapter, 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.

  6. Earlier in this chapter, 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.




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