Exercises 2.2

 

For Exercises 1-16, determine if each is a valid Java identifier.  If it is not, give a reason.

1.      XRay   valid

2.      X-Ray   invalid; - is not a legal character in an identifier

3.      Jeremiah   valid

4.      R2_D2   valid

5.      3M   invalid; identifiers cannot begin with a number

6.      PDQ123   valid

7.      PS.175   invalid; . is not a legal character in an identifier

8.   x   valid

9.      4   invalid; identifiers cannot begin with a number

10. N/4   invalid; / is not a legal character in an identifier

11.    M&M   invalid; $ is not a legal character in an identifier

12.    Z_Z_Z_Z_Z_Z  valid

13.    night   valid

14.    ngiht   valid

15.    nite   valid

16.    to day   invalid; identifiers cannot contain spaces

 

 

For Exercises 17-21, write declarations for each variable.

17.    double item, number, job;

 

18.    int shoeSize;

 

19.    double mileage;

     short cost, distance;

 

20.  long alpha, beta;

     double root;

 

21.    String name;

     isAStudent boolean;

 

 

 

22-26.  For each variable in Exercises 17-21, tell what default value will be used to initialize it.

 

22.    double item, number, job;  -- all will be initialized to zero

 

23.    int shoeSize; -- will be initialized to zero

 

24.    double mileage;  -- will be initialized to zero

     short cost, distance; -- will be initialized to zero

 

 

25.  long alpha, beta; -- will be initialized to zero

     double root; -- will be initialized to zero

 

26.    String name; -- will be initialized to null

     isAStudent boolean; -- -- will be initialized to false

 

 

For Exercises 27-28, write declarations for each variable so that it has the specified type and initial value.

 

27.

int     numberOfDeposits = 0,

        numberOfChecks = 0;

 

double totalDeposits = 0.0,

       totalChecks = 0.0,

       serviceCharge = 0.25;

 

 

28.

char symbol_1 = ' ' , symbol_2 = ';' ;

boolean    debug = true;

 

 

For Exercises 29-32, write constant declarations to associate each name with the specified literal:

 

29.  const double RATE = 1.25;

 

30.  const double REGULAR_HOURS = 40.0, OVERTIME_FACTOR = 1.5;

 

31.  const int YEAR = 1776;

    const char FEMALE = 'F',

               BLANK = ' ';

 

32.  const int ZERO = 0;

    const char ASTERISK = '*',

    APOSTROPHE = '\'';

 

33. const int YEAR = 2001;

    const double MAXIMUM_SALARY = 99999.99;

    int prime, number;

    char initial;