+ All Categories
Home > Education > Chapter2 array of objects

Chapter2 array of objects

Date post: 22-Jan-2017
Category:
Upload: mahmoud-alfarra
View: 260 times
Download: 0 times
Share this document with a friend
17
Object Oriented Programming Chapter 2: Array of objects Prepared by: Mahmoud Rafeek Alfarra 2016
Transcript
Page 1: Chapter2 array of objects

Object Oriented Programming

Chapter 2: Array of objects

Prepared by: Mahmoud Rafeek Alfarra

2016

Page 2: Chapter2 array of objects
Page 3: Chapter2 array of objects

Outlines◉User defined type

◉Array of User defined type (new class)

◉Passing Objects to Methods

◉Immutable Objects and Classes

◉Full Example

Note: I have prepared this material Based on (Liang: “Introduction to Programming using Java”, 10’th edition, 2015)

Page 4: Chapter2 array of objects

Lecture Let’s think on fieldes of class

3

Page 5: Chapter2 array of objects

o As the defined class is a new data type, we can defined any variables or arrays of it.

User defined type

Page 6: Chapter2 array of objects

Array of User defined type

int sal;int id;String name;…

Employee classe1

e2

e3

e4

e5

e6

e7

e8

Employee [ ] x ;

Page 7: Chapter2 array of objects

Array of User defined type

o An array of objects is actually an array of reference variables.

Circle[] circleArray = new Circle[10];

Page 8: Chapter2 array of objects

Array of User defined type

Class [ ] x ;

Add objectsDelete objectsLooking for objectsPrinting all data….

ClassTo management many

objects which are stored in array

Page 9: Chapter2 array of objects

o Passing by value for primitive type value (the value is passed to the parameter)

o Passing by value for reference type value (the value is the reference to the object)

Passing Objects to Methods

Page 10: Chapter2 array of objects

Passing Objects to Methods

Page 11: Chapter2 array of objects

o For a class to be immutable, it must mark all data fields private and provide no mutator methods and no accessor methods that would return a reference to a mutable data field object.

o If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class.

Immutable Objects and Classes

Page 12: Chapter2 array of objects

Immutable ? public class Student { private int id; private BirthDate birthDate;

public Student(int ssn, int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); }

public int getId() { return id; }

public BirthDate getBirthDate() { return birthDate; }}

public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear, int newMonth, int newDay) { year = newYear; month = newMonth; day = newDay; } public void setYear(int newYear) { year = newYear; }}

Page 13: Chapter2 array of objects

Example: ContactBook

int serial;String name;String mobile;String address;…

Contact class

int count;void search(){}void print(){}void insert(){}…

ManagArray class

e1

e2

e3

e4

e5

e6

e7

e8

Contact [ ] conArray ;

Objects of contact class

Manages the contacts

Page 14: Chapter2 array of objects

Contact bookclass Contact {

private String name; //….public Contact (String name, String mobile) { … }

}

class ManagArray {private Contact [ ] contBook; private int count;// …public ManagArray(int size){ … }public void insert(Contact c) { …. }

public void printData() { … }public void searchByName(String name) { … }

}

Page 15: Chapter2 array of objects

static int x;

class Test

obj1

X = 0X = 1

obj2

X = 2

obj3

X = 3

Now: Print Obj1.X

3

Print Obj1.X

3

Print Obj1.X

3

Page 16: Chapter2 array of objects

Practices

Group 1Give 3 examples to array of objects.

Group 2Diffrenciate between Passing by value and by reference.

Group 3Develop Immutable class.

Page 17: Chapter2 array of objects

THANKS!

Any questions?You can find me at:

Fb/mahmoudRAlfarraStaff.cst.ps/mfarraYoutube.com/mralfarra1@mralfarra


Recommended