looking inside classes fields, constructors & methods week 3
TRANSCRIPT
Looking inside classes
Fields, Constructors & Methods
Week 3
• Fields
• Constructors
• Methods
• Parameters
• Printing
Java Class Definitions CONCEPTS COVERED
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?
Java Class DefinitionsTICKET MACHINES – AN EXTERNAL VIEW
BlueJ DemonstrationA 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()
• 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 DefinitionsSOURCE CODE – AN INTERNAL VIEW
Java Class DefinitionsBASIC STRUCTURE OF A JAVA CLASS
public class ClassName{ Fields Constructors Methods}
public class TicketMachine{ Inner part of the class omitted.}
Java Class DefinitionsFIELDS
• 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 DefinitionsSTRUCTURE OF FIELDS IN A JAVA CLASS
public class TicketMachine{ private int price; private int balance; private int total;
Constructor and methods omitted.}
private int price;
visibility modifier data type variable name
Java Class DefinitionsCONSTRUCTORS
• 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 DefinitionsSTRUCTURE OF A CLASS CONSTRUCTOR
public class TicketMachine{ private int price; private int balance; private int total;
public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; } }
• 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 DefinitionsACCESSOR METHODS
Java Class DefinitionsACCESSOR METHODS
public int getPrice(){ return price;}
return typemethod name
parameter list (empty)
start and end of method body (block)
return statement
visibility modifier
• 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 DefinitionsMUTATOR METHODS
Java Class DefinitionsMUTATOR METHODS
public void insertMoney(int amount){ balance = balance + amount;}
return type (void)
method name parameter
visibility modifier
assignment statementfield being changed
start and end of method body (block)
Java Class DefinitionsPASSING DATA VIA PARAMETERS
Java Class DefinitionsPRINTING 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 ReadingObjects First With Java – A Practical Introduction using
BlueJ
Related reading for this lecture
• Chapter 2 pp18-37 (Constructors & Methods)
Reading for next week’s lecture
• Chapter 2 pp37-45 (Choices)