created 07/29/99

Chapter 8 Programming Exercises


These exercises are simple and you might suspect they are "busy work" — dumb exercises whose only purpose is to keep you busy. Actually, although these exercises are simple and somewhat tedious, their goal is to demonstrate the concept of primitive data type which is of crucial importance and well worth a bit of practice. Please do them.


Exercise 1 — Shortfall

The following program uses primitive data type short:

class Shortfall
{
  public static void main ( String[] args )
  {
    short value = 32;
    System.out.println("A short: " +  value);
  }
}

value in this program is a variable—a name for a section of memory that holds data using a particular data type. In this case value is the name for 16 bits of main memory that uses short to represent an integer. (The next chapter says much more about variables.) This program puts the value 32 into value. Then it writes out:

A short: 32

In other words, that line of the program examines the variable and writes out what it finds.

Your Job: Create a file called Shortfall.java that contains this program. (Copy and paste will greatly speed this up.) Compile and run the program. Check what it writes onto the screen. Now edit the program so that the 32 is changed to some other small number, say 356. Compile and run the program. Everything should be fine.

Next change the number to 35000 and try to compile and run the program. This number is too large to work with the data type short (in other words, it cannot be represented in 16 bits using data type short.) What happens?

Now edit the program (don't change the 35000) so that the data type is int. Compile and run the program. Is there a difference?

Click here to go back to the main menu.


Exercise 2 — Double Jeopardy

The following program uses primitive data type double:

class DoubleJeopardy
{
  public static void main ( String[] args )
  {
    double value = 32;
    System.out.println("A double: " +  value);
  }
}

In this program, value is the name for a variable that uses the double data type to represent floating point numbers. Recall that this data type uses 64 bits.

It is perfectly OK to use the name value in this and in the previous program. A variable name helps describe what you want the program to do. It does not permanently reserve part of computer memory for any particular use.

Compile and run the program. Does its output (what it puts on the screen) differ from the output of the the previous exercise?

Change the 32 to 32.0 and see if that makes a difference when you compile and run the program.

Now try to "break" the program. Look back in this chapter at the chart of primitive data types and the range of values they can hold. Change the "32" to a value that is too big for double. You may wish to use scientific notation for this.

Click here to go back to the main menu.


Exercise 3 — Exponential Explosion

The following program also primitive data type double. This program computes and writes out the value of exp( 32 ). This is the number "e" raised to the power 32. ("e" is the base of natural logarithms. Don't worry much about this. The point of the program is not the math but the floating point numbers.)

class ExponentialExplosion
{
  public static void main ( String[] args )
  {
    double value = 32;
    System.out.println("e to the power value: " +  Math.exp( value ) );
  }
}

Compile and run the program. Does it compile and run correctly? Now change the 32 to larger and larger numbers until something goes wrong.

Click here to go back to the main menu.


Exercise 4 — Character Assassination

The following program uses primitive data type char:

class CharAssassination
{
  public static void main ( String[] args )
  {
    char ch = 'A' ;
    System.out.println("A char: " +  ch );
  }
}

The variable ch is 16 bits of main memory that uses the char data type to represent characters. The a bit pattern that represents 'A' is placed in it. The program writes:

A char: A

Do the following:

Observe and explain what works and what does not work in the above.

Click here to go back to the main menu.


End of Exercises