Chapter 2


Exercise 1 According to Table 2.1, the maximum values for the byte and short data types are 127 and 32767, respectively. Use the Twos-Complement Lab animated illustration to verify this. Which byte and short bit patterns produce the maximum values? In general, which bit pattern produces the maximum value for a two's complement number of N bits?

Solution 1 The maximum-value byte is 01111111. The maximum-value short is 0111111111111111. The general formula is a leading 0 followed by all 1s.

Exercise 2 According to Table 2.1, the minimum values for the byte and short data types are -128 and -32768, respectively. Use the Twos-Complement Lab animated illustration to verify this. What byte and short bit patterns produce the minimum values? In general, what bit pattern produces the minimum value for a two's complement number of N bits?

Solution 2 The minimum-value byte is 10000000. The minimum-value short is 1000000000000000. The general formula is a leading 1 followed by all 0s.

Exercise 3 Launch the Twos-Complement Lab animated illustration by typing java TwosCompLab, set the data type to int, and set all the bits to 1. Then set the three bits on the right to 0. Compute the value. Do the same for the byte and short data types. What do you observe?

Solution 3 In each case, the result is -8.

Exercise 4 Launch the Floating-Point Lab animated illustration by typing java floating.FloatFrame. Set the rightmost bit to 1 and all other bits to 0. The value represented is 1.4E-45. Try changing various bits' values by clicking on them. Can you create a value that is smaller than 1.4E-45 but still greater than 0?

Solution 4 1.4E-45 is the smallest possible greater-than-zero float value. Table 2.2 says so. Changing any bits in the exponent part yields a bigger power of 2. Changing any bits in the fraction part yields a bigger fraction, unless you set all the fraction bits to 0, which represents an overall value of 0.

Exercise 5 Write a Java application that declares and assigns values to three int variables named x, y, and z. Print out all three values, separated by commas, on a single line.

Solution 5 The following application prints out the values, separated by commas:

public class Ch2Q5 {   public static void main(String[] args)   {     int x, y, z;     x = 10;     y = 20;     z = 30;     System.out.println(x + "," + y + "," + z);   } }

Exercise 6 White space means spaces, tabs, and line-break characters. Type in the VerySimple application from Chapter 2 (reproduced below) and experiment with inserting white space. Does anything change during compilation or execution if you insert extra spaces between public and class? What if you insert a line break between public and class? Can you find any adjacent words or symbols such that inserting white space between them changes compilation or execution?

public class VerySimple {   public static void main(String[] args)   {     double age;     age = 123.456;   } } 

Solution 6 White space between words and symbols has no effect on compilation or execution, unless you put the white space inside a literal string. In that case, of course, the literal string will be changed. This means that you can use white space to make your source code as readable as possible. This is discussed further in Chapter 3, in the section "White Space and Comments."




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