SW08 - Object-Oriented Programming and Principles
Understanding class
definitions
Looking inside classes
2.1
Main concepts to be covered
• fields
• constructors
• methods
• parameters
• assignment statements
• conditional statements
© M. Kölling, University of Southern Denmark 1
SW08 - Object-Oriented Programming and Principles
Ticket machines – an external
view
• Exploring the behavior of a typical
ticket machine.
– Use the naive-ticket-machine project.
– Machines supply tickets of a fixed price.
• How is that price determined?
– How is ‘money’ entered into a machine?
– How does a machine keep track of the
money that is entered?
Ticket machines – an internal
view
• Interacting with an object gives us
clues about its behavior.
• Looking inside allows us to determine
how that behavior is provided or
implemented.
• All Java classes have a similar-looking
internal view.
© M. Kölling, University of Southern Denmark 2
SW08 - Object-Oriented Programming and Principles
Basic class structure
public class TicketMachine
The outer wrapper
{ of TicketMachine
Inner part of the class omitted.
}
public class ClassName
{
Fields
Constructors
The contents of a
Methods class
}
Fields
• Fields store values public class TicketMachine
for an object. {
private int price;
• They are also known private int balance;
as instance variables. private int total;
• Use the Inspect }
Constructor and methods omitted.
option to view an
object’s fields.
• Fields define the visibility modifier type variable name
state of an object.
private int price;
© M. Kölling, University of Southern Denmark 3
SW08 - Object-Oriented Programming and Principles
Constructors
• Constructors public TicketMachine(int ticketCost)
initialize an object. {
price = ticketCost;
• They have the same balance = 0;
total = 0;
name as their class. }
• They store initial
values into the
fields.
• They often receive
external parameter
values for this.
Passing data via parameters
© M. Kölling, University of Southern Denmark 4
SW08 - Object-Oriented Programming and Principles
Assignment
• Values are stored into fields (and
other variables) via assignment
statements:
– variable = expression;
– price = ticketCost;
• A variable stores a single value, so
any previous value is lost.
Accessor methods
• Methods implement the behavior of objects.
• Accessors provide information about an
object.
• Methods have a structure consisting of a
header and a body.
• The header defines the method’s signature.
public int getPrice()
• The body encloses the method’s statements.
10
© M. Kölling, University of Southern Denmark 5
SW08 - Object-Oriented Programming and Principles
Accessor methods
return type
visibility modifier method name
parameter list
public int getPrice() (empty)
{
return price; return statement
}
start and end of method body (block )
11
Mutator methods
• Have a similar method structure: header
and body.
• Used to mutate (i.e., change) an object’s
state.
• Achieved through changing the value of
one or more fields.
– Typically contain assignment statements.
– Typically receive parameters.
12
© M. Kölling, University of Southern Denmark 6
SW08 - Object-Oriented Programming and Principles
Mutator methods
visibility modifier return type (void)
method name parameter
public void insertMoney(int amount)
{
balance = balance + amount;
}
field being changed assignment statement
13
Printing from methods
public void printTicket()
{
// Simulate the printing of a ticket.
[Link]("##################");
[Link]("# The BlueJ Line");
[Link]("# Ticket");
[Link]("# " + price + " cents.");
[Link]("##################");
[Link]();
// Update the total collected with the balance.
total = total + balance;
// Clear the balance.
balance = 0;
}
14
© M. Kölling, University of Southern Denmark 7
SW08 - Object-Oriented Programming and Principles
Reflecting on the ticket
machines
• Their behavior is inadequate in
several ways:
– No checks on the amounts entered.
– No refunds.
– No checks for a sensible initialization.
• How can we do better?
– We need more sophisticated behavior.
15
Making choices
public void insertMoney(int amount)
{
if(amount > 0) {
balance = balance + amount;
}
else {
[Link]("Use a positive amount: " +
amount);
}
}
16
© M. Kölling, University of Southern Denmark 8
SW08 - Object-Oriented Programming and Principles
Making choices
boolean condition to be tested - gives a true or false result
‘if’ keyword
actions if condition is true
if(perform some test) {
Do the statements here if the test gave a true result
}
else {
Do the statements here if the test gave a false result
}
actions if condition is false
‘else’ keyword
17
Local variables
• Fields are one sort of variable.
– They store values through the life of an object.
– They are accessible throughout the class.
• Methods can include shorter-lived
variables.
– They exist only as long as the method is being
executed.
– They are only accessible from within the
method.
18
© M. Kölling, University of Southern Denmark 9
SW08 - Object-Oriented Programming and Principles
Local variables
A local variable
public int refundBalance()
{
No visibility int amountToRefund;
modifier
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
19
Review
• Class bodies contain fields,
constructors and methods.
• Fields store values that determine an
object’s state.
• Constructors initialize objects.
• Methods implement the behavior of
objects.
20
© M. Kölling, University of Southern Denmark 10
SW08 - Object-Oriented Programming and Principles
Review
• Fields, parameters and local variables
are all variables.
• Fields persist for the lifetime of an
object.
• Parameters are used to receive values
into a constructor or method.
• Local variables are used for short-lived
temporary storage.
21
Review
• Objects can make decisions via
conditional (if) statements.
• A true or false test allows one of two
alternative courses of actions to be
taken.
22
© M. Kölling, University of Southern Denmark 11