Fundamentals of Computing 1
Lecture Title: Objects and Classes
@2013 by Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Carnegie Mellon university Email:
[email protected] Duy Tan University
Agenda
Java OOPs Concepts
Creating Objects and Calling methods
Using Java class Library
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
2
OOP
(Object Oriented Programming)
Object-Oriented Programming is a methodology or paradigm to
design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
We will learn about these concepts one by one later.
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
3
Advantage of OOPs over Procedure-
oriented programming language
OOP makes development and maintenance easier where as in
Procedure-oriented programming language it is not easy to
manage if code grows as project size grows
OOP provides data hiding whereas in Procedure-oriented
programming language a global data can be accessed from
anywhere.
OOP provides ability to simulate real-world event much more
effectively. We can provide the solution of real word problem if
we are using the Object-Oriented Programming language
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
4
Classes and Objects
In object-oriented programming, we design a
program using objects and classes.
A class works as a template from which we
create the objects. (cookies cutters)
Object is the physical entity whereas class is
the logical entity. (cookies made with cookies
cutters)
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
5
Objects
A runtime entity that has state and behaviour is
known as an object
An object has two characterstics
state: represents the data of an object. (data)
behaviour:represents the behaviour of an object
(methods)
Object is an instance of a class. Class is a
template or blueprint from which objects are
created. Instructor: Dao, Nguyen Thi Anh
Professor: Lynn Robert Carter
Duy Tan University
6
Objects
State Behavior
Dog Name Barking
Color Wagging tail
Sex Fetching
Lamp On Turn on
Off Turn off
Student Name View GPA
Year Get schoolarship
GPA
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
7
Classes
A class is a group of objects that have
common property. It is a template or
blueprint from which objects are created
A class in java can contain:
data member(s)
Method(s)
Constructor(s)
Block(s)
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
8
Classes (cont.)
An object is a specific instance of a class.
ex: each Bicycle object (Mountain bikes, road
bikes,…) is an instance of the Bicycle class
Each instance have different instance
attributes
ex: a Mountain Bike might be in 5th gear
while a Road Bike might be in 3rd gear.
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
9
Example
Bicycle Class
Objects of
Bicycle Class
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
10
Class structure
Class <Class_name>{
data members;
methods;
}
Ex: Class Boy{
int age;
public void saySomething(){
}
}
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
11
Agenda
Java OOPs Concepts
Using Java class Library
Creating Objects and Calling methods
Using Java Predefined class.
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
12
Common Used Java classes
Class name Pakages Short description
Scanner Java.util Methods for reading input from
console
Random Java.util Generates random numbers
String Java.text Data type for charater strings
Math Java.lang Methods for performing mathematical
operations
Object Java.lang Object equivalent to primitive data
Wrappers types
NumberFormat/ Java.text Allow you to format numbers for
DecimalFormat outputpredefined classes.
Java provide more than 2,000
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
13
Scanner
import java.util.Scanner;
//or import java.util.*;
reference variable
Scanner keyboard = new Scanner(System.in);
object instantiation
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
14
Some other Scanner construtors
Sr.No. Constructor & Description
1 Scanner(File source)
This constructs a new Scanner that produces values scanned from the specified file.
2 Scanner(File source, String charsetName)
This constructs a new Scanner that produces values scanned from the specified file.
3 Scanner(InputStream source)
This constructs a new Scanner that produces values scanned from the specified input
stream.
4 Scanner(InputStream source, String charsetName)
This constructs a new Scanner that produces values scanned from the specified input
stream.
5 Scanner(Readable source)
This constructs a new Scanner that produces values scanned from the specified source.
6 Scanner(ReadableByteChannel source)
This constructs a new Scanner that produces values scanned from the specified channel.
7 Scanner(ReadableByteChannel source, String charsetName)
This constructs a new Scanner that produces values scanned from the specified channel.
8 Scanner(String source)
This constructs a new Scanner that produces values scanned from the specified string.
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
15
Some methods
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
16
Calling methods
int a = keyboard.nextInt();
String hoten=keyboard.next();
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
17
String
import jav.text.*;
String hoten = “nguyen van”;
String ten = new String(“nguyen
van”);
hoten =hoten.concat(“Binh”);
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
18
Math (java.lang)
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
19
Creating Objects
Constructor:
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
20
Calling class’s methods
Professor: Lynn Robert Carter Instructor: Dao, Nguyen Thi Anh
Duy Tan University
21