>> Lecture 4
BUILDING THE PROGRAM IV. CLASS AND OBJECT OF JAVA
Learning Outcomes
At the end of this topic, the student is able to :-
i. Differentiate constructor and method
ii. Create new Java objects
iii. Manipulating objects
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
>> Lecture 4
Pre Requisite
BEFORE WE START
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
>> Lecture 4
Pre Requisite of this Chapter
• You should know how to define a Java class
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri
>> Lecture 4
Recap
BLAST FROM THE PAST
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
>> Lecture 4
Programming Token
Class name
public class Token {
Instance
variables private String name;
private int currentPosition = 0;
Token private String color;
-name:String
-currentPosition:int = 0 public int updatePosition (int steps) {
-color:String Method
+updatePosition(steps:int):int
currentPosition = currentPosition + steps;
return currentPosition;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
5
Auhtored by Emma McKay-Fikri
>> Lecture 4
Type of Instance Variables
• Primitive Type
– short, int, float, double
– char
– float, double
– Boolean
• Reference Type
– Will be explain later
BITP 3113 Object Oriented Programming
Sem I 2012/2013
6
Auhtored by Emma McKay-Fikri
>> Lesson 1
Lesson 1
MORE ON TYPE
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
7
>> Lesson 1
Reference Type
• A class can be a member of another class.
– This is what we called as reference type
• Defined as instance variables
• Why?
– This is due to complexity of the object
– Implementing association, aggregation and composition relationship
BITP 3113 Object Oriented Programming
Sem I 2012/2013
8
Auhtored by Emma McKay-Fikri
>> Lesson 1
Reference Type
• For example,
pick
Player Token
Reference type
as instance variables
Player
- name:String
- cashInHand:double
- selectedToken : Token
+ updateCashInHand(amount:double) : void
BITP 3113 Object Oriented Programming
Sem I 2012/2013
9
Auhtored by Emma McKay-Fikri
>> Lesson 1
Reference Type
public class Player {
private String name;
private double cashInHand;
Reference type
private Token selectedToken; as instance variables
public void updateCashInHand(double amount){
cashInHand += amount;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 0
>> Lesson 2
Lesson 2
CREATING OBJECT
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
1
>> Lesson 2
Class and Object
• Class is defined
• Object is created
• In programming, object will be manipulated to make the
application works.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 2
>> Lesson 2
Creating Object
• Object must be created before the program can manipulates
it.
• Object is created using keyword, new and default
constructor.
– Let stick with default constructor as of now
• For example
Token wheelbarrow = new Token( );
new
Class Default
Object keyword
name constructor
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 3
>> Lesson 2
Creating Object
• A memory space will be assigned to an object once it is
created.
• This will allow you to manipulate the object
– Assign value
– Retrieve value
– Ask the object to do something
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 4
>> Lesson 2
Creating Object
Token.java SomeProgram.java
public class Token { Token wheelbarrow = new Token( );
private String name;
private int currentPosition = 0;
private String color;
Created in memory
public int updatePosition (int steps) {
currentPosition = currentPosition + steps;
return currentPosition; wheelbarrow
}
-name=?
} -currentPosition= 0
-color=?
+updatePosition(steps:int):int
• wheelbarrow is going to be manipulated in the application
• wheelbarrow is representing in the Monopoly game.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 5
>> Lesson 3
Lesson 3
DO IT THIS WAY
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
1
6
>> Lesson 3
A Good Practice
• A typical Java program will need at least two Java classes.
• 1 .java file to define a class
• 1 .java file that contains main( ) method
• A good Java programmer will separate class definition and class
manipulation.
– We, the Good Java Programmer will not put everything into one class
and in main( ) method.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 7
>> Lesson 3
Example : Monopoly Game
• To program a Monopoly Game program, we need at least two
classes.
• 1 .java class that defines Token = Token.java
• 1 .java class that have main( ) method =
MonopolyGame.java
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 8
>> Lesson 3
Token.java
public class Token {
private String name;
private int currentPosition = 0;
private String color;
public int updatePosition (int steps) {
currentPosition = currentPosition + steps;
return currentPosition;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
1
Auhtored by Emma McKay-Fikri 9
>> Lesson 3
MonopolyGame.java
public class MonopolyGame {
public static void main(String args[]) {
Token wheelbarrow = new Token( );
Token hat = new Token( );
Token car = new Token( );
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 0
>> Lesson 3
Object Creation
Token wheelbarrow = new Token( ); wheelbarrow
created in memory -name=?
-currentPosition= 0
-color=?
+updatePosition(steps:int):int
Token hat = new Token( ); hat
created in memory -name=?
-currentPosition= 0
-color=?
+updatePosition(steps:int):int
Token car = new Token( ); car
-name=?
created in memory
-currentPosition= 0
-color=?
+updatePosition(steps:int):int
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 1
>> Lesson 4
Lesson 4
MAKING THINGS WORK
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
2
>> Lesson 4
Manipulating Object
• Objects will be manipulated in Java program.
• Object manipulation
– Assign value to object
– Retrieve value from object
– Ask object to do something
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 3
>> Lesson 4
Ask Object To Do Something
• Class defines method.
• Thus, we can ask object to do something via method.
• Object will do something when the method is invoked.
• To invoke a method we used dot operator.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 4
>> Lesson 4
Example : Moving the Token
• Let say we need to move wheelbarrow four steps forward.
• Then we will invoke updatePosition( ) method and pass 4 into
the method.
int newPosition = wheelbarrow.updatePosition(4);
An int variable to hold a Object Dot Method and parameter value
value return by operator
updatePosition()
• Method must be call/invoke as how it is being defined by the class.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 5
>> Lesson 4
MonopolyGame.java
public class MonopolyGame {
public static void main(String args[]) {
Token wheelbarrow = new Token( );
int newPosition = wheelbarrow.updatePosition(4);
System.out.println(“New position is “ + newPosition);
}
/*
What does this program do?
Put your answer here : ______________________________
*/
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 6
>> Lesson 4
More On Dot Operator
• Dot operator can be used to access any non-private member
in the class.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 7
>> Lesson 5
Lesson 5
LET’S DO MORE!
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
2
8
>> Lesson 5
Accessing and Retrieving Value
• Non private member can be directly access using dot
operator.
• Let say name and color is not private in Token.
• We can assign value to name and color directly.
• We, can also retrieve the value of name and color directly.
wheelbarrow.name = “Wheelbarrow”;
wheelbarrow.color = “Grey”;
hat.name = “Hat”;
hat.color = wheelbarrow.color;
BITP 3113 Object Oriented Programming
Sem I 2012/2013
2
Auhtored by Emma McKay-Fikri 9
>> Lesson 5
MonopolyGame.java (Name and color is not private)
public class MonopolyGame {
public static void main(String args[]) {
Token wheelbarrow = new Token( );
Token hat = new Token( );
wheelbarrow.name = “Wheelbarrow”;
wheelbarrow.color = “Grey”;
hat.name = “Hat”;
hat.color = wheelbarrow.color;
}
}
/*
What does this program do?
Put your answer here : ______________________________
*/
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 0
>> Lesson 5
That’s Not The Case
• However, name and color is private.
• The program in the previous slide will give you an error at
compilation.
– Trying to access private member directly.
• We need to amend Token.java.
– Either change private to public (this is not a good idea) or
– Add additional method to access and retrieve all private instance
variables.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 1
>> Lesson 5
Getter and Setter
• Private instance variables are sensitive data.
• These need to be protected from direct access or violation of
external object.
• To overcome this, Java has introduced a getter and setter
method.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 2
>> Lesson 5
A Getter
• A Getter is a public method that will return the value of an
instance variable.
• A Getter will have get and followed by an instance variable
name.
Getter for name
• Example
public String getName( ) {
return name;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 3
>> Lesson 5
Token.java
public class Token {
private String name;
private int currentPosition = 0;
private String color; Getter for name
public String getName(){
return name;
}
public int updatePosition (int steps) {
currentPosition = currentPosition + steps; return
currentPosition;
}
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 4
>> Lesson 5
A Setter
• A Setter is a public method that will assign parameter value
to an instance variable.
• A Setter will have set and followed by an instance variable
name.
Setter for name
• Example
public void setName(String name) {
this.name = name;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 5
>> Lesson 5
Token.java
public class Token {
private String name;
private int currentPosition = 0;
private String color;
public String getName(){
return name;
}
public void setName(String name){
return this.name = name;
}
public int updatePosition (int steps) {
currentPosition = currentPosition + steps; return currentPosition;
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 6
>> Lesson 5
this Neither That Nor This
• this is a Java reserved word.
– Cannot be used as variable name.
• this is a keyword that refers to the current object.
– It is to avoid confusion between instance variables and variables that uses the same
name within a method.
public void setName(String name){
this.name = name;
} Refers to name (instance
variable) in Token
• It is a common practice in Java to use the same variable (especially
parameter) to assign value in setter.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 7
>> Lesson 5
Complete Token Definition
• Download Complete Token dot java.pdf from
ulearn
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 8
>> Lesson 5
Assign and Retrieve Value
• Now, those private instance variables can be access via getter
and setter methods.
• Say that we are assign value and print details of Token
objects.
wheelbarrow.setName(“Wheelbarrow”);
wheelbarrow.setColor(“Grey”);
String name = wheelbarrow.getName();
String color = wheelbarrow.getColor();
System.out.print(“Name : “ + name);
System.out.println(“Color : “ + color);
BITP 3113 Object Oriented Programming
Sem I 2012/2013
3
Auhtored by Emma McKay-Fikri 9
>> Lesson 5
MonopolyGame.java
public class MonopolyGame {
public static void main(String args[]) {
Token wheelbarrow = new Token( );
/* Setting values to wheelbarrow */
wheelbarrow.setName(“Wheelbarrow”);
wheelbarrow.setColor(“Grey”);
/* Getting values from wheelbarrow */
String name = wheelbarrow.getName();
String color = wheelbarrow.getColor();
/* Print details of wheelbarrow */
System.out.print(“Name : “ + name);
System.out.println(“Color : “ + color);
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 0
>> Lesson 5
Complete MonopolyGame
• Download
Complete Monopoly Game dot java.pdf
from ulearn
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 1
>> Lesson 6
Lesson 6
METHOD LIKE
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
4
2
>> Lesson 6
Constructing Object
• Creating object = constructing object
• Object is created using constructor.
• Constructor looks like method but having the same name as
the class with no return value.
• Example
Constructor
Token wheelbarrow = new Token( );
Same as class name
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 3
>> Lesson 6
What is a Constructor?
• A constructor looks like a method but it is not a method.
• A constructor will have the same name as the declared class.
• A constructor will not have a return value.
• A constructor may or may not have parameters.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 4
>> Lesson 6
Why Using Constructor?
• To create object
– To allocate memory to the object
• To initialize values
– To give initial values to instance variables.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 5
>> Lesson 6
Default Constructor
• A default constructor is a constructor without parameter.
• A default constructor exist even if it is not explicitly defined
in a class.
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 6
>> Lesson 6
Example : Constructor for Token.java
• Say that we want to give value for name when when creating
object from class Token.
• Therefore, we need to pass value of name in the constructor.
– Define a constructor with parameter.
Same as class name
public Token (String name) {
this.name = name;
System.out.println(“Token “ + this.name + “ is created.”);
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 7
>> Lesson 5
Defining Constructor for Token.java
public class Token {
private String name;
private int currentPosition = 0;
private String color;
public Token (String name) {
this.name = name;
System.out.println(“Token “ + this.name + “ is
created.”);
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 8
>> Lesson 6
Calling Constructor
• When a constructor is explicitly specified (as in previous
slide), it must be called exactly how it is being defined.
• For example, in the previous slide the constructor has one
parameter.
– Thus, you need to specify the value when you call the constructor.
String value to be pass to
Token wheelbarrow = new Token(“Wheelbarrow”);
constructor
BITP 3113 Object Oriented Programming
Sem I 2012/2013
4
Auhtored by Emma McKay-Fikri 9
>> Lesson 6
Token.java MonopolyGame.java
public class MonopolyGame {
public static void main (String args)
public class Token { {
private String name; Token wheelbarrow =
private int currentPosition = 0; new Token(“Wheelbarrow”);
private String color;
Calling constructor
Token hat = new Token(“Tophat”);
public Token (String name) Token car = new Token (“Ford”);
}
this.name = name; }
color = “Grey”;
System.out.println(“Token “ +
this.name + “ is created.”);
}
}
BITP 3113 Object Oriented Programming
Sem I 2012/2013
50
Auhtored by Emma McKay-Fikri
>> Lesson 6
Object Creation
Token wheelbarrow = new Token(“Wheelbarrow”); wheelbarrow
created in memory -name=“Wheelbarrow”
-currentPosition= 0
-color=“Grey”
+updatePosition(steps:int):int
Token hat = new Token(“Tophat”); hat
created in memory -name=“Tophat”
-currentPosition= 0
-color=“Grey”
+updatePosition(steps:int):int
Token car = new Token(“Ford”); car
-name=“Ford”
created in memory
-currentPosition= 0
-color=“Grey”
+updatePosition(steps:int):int
BITP 3113 Object Oriented Programming
Sem I 2012/2013
5
Auhtored by Emma McKay-Fikri 1
>> Lecture 4
Conclusion
LET’S WRAP IT!
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
2
>> Lecture 4
What Have You Learn Today?
Download Complete Player dot java.pdf from ulearn
Learning Outcomes Check Yourself Checked
i. Differentiate constructor i. Define constructor and method for
and method. Player.
ii. Create new Java objects i. Create two player objects
iii. Manipulating objects i. Set value to player
BITP 3113 Object Oriented Programming
Sem I 2012/2013
5
Auhtored by Emma McKay-Fikri 3
>> Lecture 4
See You in the Next Class
THAT’S WRAP FOR TODAY
BITP 3113 Object Oriented Programming
Sem I 2012/2013
Auhtored by Emma McKay-Fikri
5
4