Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BBIT 212/ CISY 111 Object Oriented Programming (OOP) Objects and classes, object interactions in Java 22.02.2011."— Presentation transcript:

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

2 (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.

3 22 nd Feb 2011 - RECAP Anatomy of a Java Program – Comments – Reserved words – Modifiers – Statements – Blocks – Classes – Methods – The main method

4

5

6 Recap From the notes! – Variables What they are? Declaring Initializing * – Operators Numeric operators Shorthand operators Increment and Decrement operators – Constants – Expressions, Statements and Blocks

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

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

9 (Numeric) Operators

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

11 Shorthand Operators (2)

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

13

14 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

15

16 Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

17 Recap Exercise (in groups!) – Creating a table (as reference) to primary school students for Perimeter and Area of a Circle RadiusPerimeterArea 722154 1444616 21 28 35

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

19

20 Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

21 Are objects same as classes?

22 – 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)

23 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

24 public class Person{ String name; String dateOfBirth; int identification; public static void main (String args []) { System.out.println ("This is a Peron Class"); }

25 public class Person{ String name; String dateOfBirth; int identification; // a method to print details of a person public 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"); }

26 How are objects created? – A class constructor is used to create instances of a class by using the new keyword – Recall

27 public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public 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; }

28 public class Person { String name; String dateOfBirth; int identification; // a method to print details of a person public 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(); }

29 Demo

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

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

32 Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

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

34 Today 1.Objects and classes 2.Object interactions 3.Others 1.A sample form of the exercise from last week + introduce Flow control 2.CAT next week

35 Practice !


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

Similar presentations


Ads by Google