Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.

Similar presentations


Presentation on theme: "IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki."— Presentation transcript:

1 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki

2 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki2 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.

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

4 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki4 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:

5 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki5 Code example: static array class 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; }

6 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki6 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); }

7 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki7 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 collection ArrayList collection = new ArrayList(); // adding object to collection collection.add(object); // deleting object from collection collection.remove(id); or remove(object);

8 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki8 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: Iterator ArrayList 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() ) ; }

9 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki9 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.

10 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki10 Code example: collection class 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); }

11 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki11 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.

12 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki12 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

13 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki13 Disadvantages of class extension using a separate class It is the most complicated way of attendance of class extension

14 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki14 Code example: separate class class 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(); }

15 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki15 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()); }

16 IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki16 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.


Download ppt "IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki."

Similar presentations


Ads by Google