-
Notifications
You must be signed in to change notification settings - Fork 1
Module 12 Mastery Check
wolvesled edited this page Sep 21, 2013
·
1 revision
- Enumeration constants are said to be self-typed. What does this mean? Answer: enumeration are defined as class, so the constants are object type of the defining enumeration class.
- What class do all enumerations automatically inherit? Answer: Enum
- Given the following enumeration, write a program that uses values() to show a list of the constants and their ordinal values.
enum Tools { SCREWDRIVER, WRENCH, HAMMER, PLIERS } - The traffic light simulation developed in Project 12-1 can be improved with a few simple changes that take advantage of an enumeration's class features. In the version shown, the duration of each color was controlled by the TrafficLightSimulator class by hard-coding these values into the run() method. Change this so that the duration of each color is stored by the constants in the TrafficLightColor enumeration. To do this, you will need to add a constructor, a private instance variable, and a method called getDelay(). After making these changes, what improvements do you see? On your own, can you think of other improvements? (Hint: try using ordinal values to switch light colors rather than relying on a switch statement.)
- Define boxing and unboxing. How does autoboxing/unboxing affect these actions? Answer: boxing is wrap primitive types into corresponding object, unboxing do the opposite. autoboxing/unboxing do type conversion in place where corresponding types of primitive and object exchange value without need of manually wrap and using get value method. this create seamless way using primitive types and numeric objects.
- Change the following fragment so that it uses autoboxing.
Short val = new Short(123);Answer:Short val = 123; - In your own words, what does static import do? Answer: bring static members of classes that then no need for use it with full qualification.
- What does this statement do?
import static java.lang.Integer.parseInt;Answer: bring static method of Integer class into view, so you can call the method parseInt() without qualifying with Integer. - Is static import designed for special-case situations, or is it good practice to bring all static members of all classes into view? Answer: it's for special-case situations, not good for bring all static members of all classes into view because it will crowd the namespace or create unnecessary collision.
- An annotation is syntactically based on a/an ________________________ . Answer: interface
- What is a marker annotation? Answer: annotations that don't have parameters are called marker annotations.
- An annotation can be applied only to methods. True or False? Answer: false