Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 3 ■ DATA TYPES


} else {
return NORTH;
}
}
}

Now that we have our completed enumeration, we need a test program to see how it works. Listing
3-17 shows a program class that does the job.

Listing 3-17. A test class for our enumeration

package com.bryantcs.examples.enumExample;

public class EnumExample {

public static void main(String[] args) {
int[] compassPoints = {22, 77, 144, 288};
for (int i = 0; i < compassPoints.length; i++) {
System.out.println(compassPoints[i] + " degrees is (very roughly) "
+ Direction.findCardinalDirection(compassPoints[i]));
}
for (Direction d : Direction.values()) {
System.out.println(d + " is " + d.getDegrees() + " degrees.");
}
}
}

EnumExample produces the output in the console shown in Listing 3-18.

Listing 3-18. EnumExample output

22 degrees is (very roughly) NORTH
77 degrees is (very roughly) EAST
144 degrees is (very roughly) SOUTH
288 degrees is (very roughly) WEST
NORTH is 0 degrees.
EAST is 90 degrees.
SOUTH is 180 degrees.
WEST is 270 degrees.

Summary


In this chapter, we learned the basics about the various data types that are available in Java. We found
out:


  • The size restrictions of the various numeric data types

  • How the boolean data type works

  • How char variables and String objects work and a bit about how they can interact

Free download pdf