looking inside classes fields, constructors & methods week 3

20
Looking inside classes Fields, Constructors & Methods Week 3

Upload: amy-clemo

Post on 31-Mar-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Looking inside classes Fields, Constructors & Methods Week 3

Looking inside classes

Fields, Constructors & Methods

Week 3

Page 2: Looking inside classes Fields, Constructors & Methods Week 3

• Fields

• Constructors

• Methods

• Parameters

• Printing

Java Class Definitions CONCEPTS COVERED

Page 3: Looking inside classes Fields, Constructors & Methods Week 3

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

Page 4: Looking inside classes Fields, Constructors & Methods Week 3

BlueJ DemonstrationA first look at the naive-ticket-machine project

Page 5: Looking inside classes Fields, Constructors & Methods Week 3

TicketMachine

int price

int balance

int total

int getPrice()

int getBalance()

void insertMoney(int amount)

void printTicket()

Page 6: Looking inside classes Fields, Constructors & Methods Week 3

• 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

Page 7: Looking inside classes Fields, Constructors & Methods Week 3

Java Class DefinitionsBASIC STRUCTURE OF A JAVA CLASS

public class ClassName{ Fields Constructors Methods}

public class TicketMachine{ Inner part of the class omitted.}

Page 8: Looking inside classes Fields, Constructors & Methods Week 3

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.

Page 9: Looking inside classes Fields, Constructors & Methods Week 3

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

Page 10: Looking inside classes Fields, Constructors & Methods Week 3

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

Page 11: Looking inside classes Fields, Constructors & Methods Week 3

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.

Page 12: Looking inside classes Fields, Constructors & Methods Week 3

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

Page 13: Looking inside classes Fields, Constructors & Methods Week 3

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; } }

Page 14: Looking inside classes Fields, Constructors & Methods Week 3

• 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

Page 15: Looking inside classes Fields, Constructors & Methods Week 3

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

Page 16: Looking inside classes Fields, Constructors & Methods Week 3

• 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

Page 17: Looking inside classes Fields, Constructors & Methods Week 3

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)

Page 18: Looking inside classes Fields, Constructors & Methods Week 3

Java Class DefinitionsPASSING DATA VIA PARAMETERS

Page 19: Looking inside classes Fields, Constructors & Methods Week 3

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;}

Page 20: Looking inside classes Fields, Constructors & Methods Week 3

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)