bbit 212/ cisy 111 object oriented programming (oop) objects and classes, object interactions in...

Post on 01-Apr-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

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

22nd Feb 2011 - RECAP

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

Recap

• From the notes!– Variables

• What they are?• Declaring • Initializing *

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

– Constants– Expressions, Statements and Blocks

• 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;

– 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;

• (Numeric) Operators

• 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;

• Shorthand Operators (2)

– 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.

• 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

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

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

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

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

• Are objects same as classes?

• 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)

• 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

public class Person{

String name;String dateOfBirth;int identification;

public static void main (String args [])

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

}}

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"); }}

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

class by using the new keyword– Recall

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 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();

}}

• Demo

• 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).

– 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.

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

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

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

• Practice !

top related