IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

16
IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

description

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki3 How to implement class extent? Using static array Using collections Using a separate class

Transcript of IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

Page 1: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT

by Paweł Świetlicki

Page 2: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

2

Class Extent – What is it? Class extent is an actual set of all occurences of a class. Class extent is

implemented as a special data structure which stores all objects of this class.

Page 3: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

3

How to implement class extent?

Using static array Using collections Using a separate class

Page 4: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

4

Static array implementation One of the methods is to use a static array which stores all occurences of

particular class. Using this method we should remember that the array which is storing the object list should be updated every time the class is created or deleted.In this method, class extent functions ( those which works on every class instance ) should be static.

Code example:

Page 5: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

5

Code example: static arrayclass Test {

private static final int MAX_SIZE = 100;

private static Test[] extension = new Test[MAX_SIZE];private static int objectCount = 0;

private int number;

public Test(int number) {this.number = number;extension[objectCount] = this;objectCount++;}public int getNumber() {return number;}public static int sum() {int sum = 0;

for (int i = 0; i < objectCount; i++) {sum += extension[i].getNumber();}return sum;}public static void remove(Test t) {for (int i = 0; i < objectCount; i++) {if (extension[i] == t) {extension[i] = extension[objectCount - 1];extension[objectCount] = null;objectCount--;break;}}}

}

Page 6: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

6

Code example: static array (2)public class Demo {

public static void main(String[] args) {Test test1 = new Test(8);Test test2 = new Test(31);Test test3 = new Test(419);

System.out.println("The sum is " + Test.sum());

Test.remove(test1);Test.remove(test2);Test.remove(test3);

}}

Page 7: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

7

Collections A collection is a kind of data structure which ( just like arrays ) is used to

store data and perform some operations on it.In difference from arrays it is a class which includes some methods allowing some basic operations on stored data such as adding, deleting, viewing.

Collection classes in Java: HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, WeakHashMap, HashTable, Vector, Stack...

Example: ArrayList // creating collectionArrayList collection = new ArrayList();// adding object to collectioncollection.add(object);// deleting object from collectioncollection.remove(id); or remove(object);

Page 8: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

8

Collections – iterators Viewing collections is performed using iterators. Iterators enables access to

each element in a collection in specific order (generally by order of inserting the elements into the queue)

Example: IteratorArrayList collection = new ArrayList();

Integer num1 = new Integer(2);Integer num2 = new Integer(5);Integer num3 = new Integer(8);

collection.add(num1);collection.add(num2);collection.add(num3);

Iterator i = collection.iterator();while ( i.hasNext() ) {

System.out.println ( (Integer)i.next() ) ;}

Page 9: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

9

Class extension as object collection We may use static collection as class

extension, which causes much less amount of code lines and makes application working faster. Use of object collection joins the advantages of using static array and dynamic allocated static array.

Page 10: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

10

Code example: collectionclass Test {

private static ArrayList extension = new ArrayList();

private int number;

public Test(int number) {this.number = number;extension.add(this);}

public int getNumber() {return number;}

public static int sum() {int sum = 0;

Iterator it = extension.iterator();

while (it.hasNext()) {Test t = (Test) it.next();sum += t.getNumber();}

return sum;}

public static void remove(Test t) {extension.remove(t);}

}

Page 11: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

11

Implementaion of class extension using a separate class Attendance of class extension by seperate

class is organised in such a way that another (seperate) class (so called „container”) is responsible for object creating and management. Container contains its own methods that enable adding and deleting objects in another class.

Page 12: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

12

Advantages of class extension using a separate class

The greatest advantage is that programmer has control over creating and deleting class objects

Such classes have great functionality like sorting and many other

Page 13: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

13

Disadvantages of class extension using a separate class It is the most complicated way of

attendance of class extension

Page 14: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

14

Code example: separate classclass Test {

int number;

public Test(int number) {this.number = number;}

}class TestContainer {

private static ArrayList tests = new ArrayList();

public void addTest(int number) {Test t = new Test(number);tests.add(t);}public int getTestsCount() {return test.count();}public int sum() {int sum = 0;

Iterator it = tests.iterator();

while (it.hasNext()) {Test t = (Test) it.next();sum += t.getNumber();}return sum;}public void remove(Test t) {tests.remove(t);}public void removeAll() {test.clear();}

}

Page 15: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

15

Code example: separate class (2)

public class Demo {public static void main(String[] args) {

new Demo();}

public Demo() {TestContainer container = new TestContainer();

container.add(5);container.add(8);

container.remove(1);

System.out.println("The sum is " + container.sum());}

}

Page 16: IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

16

Homework Implement the extension of class Person

using static array (Person has first name, last name and age). Write a method to find the youngest person and calculate average age.