Lecture 9- 18-Feb-2025
Lecture 9- 18-Feb-2025
Creating Objects
The String Class
Packages
Formatting Output
Enumerated Types
Wrapper Classes
3-42
Enumerated Types
• Java allows you to define an enumerated type, which can then be
used to declare variables
• An enumerated type establishes all possible values for a variable of
that type
• The values are identifiers of your own choosing
• The following declaration creates an enumerated type called
Season
enum Season {winter, spring, summer, fall};
• Any number
Enumerated ofquite
types can be values
helpful can be listed
in situations in which you have a relatively small number of distinct values
that a variable can assume. For example, suppose we wanted to represent the various letter grades a student could
earn. We might declare the following enumerated type:
3-43
Enumerated Types
• Once a type is defined, a variable of that type can be declared
Season time;
and it can be assigned a value
time = Season.fall;
• The values are specified through the name of the type
• Enumerated types are type-safe – you cannot assign any value other
than those listed
3-44
Enumerated Types- Example
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,
SUNDAY
}
Day today = Day.MONDAY; // 'Day' is the enum type, 'MONDAY' is one of its constants
Enumerated Types- Ordinal and name methods
• Internally, each value of an enumerated type is stored as an integer, called its ordinal value
• The first value in an enumerated type has an ordinal value of zero, the second one, and so on
• However, you cannot assign a numeric value to an enumerated type, even if it corresponds to a
valid ordinal value
• The declaration of an enumerated type is a special type of class, and each variable of that type is
an object
• The ordinal method returns the ordinal value of the object
• The name method returns the name of the identifier corresponding to the object's value
• See IceCream.java (page 137)
3-46
public class IceCream
{
enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough}
// Creates and uses variables of the Flavor type.
public static void main(String[] args)
{
Flavor cone1, cone2, cone3;
cone1 = Flavor.rockyRoad;
cone2 = Flavor.chocolate;
System.out.println("cone1 value: " + cone1);
System.out.println("cone1 ordinal: " + cone1.ordinal());
System.out.println("cone1 name: " + cone1.name());
System.out.println();
System.out.println("cone2 value: " + cone2);
System.out.println("cone2 ordinal: " + cone2.ordinal());
System.out.println("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println();
System.out.println("cone3 value: " + cone3);
System.out.println("cone3 ordinal: " + cone3.ordinal());
System.out.println("cone3 name: " + cone3.name());
}
}
3-47
public class IceCream
{
enum Flavor {vanilla, chocolate, strawberry, fudgeRipple, coffee, rockyRoad, mintChocolateChip, cookieDough}
// Creates and uses variables of the Flavor type.
public static void main(String[] args)
{
Flavor cone1, cone2, cone3;
cone1 = Flavor.rockyRoad;
cone2 = Flavor.chocolate;
System.out.println("cone1 value: " + cone1);
System.out.println("cone1 ordinal: " + cone1.ordinal());
System.out.println("cone1 name: " + cone1.name());
System.out.println();
System.out.println("cone2 value: " + cone2);
System.out.println("cone2 ordinal: " + cone2.ordinal());
System.out.println("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println();
System.out.println("cone3 value: " + cone3);
System.out.println("cone3 ordinal: " + cone3.ordinal());
System.out.println("cone3 name: " + cone3.name());
}
}
3-48
Outline
Creating Objects
The String Class
Packages
Formatting Output
Enumerated Types
Wrapper Classes
3-49
Wrapper Classes
• The java.lang package contains wrapper
classes that correspond to each primitive type:
Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
void Void
3-50
Wrapper Classes
• The following declaration creates an Integer object which represents
the integer 40 as an object
Integer age = new Integer(40);
age 40
• float floatValue()
• returns the value of this integer as the float type
Because Some of the methods are static we can use the class name (
Integer.MAX_VALUE).
We don’t need to use “new”.
3-53
Autoboxing/Unboxing
• Autoboxing is the automatic conversion between
• Primitive value and corresponding wrapper object
• Integer obj1;
int num1 = 69;
Obj1 = num1; // automatically creates an Integer object
• JOptionPane
• Contained in the package javax.swing
• Offers two methods:
• ShowInputDialog: allows user to input a string from keyboard
• ShowMessageDialog: allows user to display results
Syntax for ShowInputDialog
• To display a dialog box
• Containing the string in the object stringExpression
• Storing the input data in the String object str