0% found this document useful (0 votes)
3 views

Lecture 9- 18-Feb-2025

The document covers key concepts in Java programming, including enumerated types, which define a set of named constants, and wrapper classes that allow primitive types to be treated as objects. It also discusses autoboxing and unboxing for automatic conversion between primitive values and their corresponding wrapper objects, as well as using JOptionPane for GUI-based input and output. Additionally, it highlights the organization of Java's standard class library into packages and the use of the Math class for mathematical operations.

Uploaded by

L A A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 9- 18-Feb-2025

The document covers key concepts in Java programming, including enumerated types, which define a set of named constants, and wrapper classes that allow primitive types to be treated as objects. It also discusses autoboxing and unboxing for automatic conversion between primitive values and their corresponding wrapper objects, as well as using JOptionPane for GUI-based input and output. Additionally, it highlights the organization of Java's standard class library into packages and the use of the Math class for mathematical operations.

Uploaded by

L A A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Outline

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

• An object of a wrapper class can be used in any situation where a primitive


value will not suffice
• For example, some objects serve as containers of other objects
• Primitive values could not be stored in such containers, but wrapper
objects could be
3-51
Wrapper Classes methods
• Wrapper classes methods
• manages the associated primitive type
• Ex: Integer provides methods returning the int stored in it

• Some methods of the Integer class


• Integer (int value)
• Constructor: creates an new Integer object storing value

• float floatValue()
• returns the value of this integer as the float type

• static int parseInt (String str)


• Returns the int corresponding to the value in str
Wrapper Classes
• The wrapper classes often contain useful constants as well
• For example, the Integer class contains MIN_VALUE and MAX_VALUE
which hold the smallest and largest int values
Integer a = 5;
System.out.print (a.MAX_VALUE);
2147483647

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

• Unboxing is the reverse condition


• Integer obj2 = new Integer (69);
int num2;
num2 = Obj2; // automatically extracts the int value
Using Dialog Boxes for Input/Output
• Another way to gather input is to use a GUI
• such as JOptionPane offered by Java
• Which allows a programmer to use GUI for I/O
• Making I/O more efficient and the program more attractive

• 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

• The syntax to use the method is:


• str = JOptionPane.showInputDialog(stringExpression);
• A dialog appears on the screen
• prompting user to enter data
• Data is returned as a string and assigned to variable str

• The user enters the data in the white area


• Called text field
Syntax for showMessageDialog
• The syntax to use showMessageDialog is
• JoptionPane.showMessageDialog(null, messageStringExpression,
boxTitleString, messageType).
• messageStringExpression:evaluated and value appears in box
• boxTitleString: the title of the dialog box
• messageType: type of icon appearing in the dialog box
• JoptionPane.ERROR_MESSAGE
• JoptionPane.INFORMATION_MESSAGE
• JoptionPane.Question_MESSAGE
• JoptionPane.PLAIN_MESSAGE
• JoptionPane.WARNING_MESSAGE
Example
import javax.swing.*;

public class JOptionPaneExample {


public static void main(String[] args) {
// Get input from the user
String name = JOptionPane.showInputDialog("What is your name?");
System.out.println("Hello, " + name);

// Show a message with the input


JOptionPane.showMessageDialog(null, "Hello, " + name + "!");
}
}
Summary
• The new operator returns a reference to a newly created object.
• Multiple reference variables can refer to the same object.
• A class library provides useful support when developing programs.
• The Java standard class library is organized into packages.
• All classes of the java.lang package are automatically imported for every program.
• A pseudorandom number generator performs a complex calculation to create the illusion of randomness.
• All methods of the Math class are static, meaning they are invoked through the class name.
• The printf method in Java is used for formatted output to control how variables are displayed using format specifiers.
• Enumerated types are type-safe, ensuring that invalid values will not be used.
• A wrapper class allows a primitive value to be managed as an object.
• Autoboxing provides automatic conversions between primitive values and corresponding wrapper objects.
• JOptionPane, part of the javax.swing package, provides a GUI-based way to handle input and output in Java through its
showInputDialog method for user input and showMessageDialog method for displaying results.

You might also like