Transcript object
Java Programming
Objects
Java is an object-oriented programming
language
– use objects to define both the data type
and the operations that can be applied to
the data
Objects have attributes and functionality
– attributes describe the state of the object
– the functionality of an object is the set of
actions the object can perform
In Java, we define an object’s attributes using
variables and its functionality using methods
Example: “Real-world” objects
Suppose we want to describe a car in terms
of its attributes and functionality
Attributes:
– tires, steering wheel, doors, engine
– model, make, year, color
– attributes can have constant variables
• all cars have a steering wheel
Actions:
– brake, stop, drive, reverse, park
Java classes
Java objects are created using classes
Encapsulation
– combining elements to create a new entity
A class encapsulates the variables and
methods that define an object
Instantiation
– the act of creating an object
– objects are called class instances
Java provides many predefined classes
You can (and will!) define your own classes
Java String class
The String class represents character
strings
String firstname = “Tammy”;
String lastname = “Bailey”;
Strings are basically an array of characters
Strings can be concatenated (added
together) using the concatenation operator +
String fullname =
firstname + “ ” + lastname;
Instantiation
Creating an object is called instantiation
– the new operator is used with class name
Example: Create a TextField object
TextField t = new TextField();
Can create multiple instances of the same class
TextField t1 = new TextField();
TextField t2 = new TextField();
Exception: The new operator is not required
when creating a String
Java TextField class
The TextField class allows the editing and
display of a single line of text
TextField t = new TextField();
Methods
– setText(String s)
• set the text of the field to the string s
– String getText()
• get the text of the field and assign it to a
variable of type string
Invoking an object’s methods
Once we create a text field, we can perform
actions on it using its methods
The variables and methods of an object are
accessed using the dot operator
TextField t = new TextField();
t.setText(“Hello”);
Syntax
– object.verb( data );
– Perform verb on object using data
Interactive objects
User interaction determines the behavior of the
program
Program receives user input through mouse
and keyboard and performs associated method
or action
Text fields
– edit and display single line of text
Buttons
– can specify action to occur when button is
clicked
Action listeners
If we want a button to know when it is clicked,
we have to enable it to “listen” for user input
This is done using the button method
addActionListener
Button b = new Button(“click!”);
b.addActionListener(this);
If we don’t invoke the addActionListener
method on a button, nothing will happen
when the button is clicked
Example
We would like our applet to do the following:
– get text from text field t1 and display it in
text field t2 when button b is clicked
TextField t1 = new TextField();
TextField t2 = new TextField();
Button b = new Button(“copy text”);
b.addActionListener(this);
Actions
We specify actions to occur when a button is
clicked in the actionPerformed method
public void actionPerformed(ActionEvent event)
{
Object cause = event.getSource();
if(cause == b)
{
t2.setText(t1.getText());
}
}
Numeric input
Suppose we want an applet that allows the
user to enter two integers and display the
minimum
– A text field contains a string
– If we want to perform numeric operations
on the input from a text field, we have to
convert the string to a numeric data type
– numbers are primitive data types, not
objects
Can convert using Java type wrappers
Type wrappers
Convert primitive types into objects
Primitive Type
byte
short
int
long
float
double
char
boolean
Wrapper Type
Byte
Short
Integer
Long
Float
Double
Character
Boolean
String conversion
Convert String str to byte b
byte b = Byte.parseByte(str);
Convert String str to short s
short s = Short.parseShort(str);
Convert String str to int i
int i = Integer.parseInt(str);
Convert String str to long l
long l = Long.parseLong(str);
Convert String str to float f
float f = Float.parseFloat(str);
Convert String str to double d
double d = Double.parseDouble(str);
Action performed for integer input
public void actionPerformed(ActionEvent event)
{
Object cause = event.getSource();
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
if(cause == b)
{
int min = minimum(x,y);
t3.setText(“The minimum is: ” + min);
}
}