Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

CSC186

OBJECT ORIENTED
PROGRAMMING
Topic 3:
Basic Concepts Of Classes
(Part 2)
LESSON OUTCOME

 Difference of class and object


 Object creation and application
 Introduce application class
 Main method
 Object instantiation
 Use the object for a simple process
CLASS VS
OBJECT
JAVA PROGRAM ANATOMY

Java Classes (Programs)

CLASS OBJECT
Class Program App Program

-Data member (fields)


-Methods -public static void main( )
-static methods
- Constructors
- Mutators
- Accessors //how these methods can be
- Printer applied in App Program?
- Processor
Class Object
 a kind of template.  a thing, both tangible and
 represents the intangible.
common structure  is comprised of data &
behavior shared by operations that
the same type. manipulate these data.
 a collection of  is called an instance of a
objects of similar class.
type.
 must be defined
before creating an
instance of the class.
OBJECT
CREATION
A JAVA APPLICATION PROGRAM

 To run a class, a class must contain a main method.


 This class is called Java program or Java application

Class header

Main method
CLASS CONCEPT

 Bicycle is the programmer-defined classes.


OBJECT CREATION

 Known as object instantiation.


OBJECT CREATION (CONSTRUCTOR)
OBJECT CREATION
 The following sample program creates two Bicycle objects, assigns the
owners’ names to them, and displays the information:
OBJECT CREATION

 Object memory state, assigns the owners’ names :


CALLING THE
METHODS
CALLING THE METHODS
CALLING THE METHODS

void method

void method

return method
MATCHING
ARGUMENTS
AND PARAMETERS
MATCHING ARGUMENTS AND
PARAMETERS
 Class name: Demo
MATCHING ARGUMENTS AND
PARAMETERS
 Application class :

Using access control to the


calling object (dot)
MATCHING ARGUMENTS AND
PARAMETERS
MATCHING ARGUMENTS AND
PARAMETERS
RECALL THE METHOD PURPOSE

To change one or more of an


Mutator object’s values

Accessor To retrieve the values

Processor For calculation purpose

Display a string representation


Printer of an object attributes
SAMPLE JAVA
PROGRAMS
EXAMPLE OF JAVA CLASS DEFINITION
public class Actor Class header : modifier, reserved word class and class name
{
private String actName;
private String movieName; Attributes / data members/ variables declaration:
private double hourWork; identify attributes of an object and its data type

public Actor() Default constructor : initialize the object variables to


{ zero or null
actName =“”;
movieName = “”;
hourWork = 0.0;
}

Mutator method : to
public void setData(String n, String m, double h) set the object’s value
{
Method definition actName = n;
movieName = m;
hourWork = h; }

I/O method : to display the object’s


public String toString()
values
{
return ( actName + “ “ + movieName + “ “ + hourWork ) ;
}
}
EXAMPLE OF JAVA APPLICATION
import java.util.*; Import the package : be able to use the pre-defined classes

public class ActorApp Class header : modifier, reserved word class and class name
{
public static void main(String[] args) Main method
{
Scanner a = new Scanner(System.in); Create input object : to input using
I/O console

To create Actor’s object Actor x = new Actor();

System.out.println("Enter actor's name: ");


String name = a.next();
Read the input entered by
System.out.println("Enter actor's movie : "); user using I/O console
String movie = a.next();

System.out.println("Enter hour work ");


double hour = a.nextDouble();

x.setData(name, movie, hour); To set the object’s values


(properties values)

To display the object’s values System.out.println(x.toString ());


}
}
TEST YOURSELF 1
1. Names the following methods according to the tasks performed.
a) To create an object.
b) To change the value of an object.
c) To be applied to retrieve the value of an object.
d) To process an object’s attributes.
e) To return all the values of the object’s attributes.

2. From the example of Actor and its application above, add the coding to the
programs to do the following tasks:
a) Add the retrievers for each attributes.
b) Add a processor to calculate actor’s payment, let the payment made by
RM350 per hour.
c) In the application class, display the actor’s name and the payment made.
d) Display the actor’s information with payment more than RM1000.
TEST YOURSELF 1

 Complete the application for Account class based on the


following tasks:
a) Create two objects of Account with the value input by the
user.
 Fist object is created using default constructor.
 Second object is created using normal constructor.
b) Display which Account has more balance.
c) Change the both Account balance to become RM10.00
MORE READING

 Wu C. Thomas, “An Introduction to Object-Oriented


Programming with Java.”
 Chapter 4
 Chapter 7
END.
Thank you.

You might also like