Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CSCI 317 MOBILE APPLICATION

DEVELOPMENT
LECTURE 5 OBJECTED ORIENTED PROGRAMMING

Prepared by Dr. Donghwoon Kwon


Rockford University
Recap
• Basic Java Syntax

Programming Data Processing

1. Variables 1. Conditional
2. Data types statements
3. Data type 2. Loops
conversion 3. Methods
4. Arrays
What is OOP?
• Object-Oriented Programming is assembly programming.
In other words, it is to assemble objects to create an
entire program.

Bicycle (Program) = Body frame (Object) + Wheels (Object) + Steering wheel (Object)
Advantages of OOP
• Easy for maintenance

• Reusability

• Scalability
Class and Object
• How to make an object?

• Just learned that a program is made from combinations of objects

• What does an object make of?

• The answer is class


Class and Object (Cont’)

Instantiation

Program
(Bicycle)

Class Objects
(Blueprint) (Parts)
Class and Object (Cont’)
• Class Structure

Fields (state)

Methods (Behavior)

Class Name

Fields (State)

Methods
(Behavior)
Class and Object (Cont’)
Class Name

Fields (State)

Methods
(Behavior)

class Cat{
/*fields*/
String name;
String breed;
double weight;

/*methods*/
void claw() {
System.out.println("CLAW!!");
}
void meow() {
System.out.println("MEOW!!");
}
}
Class and Object (Cont’)
• Exercise 1
• Please make a square class and then return the area.

// field

// method
Constructor in Java
• What is a constructor?
• A constructor in Java is a special method to create an object

Constructor

Constructor

Constructor
Constructor in Java (Cont’)
• How to create / use a constructor?
• You already used a constructor using the keyword, new!
Constructor in Java (Cont’)
• 2 roles of a constructor

1. Create an object

2. Initialize an object

Cat cat0 = new Cat(“Kitty”, 3.78, 3);


Constructor in Java (Cont’)
• A constructor is a special type, but it is also a method.
Therefore, it is divided into “define and call”.
• Call a constructor

// Class_Type Variable = new Class_Name(Parameters);


Cat cat0 = new Cat(“Kitty”, 3.78, 3);

• Define a constructor class Cat {


String name;
double weight;
int age;
/* Define a constructor */
Cat (String s, double d, int i) {
name = s; //initialize name
weight = d; //initialize weight
age = i; //initialize age
}
}
Constructor in Java (Cont’)
• Exercise 2

• Based on slides 12 and 13, please make a program in Java to

show the following results


Constructor in Java (Cont’)
• Exercise 3: String.format() method

• The String.format() method is a method that creates a type string

String name = “Pepperoni”;


int price = 9;
String str = String.format(“Pizza { %s, $%d }", name, price);
System.out.println(str);
// => “Pizza { Pepperoni, $9 }"
Constructor in Java (Cont’)
• Exercise 4

• Let’s make a game like Starcraft!

You might also like