Transcript Slide 1

UNDERSTANDING
CLASS DEFINITIONS
CITS1001
2
Main concepts to be covered
• Fields/attributes
• Constructors
• Methods
• Parameters
Source ppts: Objects First with Java - A Practical Introduction
using BlueJ, © David J. Barnes, Michael Kölling
3
Source code
• In Java, classes are defined by text files of source code
• Source code is designed to be both
• human readable, and
• machine readable
• Source code must specify everything about how each of the
objects of a class behave
Turn to the first code examples in the handouts
Java Class File: Student.java
Q: Where does the name of the class appear?
4
A very important point
• Program code is designed to be human readable
• Familiar words are used for programming constructs
(if, else, while, repeat, for)
• Indented format is similar to paragraphs and sections in text
• Meaningful variable names suggest what is intended
(e.g. price, mark, studentName)
• AND program code is also executed by a computer
• The computer will do exactly what it is TOLD to do
• The RULES of the language determine EXACTLY what happens
when the program is run
THE COMPUTER DOES NOT KNOW WHAT
YOU INTENDED THE PROGRAM TO DO
5
“Programming can be difficult at first.
It is annoying when your program doesn’t
work, and you spend ages trying to figure
our why. Bugs can seem to come from
nowhere, for no reason. But there is
always a logical reason behind a bug. It
is incredibly satisfying when your program
does work.”
6
Fields/attributes
• Source code specifies the list of attributes
that each object has
• Each object in a class has the same attributes,
but the values of those attributes may be different
for different objects
• e.g. every student has some student number, but
different students have different student numbers
• Q: Identify the attributes of the Student class
7
Constructor and methods
• Source code specifies how objects are constructed
• It specifies what methods the objects have, and
exactly what each method does
• Each object in a class can perform the same collection of methods
• The constructor is a special method (may be more than one)
• It has the same name as the class
• It has no return type
• Other methods have a return type or void
• Q: identify the constructor and other methods of the
student class
8
Ticket machines – an external view
• Turn to Example 3 in the code handouts:
TicketMachine.java
• Exploring the behavior of a typical ticket machine
• Use the naive-ticket-machine project
• Machines supply tickets at 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?
9
Ticket machines – an internal view
• Interacting with an object gives us clues
about its behavior
• Looking inside allows us to determine how
that behaviour is provided or implemented
• All Java classes have a similar-looking internal view
10
Basic class structure
public class TicketMachine
{
Inner part omitted.
}
public class ClassName
{
Fields
Constructors
Methods
}
}
The outer wrapper of
TicketMachine
The inner contents
of a class
11
Keywords
• Words with a special meaning in the language
• public
• class
• private
• int
• Also known as reserved words
Fields
• Fields store values
•
•
•
•
•
public class TicketMachine
{
private int price;
private int balance;
private int total;
for an object
They are also known
as instance variables
or attributes
Further details omitted.
Fields define the state
}
of an object
Use Inspect to view
the state in BlueJ
type
variable name
Some values change often visibility modifier
Some change rarely
private int price;
(or not at all)
13
Constructors
public TicketMachine(int cost)
{
price = cost;
balance = 0;
total = 0;
}
• Constructors initialize an object
• Have the same name as their class
• Close association with the fields
• Store initial values into the fields
• External parameter values for this object
14
Passing data via parameters
Parameters are another sort of variable
15
Choosing variable names
• There is a lot of freedom over choice of names
• Use it wisely!
• Choose expressive names to make code easier to
understand
• price, amount, name, age, etc.
• Avoid single-letter or cryptic names
• w, t5, xyz123
16
Methods
• Methods implement the behaviour of objects
• Methods have a consistent structure comprising
• A header, and
• A body
• Accessor methods provide information about an object
• Mutator methods alter the state of an object
• Other sorts of methods accomplish a variety of tasks
17
Method structure
• The header provides the method’s signature
public int getPrice()
• The header tells us
• The name of the method
• What parameters it takes
• Whether it returns a result
• Its visibility to objects of other classes
• The body encloses the method’s statements
18
Review
• Class bodies contain fields, constructors, and methods
• Fields store values that determine an object’s state
• Constructors initialize objects – particularly their fields
• Methods implement the behavior of objects
Accessor (get) methods
visibility modifier
return type
method name
public int getPrice()
{
return price;
}
parameter list
(here, empty)
return statement
start and end of method body (block)
20
Accessor methods
• An accessor method always has a return type
that is not void
• An accessor method returns a value (result)
of the type given in the header
• The method will contain a return statement
to return the value
• NB: returning is not printing!
21
Mutator methods
• Have the same 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
• Often receive parameters
22
Mutator methods
visibility modifier
return type
method name
public void insertMoney(int amount)
{
balance = balance + amount;
}
field being mutated
parameter
assignment statement
23
set mutator methods
• Fields often have dedicated set mutator methods
• These have a simple, distinctive form
• void return type
• method name related to the field name
• single parameter, with the same type as the type of the field
• a single assignment statement
24
A typical set method
public void setDiscount(int amount)
{
discount = amount;
}
We can infer that discount is a field of type int, i.e.
private int discount;
25
Protective mutators
• A set method does not have to simply assign the
parameter to the field
• The parameter may be checked for validity
and rejected if inappropriate
• Mutators thereby protect fields
• Mutators support encapsulation
26
Method summary
• Methods implement all object behaviour
• A method has a name and a return type
• The return-type may be void
• A non-void return type means the method
will return a value to its caller
• A method might take parameters
• Parameters bring values in from outside
for the method to use
27
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
28
Review
• Methods have a return type
• void methods do not return anything
• non-void methods return a value
• non-void methods have a return statement
• String values can be printed to the terminal using
System.out.println()
• String values can be concatenated using +
• e.g. “cat” + “fish” is “catfish”