No Slide Title

Download Report

Transcript No Slide Title

Looking inside classes
Fields, Constructors &
Methods
Week 3
Java Class Definitions
CONCEPTS COVERED
•
•
•
•
•
Fields
Constructors
Methods
Parameters
Printing
Java Class Definitions
TICKET MACHINES – AN EXTERNAL VIEW
Exploring the behavior of a 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?
BlueJ Demonstration
A first look at the naive-ticket-machine project
TicketMachine
int price
int balance
int total
int getPrice()
int getBalance()
void insertMoney(int amount)
void printTicket()
Java Class Definitions
SOURCE CODE – AN INTERNAL VIEW
• Interacting with an object gives us clues
about its behavior.
• Looking inside the object’s class definition
(source code) allows us to determine how
that behavior is provided or implemented.
• All Java classes have a similar-looking
internal view/structure.
Java Class Definitions
BASIC STRUCTURE OF A JAVA CLASS
public class ClassName
{
Fields
Constructors
Methods
}
public class TicketMachine
{
Inner part of the class omitted.
}
Java Class Definitions
FIELDS
• Fields store values for an object.
• They are also known as instance variables,
and/or attributes.
• Use Inspect in BlueJ to view an object’s
fields.
• The values held in an object’s fields
constitute the state of that object.
Java Class Definitions
STRUCTURE OF FIELDS IN A JAVA CLASS
public class TicketMachine
{
private int price;
private int balance;
private int total;
Constructor and methods omitted.
}
visibility modifier data type variable name
private int price;
Java Class Definitions
CONSTRUCTORS
• Constructors initialize an object
• They have the same name as their class
• They do not have a return type
• They can store initial values into the fields
• They often receive external parameter
values that are used to set initial field
values
Methods
• The Java language is built from many classes, each
with its own inbuilt methods. Much of programming in
Java is about using inbuilt methods as well as being
able to create your own methods.
• The method header or signature describes the
method’s:
–
–
–
–
accessibility modifiers, public or private,
whether or not it returns a value and if so what type,
the name of the method, starting with a lowercase letter,
input needed (formal parameters) for the method to fulfill its
task.
Encapsulation
• Normally we make fields that hold the
object’s data private
• While the methods that can access those
fields are made public
• This way we provide controlled access
to the data, thus protecting it
• This is called encapsulation
Java Class Definitions
STRUCTURE OF A CLASS CONSTRUCTOR
public class TicketMachine
{
private int price;
private int balance;
private int total;
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
}
}
Java Class Definitions
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.
• Method header defines the method’s access
modifier, return type and signature.
public int getPrice()
• Method body encloses a method’s
statements.
Java Class Definitions
ACCESSOR METHODS
return type
visibility modifier
method name
public int getPrice()
{
return price;
}
parameter list
(empty)
return statement
start and end of method body (block)
Java Class Definitions
MUTATOR METHODS
• Mutators have a standard method structure
of method header and method body.
• Mutators are used to mutate (i.e. change) an
object’s state.
• Mutators achieve this through changing the
value of one or more object fields
(attributes).
– Typically contain assignment statements.
– Typically receive parameters.
Java Class Definitions
MUTATOR METHODS
visibility modifier return type (void)
method name
parameter
public void insertMoney(int amount)
{
balance = balance + amount;
}
field being changed
start and end of method body (block)
assignment statement
Java Class Definitions
PASSING DATA VIA PARAMETERS
Java Class Definitions
PRINTING FROM METHODS
public void printTicket()
{
// Simulate the printing of a ticket.
System.out.println("##################");
System.out.println("# The BlueJ Line");
System.out.println("# Ticket");
System.out.println("# " + price + " cents.");
System.out.println("##################");
System.out.println();
// Update the total collected with the balance.
total = total + balance;
// Clear the balance.
balance = 0;
}
Required Reading
Objects First With Java – A Practical Introduction using
BlueJ
Related reading for this lecture
• Chapter 2 pp18-39