bbit 212/ cisy 111 object oriented programming (oop)

35
BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java 22.02.2011

Upload: konala

Post on 20-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

BBIT 212/ CISY 111 Object Oriented Programming (OOP). Objects and classes, object interactions in Java 22.02.2011. (General!) Recap. The two steps of Object Oriented Programming Making Classes: Creating, extending or reusing abstract data types. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

BBIT 212/ CISY 111 Object Oriented Programming (OOP)

Objects and classes, object interactions in Java

22.02.2011

Page 2: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

(General!) Recap

• The two steps of Object Oriented Programming– Making Classes: Creating, extending or reusing

abstract data types.

– Making Objects interact: Creating objects from abstract data types and defining their relationships.

Page 3: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

22nd Feb 2011 - RECAP

• Anatomy of a Java Program– Comments– Reserved words– Modifiers– Statements– Blocks– Classes– Methods– The main method

Page 4: BBIT 212/ CISY 111   Object Oriented Programming (OOP)
Page 5: BBIT 212/ CISY 111   Object Oriented Programming (OOP)
Page 6: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Recap

• From the notes!– Variables

• What they are?• Declaring • Initializing *

– Operators• Numeric operators• Shorthand operators• Increment and Decrement operators

– Constants– Expressions, Statements and Blocks

Page 7: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Variables– A variable is a “place-holder” (in memory) representing data of a

certain type– Variable declaration tells the compiler “the name of the variable as

well as what type of data it represents”datatype variableName;

• E.g. – int radius;– double pie;

– If variables are of the same type, they can be declared together, as follows:

datatype variableName1, variableName2, ..., variableNameX;

E.g: double pie, perimeter, area;

Page 8: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

– Declaring and initializing variables in one step– Variables often have initial values. You can declare

a variable and initialize it in one step.• int radius = 7;• This is equivalent to the next two statements:

int radius;radius = 1;

– You can also use a shorthand form to declare and initialize variables of the same type together. For example, int i = 1, j = 2;

Page 9: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• (Numeric) Operators

Page 10: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Shorthand Operators– Very often the current value of a variable is used,

modified, and then reassigned back to the same variable.

– E.g.: i = i + 8;– Java allows you to combine assignment and

addition operators using a shorthand operator. For example, the example statement can be written as: i += 8;

Page 11: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Shorthand Operators (2)

Page 12: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

– There are two more shorthand operators for incrementing and decrementing a variable by 1. This is handy because that is often how much the value needs to be changed.

Page 13: BBIT 212/ CISY 111   Object Oriented Programming (OOP)
Page 14: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Variable types– A variable's data type determines the values it

may contain, plus the operations that may be performed on it

– A primitive type is predefined by the language and is named by a reserved keyword. Java has eight such:• Byte, short, int, long, float, double, boolean, char

– REF: Primitive types in Java.pdf

Page 15: BBIT 212/ CISY 111   Object Oriented Programming (OOP)
Page 16: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Today

1. Objects and classes2. Object interactions3. Others

1. A sample form of the exercise from last week + introduce Flow control

2. CAT next week

Page 17: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Recap

• Exercise (in groups!)– Creating a table (as reference) to primary school

students for Perimeter and Area of a Circle

Radius Perimeter Area

7 22 154

14 44 616

21

28

35

Page 18: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• What you should know by now!– The anatomy of a class– The main method (signature, use)– Variables declaration and use

Page 19: BBIT 212/ CISY 111   Object Oriented Programming (OOP)
Page 20: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Today

1. Objects and classes2. Object interactions3. Others

1. A sample form of the exercise from last week + introduce Flow control

2. CAT next week

Page 21: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Are objects same as classes?

Page 22: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Are objects same as classes?– NO!– A class is a data structure (like a variable

definition) that will be used later on– An object is an instance (a specific use of a class)

Page 23: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• E.g.– A person class• Defines some properties that ANY person can have

name, date of birth, identification, etc• For a specific person (e.g. named “Mary” or “Tom”), we

need to create an instance of a Person and set values of person’s name, date of birth, identification, etc accordingly

– A Person Object (say Mary)• Mary, 01/01/80, 144

Page 24: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

public class Person{

String name;String dateOfBirth;int identification;

public static void main (String args [])

{ System.out.println ("This is a Peron Class");

}}

Page 25: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

public class Person{String name;String dateOfBirth;int identification;

// a method to print details of a personpublic void printDetails(){ System.out.println("Name is:" + name); System.out.println(" Born: " + dateOfBirth); System.out.println(" ID Number: " + identification );}

public static void main (String args []) { System.out.println ("This is a Peron Class"); }}

Page 26: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• How are objects created?– A class constructor is used to create instances of a

class by using the new keyword– Recall

Page 27: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

public class Person {String name;String dateOfBirth;int identification;// a method to print details of a personpublic void printDetails(){

System.out.println("Name is:" + name);System.out.println(" Born: " + dateOfBirth);System.out.println(" ID Number: " + identification );

}//Constructor a Person class that receives the person

details Person(String a,String b, int c ) {

name = a;dateOfBirth = b;identification = c;

}}

Page 28: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

public class Person {String name;String dateOfBirth;int identification;// a method to print details of a personpublic void printDetails(){

System.out.println("Name is:" + name);System.out.println(" Born: " + dateOfBirth);System.out.println(" ID Number: " + identification );

}//Constructor a Person class that receives the person details Person(String a,String b, int c ) {

name = a;dateOfBirth = b;identification = c;

}

public static void main (String args []) { //using the constructor to create an instance of a Person

Person mary = new Person("Mary", "01/01/90",144);mary.printDetails();

}}

Page 29: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Demo

Page 30: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Class constructor– Constructor name is class name. A constructors must

have the same name as the class its in– Default constructor. If you don't define a constructor

for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super() from the Object class) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

Page 31: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

– Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

– Differences between methods and constructors. There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. • There is no return statement in the body of the constructor. • The first line of a constructor must either be a call on another

constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.

Page 32: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Today

1. Objects and classes2. Object interactions3. Others

1. A sample form of the exercise from last week + introduce Flow control

2. CAT next week

Page 33: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• We will create a sub class Student in the Person class

Page 34: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

Today

1. Objects and classes2. Object interactions3. Others

1. A sample form of the exercise from last week + introduce Flow control

2. CAT next week

Page 35: BBIT 212/ CISY 111   Object Oriented Programming (OOP)

• Practice !