SlideShare a Scribd company logo
1 of 122
Java™ Software Solutions: Foundations of
Program Design
Ninth Edition
Chapter 4
Writing Classes
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Writing Classes (1 of 2)
• We’ve been using predefined classes from the Java API.
Now we will learn to write our own classes.
• Chapter 4 focuses on:
– class definitions
– instance data
– encapsulation and Java modifiers
– method declaration and parameter passing
– constructors
– arcs and images
– events and event handlers
– buttons and text fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•The heart of object-oriented
programming is defining
classes that represent
objects with well-defined
state and behavior.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•The scope of a variable,
which determines where it
can be referenced, depends
on where it is declared.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•A UML class diagram helps
us visualize the contents of
and relationships among the
classes of a program.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•An object should be
encapsulated, guarding its
data from inappropriate
access.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•Instance variables should be
declared with private visibility
to promote encapsulation.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•Most objects contain
accessor and mutator
methods to allow the client to
manage data in a controlled
manner.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•The value returned from a
method must be consistent
with the return type in the
method header.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•When a method is called, the
actual parameters are copied
into the formal parameters.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•A variable declared in a
method is local to that
method and cannot be used
outside of it.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•A constructor cannot have
any return type, even void.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (1 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•The heart of object-oriented
programming is defining
classes that represent
objects with well-defined
state and behavior.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Writing Classes (2 of 2)
• The programs we’ve written in previous examples have
used classes defined in the Java API
• Now we will begin to design programs that rely on classes
that we write ourselves
• The class that contains the main method is just the
starting point of a program
• True object-oriented programming is based on defining
classes that represent objects with well-defined
characteristics and functionality
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Examples of Classes (1 of 2)
Class Attributes Operations
Student Name
Address
Major
Grade point average
Set address
Set major
Compute grade point average
Rectangle Length
Width
Color
Set length
Set width
Set color
Aquarium Material
Length
Width
Height
Set material
Set length
Set width
Set height
Compute volume
Compute filled weight
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Examples of Classes (2 of 2)
Class Attributes Operations
Flight Airline
Flight number
Origin city
Destination city
Current status
Set airline
Set flight number
Determine status
Employee Name
Department
Title
Salary
Set department
Set title
Set salary
Compute wages
Compute bonus
Compute taxes
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Classes and Objects
• Recall from our overview of objects in Chapter 1 that an
object has state and behavior
• Consider a six-sided die (singular of dice)
– Its state can be defined as which face is showing
– Its primary behavior is that it can be rolled
• We represent a die by designing a class called Die that
models this state and behavior
– The class serves as the blueprint for a die object
• We can then instantiate as many die objects as we need
for any particular program
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Key Concept
•The scope of a variable,
which determines where it
can be referenced, depends
on where it is declared.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Classes (1 of 3)
• A class can contain data declarations and method
declarations
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Classes (2 of 3)
• The values of the data define the state of an object created
from the class
• The functionality of the methods define the behaviors of the
object
• For our Die class, we might declare an integer called
faceValue that represents the current value showing on
the face
• One of the methods would “roll” the die by setting
faceValue to a random number between one and six
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Classes (3 of 3)
• We’ll want to design the Die class so that it is a versatile
and reusable resource
• Any given program will probably not use all operations of
a given class
• See RollingDice.java
• See Die.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.1 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.1 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.1 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.2 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.2 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.2 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
The Die Class
• The Die class contains two data values
– a constant MAX that represents the maximum face
value
– an integer faceValue that represents the current
face value
• The roll method uses the random method of the Math
class to determine a new face value
• There are also methods to explicitly set and retrieve the
current face value at any time
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
The toString Method
• It’s good practice to define a toString method for a
class
• The toString method returns a character string that
represents the object in some way
• It is called automatically when an object is concatenated
to a string or when it is passed to the println method
• It’s also convenient for debugging problems
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Constructors
• As mentioned previously, a constructor is used to set up
an object when it is initially created
• A constructor has the same name as the class
• The Die constructor is used to set the initial face value of
each new die object to one
• We examine constructors in more detail later in this
chapter
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Data Scope
• The scope of data is the area in a program in which that
data can be referenced (used)
• Data declared at the class level can be referenced by all
methods in that class
• Data declared within a method can be used only in that
method
• Data declared within a method is called local data
• In the Die class, the variable result is declared inside
the toString method -- it is local to that method and
cannot be referenced anywhere else
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Instance Data (1 of 2)
• A variable declared at the class level (such as
faceValue) is called instance data
• Each instance (object) has its own instance variable
• A class declares the type of the data, but it does not
reserve memory space for it
• Each time a Die object is created, a new faceValue
variable is created as well
• The objects of a class share the method definitions, but
each object has its own data space
• That’s the only way two objects can have different states
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Instance Data (2 of 2)
• We can depict the two Die objects from the
RollingDice program as follows:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
UML Diagrams
• UML stands for the Unified Modeling Language
• UML diagrams show relationships among classes and
objects
• A UML class diagram consists of one or more classes,
each with sections for the class name, attributes (data),
and operations (methods)
• Lines between classes represent associations
• A dotted arrow shows that one class uses the other (calls
its methods)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
UML Class Diagrams
• A UML class diagram for the RollingDice program:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (1 of 4)
What is the relationship between a class and an object?
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (2 of 4)
What is the relationship between a class and an object?
A class is the definition/pattern/blueprint of an object. It
defines the data that will be managed by an object but
doesn’t reserve memory space for it. Multiple objects can
be created from a class, and each object has its own copy
of the instance data.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (3 of 4)
Where is instance data declared?
What is the scope of instance data?
What is local data?
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 1 (4 of 4)
Where is instance data declared?
At the class level.
What is the scope of instance data?
It can be referenced in any method of the class.
What is local data?
Local data is declared within a method, and is only
accessible in that method.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (2 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Encapsulation (1 of 3)
• There are two views of an object:
– internal - the details of the variables and methods of
the class that defines it
– external - the services that an object provides and
how the object interacts with the rest of the system
• From the external view, an object is an encapsulated
entity, providing a set of specific services
• These services define the interface to the object
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Encapsulation (2 of 3)
• One object (called the client) may use another object for
the services it provides
• The client of an object may request its services (call its
methods), but it should not have to be aware of how those
services are accomplished
• Any changes to the object’s state (its variables) should be
made by that object’s methods
• We should make it difficult, if not impossible, for a client to
access an object’s variables directly
• That is, an object should be self-governing
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Encapsulation (3 of 3)
• An encapsulated object can be thought of as a black
box -- its inner workings are hidden from the client
• The client invokes the interface methods and they
manage the instance data
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Visibility Modifiers (1 of 5)
• In Java, we accomplish encapsulation through the
appropriate use of visibility modifiers
• A modifier is a Java reserved word that specifies
particular characteristics of a method or data
• We’ve used the final modifier to define constants
• Java has three visibility modifiers: public, protected,
and private
• The protected modifier involves inheritance, which we
will discuss later
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Visibility Modifiers (2 of 5)
• Members of a class that are declared with public
visibility can be referenced anywhere
• Members of a class that are declared with private
visibility can be referenced only within that class
• Members declared without a visibility modifier have
default visibility and can be referenced by any class in
the same package
• An overview of all Java modifiers is presented in
Appendix E
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Visibility Modifiers (3 of 5)
• Public variables violate encapsulation because they allow
the client to modify the values directly
• Therefore instance variables should not be declared with
public visibility
• It is acceptable to give a constant public visibility, which
allows it to be used outside of the class
• Public constants do not violate encapsulation because,
although the client can access it, its value cannot be
changed
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Visibility Modifiers (4 of 5)
• Methods that provide the object’s services are declared
with public visibility so that they can be invoked by clients
• Public methods are also called service methods
• A method created simply to assist a service method is
called a support method
• Since a support method is not intended to be called by a
client, it should not be declared with public visibility
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Visibility Modifiers (5 of 5)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Accessors and Mutators
• Because instance data is private, a class usually
provides services to access and modify data values
• An accessor method returns the current value of a
variable
• A mutator method changes the value of a variable
• The names of accessor and mutator methods take the
form getX and setX, respectively, where X is the name
of the value
• They are sometimes called “getters” and “setters”
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Mutator Restrictions
• The use of mutators gives the class designer the ability to
restrict a client’s options to modify an object’s state
• A mutator is often designed so that the values of variables
can be set only within particular limits
• For example, the setFaceValue mutator of the Die class
should restrict the value to the valid range (1 to MAX)
• We’ll see in Chapter 5 how such restrictions can be
implemented
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 2 (1 of 2)
Why was the faceValue variable declared as private in
the Die class?
Why is it ok to declare MAX as public in the Die class?
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 2 (2 of 2)
Why was the faceValue variable declared as private in
the Die class?
By making it private, each Die object controls its own
data and allows it to be modified only by the well-defined
operations it provides.
Why is it ok to declare MAX as public in the Die class?
MAX is a constant. Its value cannot be changed.
Therefore, there is no violation of encapsulation.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (3 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Method Declarations
• Let’s now examine methods in more detail
• A method declaration specifies the code that will be
executed when the method is invoked (called)
• When a method is invoked, the flow of control jumps to the
method and executes its code
• When complete, the flow returns to the place where the
method was called and continues
• The invocation may or may not return a value, depending
on how the method is defined
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Method Control Flow (1 of 2)
• If the called method is in the same class, only the method
name is needed
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Method Control Flow (2 of 2)
• The called method is often part of another class or object
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Method Header
• A method declaration begins with a method header
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Method Body
• The method header is followed by the method body
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
The return Statement
• The return type of a method indicates the type of value
that the method sends back to the calling location
• A method that does not return a value has a void return
type
• A return statement specifies the value that will be
returned
• Its expression must conform to the return type
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Parameters
• When a method is called, the actual parameters in the
invocation are copied into the formal parameters in the
method header
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Local Data
• As we’ve seen, local variables can be declared inside a
method
• The formal parameters of a method create automatic local
variables when the method is invoked
• When the method finishes, all local variables are destroyed
(including the formal parameters)
• Keep in mind that instance variables, declared at the class
level, exists as long as the object exists
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Bank Account Example (1 of 3)
• Let’s look at another example that demonstrates the
implementation details of classes and methods
• We’ll represent a bank account by a class named
Account
• It’s state can include the account number, the current
balance, and the name of the owner
• An account’s behaviors (or services) include deposits and
withdrawals, and adding interest
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Driver Programs
• A driver program drives the use of other, more
interesting parts of a program
• Driver programs are often used to test other parts of the
software
• The Transactions class contains a main method that
drives the use of the Account class, exercising its
services
• See Transactions.java
• See Account.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.3 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.3 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.3 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.4 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.4 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.4 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Bank Account Example (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Bank Account Example (3 of 3)
• There are some improvements that can be made to the
Account class
• Formal getters and setters could have been defined for all
data
• The design of some methods could also be more robust,
such as verifying that the amount parameter to the
withdraw method is positive
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Constructors Revisited
• Note that a constructor has no return type specified in the
method header, not even void
• A common error is to put a return type on a constructor,
which makes it a “regular” method that happens to have
the same name as the class
• The programmer does not have to define a constructor for
a class
• Each class has a default constructor that accepts no
parameters
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 3 (1 of 2)
How do we express which Account object’s balance is
updated when a deposit is made?
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Quick Check 3 (2 of 2)
How do we express which Account object’s balance is
updated when a deposit is made?
Each account is referenced by an object reference
variable:
and when a method is called, you call it through a
particular object:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (4 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Arcs (1 of 4)
• In Chapter 3 we explored basic shapes: lines,
rectangles, circles, and ellipses
• In JavaFX, an arc is defined as a portion of an ellipse
• Like an ellipse, the first four parameters to the Arc
constructor specify the center point (x and y) as well as
the radii along the horizontal and vertical
• Two additional parameters specify the portion of the
ellipse that define the arc
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Arcs (2 of 4)
• The Arc constructor:
• The start angle is where the arc begins relative to the
horizontal
• The arc length is the angle that defines how big the arc
is
• Both angles are specified in degrees
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Arcs (3 of 4)
• An arc whose underlying ellipse is centered at (150, 100), a
horizontal radius of 70 and a vertical radius of 30, a start
angle of 45 and a arc length of 90:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Arcs (4 of 4)
• An arc also has an arc type:
ArcType.OPEN The curve along the ellipse edge
ArcType.CHORD End points are connected by a straight line
ArcType.ROUND
End points are connected to the center point of
the ellipse, forming a rounded “pie” piece
• See ArcDisplay.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.5 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.5 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.5 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (5 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Images (1 of 2)
• The JavaFX Image class is used to load an image from a
file or URL
• Supported formats: jpeg, gif, and png
• To display an image, use an ImageView object
• An Image object cannot be added to a container directly
• See ImageDisplay.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.6 (1 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.6 (2 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.6 (3 of 3)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Layout Panes (1 of 2)
• This example uses a StackPane instead of a Group as
the root node of the scene
• A stack pane is a JavaFX layout pane (one of several),
which governs how its contents are presented
• A stack pane stacks its nodes on top of each other
• Since the image view is the only node in the pane, the
stack pane simply serves to keep the image centered in
the window
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Layout Panes (2 of 2)
• The background color of a layout pane is set using a call
to the setStyle method
• The setStyle method accepts a string that can specify
various style properties
• The notation used for JavaFX style properties are similar
to cascading style sheets (CSS), used to specify the look
of HTML elements on a Web page
• JavaFX style property names begin with the prefix “-fx-”
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Images (2 of 2)
• The parameter to the Image constructor can include a
pathname:
• It can also be a URL:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Viewports
• A viewport is a rectangular area that restricts the pixels
displayed in an ImageView
• It is defined by a Rectangle2D object:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (6 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (1 of 8)
• A Graphical User Interface (GUI) in Java is created with
at least three kinds of objects:
– controls, events, and event handlers
• A control is a screen element that displays information
or allows the user to interact with the program:
– labels, buttons, text fields, sliders, etc.
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (2 of 8)
• An event is an object that represents some activity to
which we may want to respond
• For example, we may want our program to perform some
action when the following occurs:
– a graphical button is pressed
– a slider is dragged
– the mouse is moved
– the mouse is dragged
– the mouse button is clicked
– a keyboard key is pressed
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (3 of 8)
• The Java API contains several classes that represent
typical events
• Controls, such as a button, generate (or fire) an event
when it occurs
• We set up an event handler object to respond to an
event when it occurs
• We design event handlers to take whatever actions are
appropriate when an event occurs
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (4 of 8)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (5 of 8)
• A JavaFX button is defined by the Button class
• It generates an action event
• The PushCounter example displays a button that
increments a counter each time it is pushed
• See PushCounter.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.7 (1 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.7 (2 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.7 (3 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.7 (4 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (6 of 8)
• A call to the setOnAction method sets up the
relationship between the button that generates the event
and the event handler that responds to it
• This example uses a method reference (using the
 operator) to specify the event handler method
• The this reference indicates that the event handler
method is in the same class
• So the PushCounter class also represents the event
handler for this program
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (7 of 8)
• The event handler method can be called whatever you
want, but must accept an ActionEvent object as a
parmeter
• In this example, the event handler method increments
the counter and updates the text object
• The counter and Text object are declared at the class
level so that both methods can use them
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Graphical User Interfaces (8 of 8)
• In this example, a FlowPane is used as the root node of
the scene
• A flow pane is another layout pane, which displays its
contents horizontally in rows or vertically in columns
• A gap of 20 pixels is established between elements on a
row using the setHGap method
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Alternate Event Handlers (1 of 4)
• Instead of using a method reference, the event handler
could be specified using a separate class that implements
the EventHandler interface:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Alternate Event Handlers (2 of 4)
• The event handler class could be defined as public in a
separate file or as a private inner class in the same file
• Either way, the call to the setOnAction method would
specify a new event handler object:
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Alternate Event Handlers (3 of 4)
• Another approach would be to define the event handler
using a lambda expression in the call to setOnAction:
• A lambda expression is defined by a set of parameters, the
operator and an expression
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Alternate Event Handlers (4 of 4)
• A lambda expression can be used whenever an object of a
functional interface is required
• A functional interface contains a single method
• The EventHandler interface is a functional interface
• The method reference approach is equivalent to a lambda
expression
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Outline (7 of 7)
• Anatomy of a Class
• Encapsulation
• Anatomy of a Method
• Arcs
• Images
• Graphical User Interfaces
• Text Fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Text Fields (1 of 3)
• Let’s look at a GUI example that uses another type of
control
• A text field allows the user to enter one line of input
• If the cursor is in the text field, the text field object
generates an action event when the enter key is pressed
• See FahrenheitConverter.java
• See FahrenheitPane.java
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.8 (1 of 2)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.8 (2 of 2)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Text Fields (2 of 3)
• The details of the user interface are set up in a separate
class that extends GridPane
• GridPane is a JavaFX layout pane that displays nodes in
a rectangular grid
• The GUI elements are set up in the constructor of
FahrenheitPane
• The event handler method is also defined in
FahrenheitPane
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.9 (1 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.9 (2 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.9 (3 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Listing 4.9 (4 of 4)
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Text Fields (3 of 3)
• Through inheritance, a FahrenheitPane is a GridPane
and inherits the add method
• The parameters to add specify the grid cell to which to add
the node
• Row and column numbering in a grid pane start at 0
• When the user presses return, the event handler method is
called, which converts the value and updates the text result
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Summary
• Chapter 4 focused on:
– class definitions
– instance data
– encapsulation and Java modifiers
– method declaration and parameter passing
– constructors
– arcs and images
– events and event handlers
– buttons and text fields
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Copyright
This work is protected by United States copyright laws and is
provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

More Related Content

What's hot

11 advance inheritance_concepts
11 advance inheritance_concepts11 advance inheritance_concepts
11 advance inheritance_concepts
Arriz San Juan
 
Object Oriented Concept
Object Oriented ConceptObject Oriented Concept
Object Oriented Concept
smj
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
Raghu nath
 
Ijarcet vol-2-issue-4-1363-1367
Ijarcet vol-2-issue-4-1363-1367Ijarcet vol-2-issue-4-1363-1367
Ijarcet vol-2-issue-4-1363-1367
Editor IJARCET
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4
sotlsoc
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Cso gaddis java_chapter6
Cso gaddis java_chapter6Cso gaddis java_chapter6
Cso gaddis java_chapter6
RhettB
 

What's hot (18)

Object Oriented Software Development revision slide
Object Oriented Software Development revision slide Object Oriented Software Development revision slide
Object Oriented Software Development revision slide
 
Object Oriented Relationships
Object Oriented RelationshipsObject Oriented Relationships
Object Oriented Relationships
 
Eo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and ObjectsEo gaddis java_chapter_06_Classes and Objects
Eo gaddis java_chapter_06_Classes and Objects
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
11 advance inheritance_concepts
11 advance inheritance_concepts11 advance inheritance_concepts
11 advance inheritance_concepts
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Object Oriented Concept
Object Oriented ConceptObject Oriented Concept
Object Oriented Concept
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
classes & objects introduction
classes & objects introductionclasses & objects introduction
classes & objects introduction
 
Machine Learning - Supervised learning
Machine Learning - Supervised learningMachine Learning - Supervised learning
Machine Learning - Supervised learning
 
Ijarcet vol-2-issue-4-1363-1367
Ijarcet vol-2-issue-4-1363-1367Ijarcet vol-2-issue-4-1363-1367
Ijarcet vol-2-issue-4-1363-1367
 
Text Classification, Sentiment Analysis, and Opinion Mining
Text Classification, Sentiment Analysis, and Opinion MiningText Classification, Sentiment Analysis, and Opinion Mining
Text Classification, Sentiment Analysis, and Opinion Mining
 
Machine learning module 2
Machine learning module 2Machine learning module 2
Machine learning module 2
 
Chapter 6.4
Chapter 6.4Chapter 6.4
Chapter 6.4
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Lect07
Lect07Lect07
Lect07
 
Cso gaddis java_chapter6
Cso gaddis java_chapter6Cso gaddis java_chapter6
Cso gaddis java_chapter6
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 

Similar to Chapter 4 Writing Classes

Cso gaddis java_chapter6
Cso gaddis java_chapter6Cso gaddis java_chapter6
Cso gaddis java_chapter6
mlrbrown
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
Ranel Padon
 
Strategy
StrategyStrategy
Strategy
Monjurul Habib
 
Object Oriented PHP Overview
Object Oriented PHP OverviewObject Oriented PHP Overview
Object Oriented PHP Overview
Larry Ball
 
Week08
Week08Week08
Week08
hccit
 
Object Modelling Technique " ooad "
Object Modelling Technique  " ooad "Object Modelling Technique  " ooad "
Object Modelling Technique " ooad "
AchrafJbr
 

Similar to Chapter 4 Writing Classes (20)

Cso gaddis java_chapter6
Cso gaddis java_chapter6Cso gaddis java_chapter6
Cso gaddis java_chapter6
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
Strategy
StrategyStrategy
Strategy
 
Strategy Pattern
Strategy PatternStrategy Pattern
Strategy Pattern
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
O6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdfO6u CS-315A OOP Lecture (1).pdf
O6u CS-315A OOP Lecture (1).pdf
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
 
1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx1_Object Oriented Programming.pptx
1_Object Oriented Programming.pptx
 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object Oriented PHP Overview
Object Oriented PHP OverviewObject Oriented PHP Overview
Object Oriented PHP Overview
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
 
Week08
Week08Week08
Week08
 
Summer Training Project On C++
Summer Training Project On  C++Summer Training Project On  C++
Summer Training Project On C++
 
Object Modelling Technique " ooad "
Object Modelling Technique  " ooad "Object Modelling Technique  " ooad "
Object Modelling Technique " ooad "
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

More from DanWooster1

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
DanWooster1
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
DanWooster1
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
DanWooster1
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
DanWooster1
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
DanWooster1
 

More from DanWooster1 (20)

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1
 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3
 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2
 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
 
Upstate CSCI 450 PHP
Upstate CSCI 450 PHPUpstate CSCI 450 PHP
Upstate CSCI 450 PHP
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
 

Recently uploaded

An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Chapter 4 Writing Classes

  • 1. Java™ Software Solutions: Foundations of Program Design Ninth Edition Chapter 4 Writing Classes Copyright © 2018 Pearson Education, Inc. All Rights Reserved
  • 2. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Writing Classes (1 of 2) • We’ve been using predefined classes from the Java API. Now we will learn to write our own classes. • Chapter 4 focuses on: – class definitions – instance data – encapsulation and Java modifiers – method declaration and parameter passing – constructors – arcs and images – events and event handlers – buttons and text fields
  • 3. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •The heart of object-oriented programming is defining classes that represent objects with well-defined state and behavior.
  • 4. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •The scope of a variable, which determines where it can be referenced, depends on where it is declared.
  • 5. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •A UML class diagram helps us visualize the contents of and relationships among the classes of a program.
  • 6. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •An object should be encapsulated, guarding its data from inappropriate access.
  • 7. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •Instance variables should be declared with private visibility to promote encapsulation.
  • 8. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •Most objects contain accessor and mutator methods to allow the client to manage data in a controlled manner.
  • 9. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •The value returned from a method must be consistent with the return type in the method header.
  • 10. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •When a method is called, the actual parameters are copied into the formal parameters.
  • 11. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •A variable declared in a method is local to that method and cannot be used outside of it.
  • 12. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •A constructor cannot have any return type, even void.
  • 13. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (1 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 14. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •The heart of object-oriented programming is defining classes that represent objects with well-defined state and behavior.
  • 15. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Writing Classes (2 of 2) • The programs we’ve written in previous examples have used classes defined in the Java API • Now we will begin to design programs that rely on classes that we write ourselves • The class that contains the main method is just the starting point of a program • True object-oriented programming is based on defining classes that represent objects with well-defined characteristics and functionality
  • 16. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Examples of Classes (1 of 2) Class Attributes Operations Student Name Address Major Grade point average Set address Set major Compute grade point average Rectangle Length Width Color Set length Set width Set color Aquarium Material Length Width Height Set material Set length Set width Set height Compute volume Compute filled weight
  • 17. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Examples of Classes (2 of 2) Class Attributes Operations Flight Airline Flight number Origin city Destination city Current status Set airline Set flight number Determine status Employee Name Department Title Salary Set department Set title Set salary Compute wages Compute bonus Compute taxes
  • 18. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Classes and Objects • Recall from our overview of objects in Chapter 1 that an object has state and behavior • Consider a six-sided die (singular of dice) – Its state can be defined as which face is showing – Its primary behavior is that it can be rolled • We represent a die by designing a class called Die that models this state and behavior – The class serves as the blueprint for a die object • We can then instantiate as many die objects as we need for any particular program
  • 19. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Key Concept •The scope of a variable, which determines where it can be referenced, depends on where it is declared.
  • 20. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Classes (1 of 3) • A class can contain data declarations and method declarations
  • 21. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Classes (2 of 3) • The values of the data define the state of an object created from the class • The functionality of the methods define the behaviors of the object • For our Die class, we might declare an integer called faceValue that represents the current value showing on the face • One of the methods would “roll” the die by setting faceValue to a random number between one and six
  • 22. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Classes (3 of 3) • We’ll want to design the Die class so that it is a versatile and reusable resource • Any given program will probably not use all operations of a given class • See RollingDice.java • See Die.java
  • 23. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.1 (1 of 3)
  • 24. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.1 (2 of 3)
  • 25. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.1 (3 of 3)
  • 26. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.2 (1 of 3)
  • 27. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.2 (2 of 3)
  • 28. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.2 (3 of 3)
  • 29. Copyright © 2018 Pearson Education, Inc. All Rights Reserved The Die Class • The Die class contains two data values – a constant MAX that represents the maximum face value – an integer faceValue that represents the current face value • The roll method uses the random method of the Math class to determine a new face value • There are also methods to explicitly set and retrieve the current face value at any time
  • 30. Copyright © 2018 Pearson Education, Inc. All Rights Reserved The toString Method • It’s good practice to define a toString method for a class • The toString method returns a character string that represents the object in some way • It is called automatically when an object is concatenated to a string or when it is passed to the println method • It’s also convenient for debugging problems
  • 31. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Constructors • As mentioned previously, a constructor is used to set up an object when it is initially created • A constructor has the same name as the class • The Die constructor is used to set the initial face value of each new die object to one • We examine constructors in more detail later in this chapter
  • 32. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Data Scope • The scope of data is the area in a program in which that data can be referenced (used) • Data declared at the class level can be referenced by all methods in that class • Data declared within a method can be used only in that method • Data declared within a method is called local data • In the Die class, the variable result is declared inside the toString method -- it is local to that method and cannot be referenced anywhere else
  • 33. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Instance Data (1 of 2) • A variable declared at the class level (such as faceValue) is called instance data • Each instance (object) has its own instance variable • A class declares the type of the data, but it does not reserve memory space for it • Each time a Die object is created, a new faceValue variable is created as well • The objects of a class share the method definitions, but each object has its own data space • That’s the only way two objects can have different states
  • 34. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Instance Data (2 of 2) • We can depict the two Die objects from the RollingDice program as follows:
  • 35. Copyright © 2018 Pearson Education, Inc. All Rights Reserved UML Diagrams • UML stands for the Unified Modeling Language • UML diagrams show relationships among classes and objects • A UML class diagram consists of one or more classes, each with sections for the class name, attributes (data), and operations (methods) • Lines between classes represent associations • A dotted arrow shows that one class uses the other (calls its methods)
  • 36. Copyright © 2018 Pearson Education, Inc. All Rights Reserved UML Class Diagrams • A UML class diagram for the RollingDice program:
  • 37. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (1 of 4) What is the relationship between a class and an object?
  • 38. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (2 of 4) What is the relationship between a class and an object? A class is the definition/pattern/blueprint of an object. It defines the data that will be managed by an object but doesn’t reserve memory space for it. Multiple objects can be created from a class, and each object has its own copy of the instance data.
  • 39. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (3 of 4) Where is instance data declared? What is the scope of instance data? What is local data?
  • 40. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 1 (4 of 4) Where is instance data declared? At the class level. What is the scope of instance data? It can be referenced in any method of the class. What is local data? Local data is declared within a method, and is only accessible in that method.
  • 41. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (2 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 42. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Encapsulation (1 of 3) • There are two views of an object: – internal - the details of the variables and methods of the class that defines it – external - the services that an object provides and how the object interacts with the rest of the system • From the external view, an object is an encapsulated entity, providing a set of specific services • These services define the interface to the object
  • 43. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Encapsulation (2 of 3) • One object (called the client) may use another object for the services it provides • The client of an object may request its services (call its methods), but it should not have to be aware of how those services are accomplished • Any changes to the object’s state (its variables) should be made by that object’s methods • We should make it difficult, if not impossible, for a client to access an object’s variables directly • That is, an object should be self-governing
  • 44. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Encapsulation (3 of 3) • An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client • The client invokes the interface methods and they manage the instance data
  • 45. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Visibility Modifiers (1 of 5) • In Java, we accomplish encapsulation through the appropriate use of visibility modifiers • A modifier is a Java reserved word that specifies particular characteristics of a method or data • We’ve used the final modifier to define constants • Java has three visibility modifiers: public, protected, and private • The protected modifier involves inheritance, which we will discuss later
  • 46. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Visibility Modifiers (2 of 5) • Members of a class that are declared with public visibility can be referenced anywhere • Members of a class that are declared with private visibility can be referenced only within that class • Members declared without a visibility modifier have default visibility and can be referenced by any class in the same package • An overview of all Java modifiers is presented in Appendix E
  • 47. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Visibility Modifiers (3 of 5) • Public variables violate encapsulation because they allow the client to modify the values directly • Therefore instance variables should not be declared with public visibility • It is acceptable to give a constant public visibility, which allows it to be used outside of the class • Public constants do not violate encapsulation because, although the client can access it, its value cannot be changed
  • 48. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Visibility Modifiers (4 of 5) • Methods that provide the object’s services are declared with public visibility so that they can be invoked by clients • Public methods are also called service methods • A method created simply to assist a service method is called a support method • Since a support method is not intended to be called by a client, it should not be declared with public visibility
  • 49. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Visibility Modifiers (5 of 5)
  • 50. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Accessors and Mutators • Because instance data is private, a class usually provides services to access and modify data values • An accessor method returns the current value of a variable • A mutator method changes the value of a variable • The names of accessor and mutator methods take the form getX and setX, respectively, where X is the name of the value • They are sometimes called “getters” and “setters”
  • 51. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Mutator Restrictions • The use of mutators gives the class designer the ability to restrict a client’s options to modify an object’s state • A mutator is often designed so that the values of variables can be set only within particular limits • For example, the setFaceValue mutator of the Die class should restrict the value to the valid range (1 to MAX) • We’ll see in Chapter 5 how such restrictions can be implemented
  • 52. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 2 (1 of 2) Why was the faceValue variable declared as private in the Die class? Why is it ok to declare MAX as public in the Die class?
  • 53. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 2 (2 of 2) Why was the faceValue variable declared as private in the Die class? By making it private, each Die object controls its own data and allows it to be modified only by the well-defined operations it provides. Why is it ok to declare MAX as public in the Die class? MAX is a constant. Its value cannot be changed. Therefore, there is no violation of encapsulation.
  • 54. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (3 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 55. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Method Declarations • Let’s now examine methods in more detail • A method declaration specifies the code that will be executed when the method is invoked (called) • When a method is invoked, the flow of control jumps to the method and executes its code • When complete, the flow returns to the place where the method was called and continues • The invocation may or may not return a value, depending on how the method is defined
  • 56. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Method Control Flow (1 of 2) • If the called method is in the same class, only the method name is needed
  • 57. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Method Control Flow (2 of 2) • The called method is often part of another class or object
  • 58. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Method Header • A method declaration begins with a method header
  • 59. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Method Body • The method header is followed by the method body
  • 60. Copyright © 2018 Pearson Education, Inc. All Rights Reserved The return Statement • The return type of a method indicates the type of value that the method sends back to the calling location • A method that does not return a value has a void return type • A return statement specifies the value that will be returned • Its expression must conform to the return type
  • 61. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Parameters • When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header
  • 62. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Local Data • As we’ve seen, local variables can be declared inside a method • The formal parameters of a method create automatic local variables when the method is invoked • When the method finishes, all local variables are destroyed (including the formal parameters) • Keep in mind that instance variables, declared at the class level, exists as long as the object exists
  • 63. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Bank Account Example (1 of 3) • Let’s look at another example that demonstrates the implementation details of classes and methods • We’ll represent a bank account by a class named Account • It’s state can include the account number, the current balance, and the name of the owner • An account’s behaviors (or services) include deposits and withdrawals, and adding interest
  • 64. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Driver Programs • A driver program drives the use of other, more interesting parts of a program • Driver programs are often used to test other parts of the software • The Transactions class contains a main method that drives the use of the Account class, exercising its services • See Transactions.java • See Account.java
  • 65. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.3 (1 of 3)
  • 66. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.3 (2 of 3)
  • 67. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.3 (3 of 3)
  • 68. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.4 (1 of 3)
  • 69. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.4 (2 of 3)
  • 70. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.4 (3 of 3)
  • 71. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Bank Account Example (2 of 3)
  • 72. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Bank Account Example (3 of 3) • There are some improvements that can be made to the Account class • Formal getters and setters could have been defined for all data • The design of some methods could also be more robust, such as verifying that the amount parameter to the withdraw method is positive
  • 73.
  • 74. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Constructors Revisited • Note that a constructor has no return type specified in the method header, not even void • A common error is to put a return type on a constructor, which makes it a “regular” method that happens to have the same name as the class • The programmer does not have to define a constructor for a class • Each class has a default constructor that accepts no parameters
  • 75. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 3 (1 of 2) How do we express which Account object’s balance is updated when a deposit is made?
  • 76. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Quick Check 3 (2 of 2) How do we express which Account object’s balance is updated when a deposit is made? Each account is referenced by an object reference variable: and when a method is called, you call it through a particular object:
  • 77. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (4 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 78. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Arcs (1 of 4) • In Chapter 3 we explored basic shapes: lines, rectangles, circles, and ellipses • In JavaFX, an arc is defined as a portion of an ellipse • Like an ellipse, the first four parameters to the Arc constructor specify the center point (x and y) as well as the radii along the horizontal and vertical • Two additional parameters specify the portion of the ellipse that define the arc
  • 79. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Arcs (2 of 4) • The Arc constructor: • The start angle is where the arc begins relative to the horizontal • The arc length is the angle that defines how big the arc is • Both angles are specified in degrees
  • 80. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Arcs (3 of 4) • An arc whose underlying ellipse is centered at (150, 100), a horizontal radius of 70 and a vertical radius of 30, a start angle of 45 and a arc length of 90:
  • 81. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Arcs (4 of 4) • An arc also has an arc type: ArcType.OPEN The curve along the ellipse edge ArcType.CHORD End points are connected by a straight line ArcType.ROUND End points are connected to the center point of the ellipse, forming a rounded “pie” piece • See ArcDisplay.java
  • 82. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.5 (1 of 3)
  • 83. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.5 (2 of 3)
  • 84. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.5 (3 of 3)
  • 85. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (5 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 86. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Images (1 of 2) • The JavaFX Image class is used to load an image from a file or URL • Supported formats: jpeg, gif, and png • To display an image, use an ImageView object • An Image object cannot be added to a container directly • See ImageDisplay.java
  • 87. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.6 (1 of 3)
  • 88. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.6 (2 of 3)
  • 89. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.6 (3 of 3)
  • 90. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Layout Panes (1 of 2) • This example uses a StackPane instead of a Group as the root node of the scene • A stack pane is a JavaFX layout pane (one of several), which governs how its contents are presented • A stack pane stacks its nodes on top of each other • Since the image view is the only node in the pane, the stack pane simply serves to keep the image centered in the window
  • 91. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Layout Panes (2 of 2) • The background color of a layout pane is set using a call to the setStyle method • The setStyle method accepts a string that can specify various style properties • The notation used for JavaFX style properties are similar to cascading style sheets (CSS), used to specify the look of HTML elements on a Web page • JavaFX style property names begin with the prefix “-fx-”
  • 92. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Images (2 of 2) • The parameter to the Image constructor can include a pathname: • It can also be a URL:
  • 93. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Viewports • A viewport is a rectangular area that restricts the pixels displayed in an ImageView • It is defined by a Rectangle2D object:
  • 94. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (6 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 95. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (1 of 8) • A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: – controls, events, and event handlers • A control is a screen element that displays information or allows the user to interact with the program: – labels, buttons, text fields, sliders, etc.
  • 96. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (2 of 8) • An event is an object that represents some activity to which we may want to respond • For example, we may want our program to perform some action when the following occurs: – a graphical button is pressed – a slider is dragged – the mouse is moved – the mouse is dragged – the mouse button is clicked – a keyboard key is pressed
  • 97. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (3 of 8) • The Java API contains several classes that represent typical events • Controls, such as a button, generate (or fire) an event when it occurs • We set up an event handler object to respond to an event when it occurs • We design event handlers to take whatever actions are appropriate when an event occurs
  • 98. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (4 of 8)
  • 99. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (5 of 8) • A JavaFX button is defined by the Button class • It generates an action event • The PushCounter example displays a button that increments a counter each time it is pushed • See PushCounter.java
  • 100. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.7 (1 of 4)
  • 101. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.7 (2 of 4)
  • 102. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.7 (3 of 4)
  • 103. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.7 (4 of 4)
  • 104. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (6 of 8) • A call to the setOnAction method sets up the relationship between the button that generates the event and the event handler that responds to it • This example uses a method reference (using the  operator) to specify the event handler method • The this reference indicates that the event handler method is in the same class • So the PushCounter class also represents the event handler for this program
  • 105. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (7 of 8) • The event handler method can be called whatever you want, but must accept an ActionEvent object as a parmeter • In this example, the event handler method increments the counter and updates the text object • The counter and Text object are declared at the class level so that both methods can use them
  • 106. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Graphical User Interfaces (8 of 8) • In this example, a FlowPane is used as the root node of the scene • A flow pane is another layout pane, which displays its contents horizontally in rows or vertically in columns • A gap of 20 pixels is established between elements on a row using the setHGap method
  • 107. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Alternate Event Handlers (1 of 4) • Instead of using a method reference, the event handler could be specified using a separate class that implements the EventHandler interface:
  • 108. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Alternate Event Handlers (2 of 4) • The event handler class could be defined as public in a separate file or as a private inner class in the same file • Either way, the call to the setOnAction method would specify a new event handler object:
  • 109. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Alternate Event Handlers (3 of 4) • Another approach would be to define the event handler using a lambda expression in the call to setOnAction: • A lambda expression is defined by a set of parameters, the operator and an expression
  • 110. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Alternate Event Handlers (4 of 4) • A lambda expression can be used whenever an object of a functional interface is required • A functional interface contains a single method • The EventHandler interface is a functional interface • The method reference approach is equivalent to a lambda expression
  • 111. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Outline (7 of 7) • Anatomy of a Class • Encapsulation • Anatomy of a Method • Arcs • Images • Graphical User Interfaces • Text Fields
  • 112. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Text Fields (1 of 3) • Let’s look at a GUI example that uses another type of control • A text field allows the user to enter one line of input • If the cursor is in the text field, the text field object generates an action event when the enter key is pressed • See FahrenheitConverter.java • See FahrenheitPane.java
  • 113. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.8 (1 of 2)
  • 114. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.8 (2 of 2)
  • 115. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Text Fields (2 of 3) • The details of the user interface are set up in a separate class that extends GridPane • GridPane is a JavaFX layout pane that displays nodes in a rectangular grid • The GUI elements are set up in the constructor of FahrenheitPane • The event handler method is also defined in FahrenheitPane
  • 116. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.9 (1 of 4)
  • 117. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.9 (2 of 4)
  • 118. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.9 (3 of 4)
  • 119. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Listing 4.9 (4 of 4)
  • 120. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Text Fields (3 of 3) • Through inheritance, a FahrenheitPane is a GridPane and inherits the add method • The parameters to add specify the grid cell to which to add the node • Row and column numbering in a grid pane start at 0 • When the user presses return, the event handler method is called, which converts the value and updates the text result
  • 121. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Summary • Chapter 4 focused on: – class definitions – instance data – encapsulation and Java modifiers – method declaration and parameter passing – constructors – arcs and images – events and event handlers – buttons and text fields
  • 122. Copyright © 2018 Pearson Education, Inc. All Rights Reserved Copyright This work is protected by United States copyright laws and is provided solely for the use of instructors in teaching their courses and assessing student learning. Dissemination or sale of any part of this work (including on the World Wide Web) will destroy the integrity of the work and is not permitted. The work and materials from it should never be made available to students except by instructors using the accompanying text in their classes. All recipients of this work are expected to abide by these restrictions and to honor the intended pedagogical purposes and the needs of other instructors who rely on these materials.

Editor's Notes

  1. If this PowerPoint presentation contains mathematical equations, you may need to check that your computer has the following installed: 1) MathType Plugin 2) Math Player (free versions available) 3) NVDA Reader (free versions available)