Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.

Similar presentations


Presentation on theme: "Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder."— Presentation transcript:

1 Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder about Stacks & Queues, and an example of using stacks.

2 Programming Exercises Only 3 exercises, but they are long. You should divide the work to 3 stages: –Designing your program structure –Programming (of-course). –Debugging (will take a lot of time). Ex1 is on the web page, start working on it!

3 Primitive and Reference DT Primitive DT: ( boolean, int, float, etc.) each is stored in a unique memory place: int a=5; int b=a; a = 6; So after this command sequence b will have a value of 5. (This is called copy “by value”). ab

4 Primitive and Reference DT Reference DT: (all objects) a variable points to a memory place created by the new command. Many variables may point to the same memory place Employee a = new Employee(); a.ID = 5; Employee b = a; a.ID = 6; So after this command sequence b.ID will have a value of 6. (by changing a we also changed b ). ID b a

5 Primitive and Reference DT When we pass an object as an argument to a method, a new reference to the same object is created. When we pass primitive DT to a method, a new variable is created. If an object variable has the value null, this actually means: “this variable does not point to any memory place” How do we make an actual copy of the object, not another reference to same object? This is called cloning.

6 Cloning Cloning – The Java way of making a copy of an object. Employee a = new Employee(); a.ID = 5; if ( a instanceof Cloneable ) { Employee b = a.clone(); a.ID = 6; } Now b is a reference to a new object (identical to a ) A class that provides the clone() method should implement the Cloneable interface. We can check if a class is Cloneable by using the instanceof operator.

7 How to be “cloneable” Class Object contains the method clone(), which we override. Class Object implements clone() as a bit-by-bit memory copy. public class Employee implements Cloneable { public int ID; public Object clone() { try { return super.clone(); } catch (CloneNotSupportedExcetion e) { } // this catch is not supposed to happen. }

8 Cloning Method Object.clone() throws CloneNotSupportedException if the class does not implement Cloneable. Therefore, if you want to use Object.clone() you should nest it in a try - catch block. Method Object.clone() is declared protected, therefore you must override the clone() method, declaring it public. public class Employee implements Cloneable { public int ID; public Object clone() { try { return super.clone(); } catch (CloneNotSupportedExcetion e) { } // this catch is not supposed to happen. }

9 Cloning - “deep” and “shallow” copy Shallow/Deep copy – Copies by reference/value the object data members. For example: A B A B A1 A B B1 Shallow copy Deep copy Original

10 “deep” and “shallow” copy Notice that Object.clone() performs shallow copy For example, Java’s Vector implements shallow copy: Emp e1 = new Emp(); Emp e2 = new Emp(); e1.id = 1; e2.id = 2; Vector v1 = new Vector(); v1.addElement(e1) ; v1.addElement(e2); Vector v2 = v1.clone(); Then: ((Emp)v2.elementAt(0)).id = 3; System.out.println(((Emp)v1.elementAt(0)).id); will print 3, but: v2.removeElementAt(0); System.out.println(((Emp)v1.elementAt(0)).id); will still print 3.

11 Cloning vs. Copy Constructor Copy constructors can be used to solve the same problem as cloning. They play an important role in languages (e.g. C++) where objects can be passed by value as well as by reference. In Java, although you can use both techniques, cloning is more general. For example, a deep copy of a list of objects of different types. There is no way of knowing what kind of copy constructor should be called for each element, but the clone() method makes sure you get the right copy of each.

12 Inner classes Motivation: Suppose you need an iterator class for your LinkedList class. Defining a new class solely for this purpose complicates your package structure. Furthermore - this class must get a handler to a specific LinkedList instance and it can’t access it’s private data members. And, there is such a class for every DS. The solution : Inner classes. Useful for simple “helper” classes that serve a very specific function at a particular place in the program, and are not intended to be general purpose “top level” classes. They make your code clearer, and prevent cluttering your package namespace.

13 Inner classes - Example & Syntax public class LinkedList { private Node head;... public Iterator iterator() { return new ListIterator() }; private class ListIterator implements Iterator { Node current; public ListIterator () { current = head; } public boolean hasNext() {...} public Object next() {... } } // end class ListIterator } // end class LinkedList

14 Packages - reminder Java classes are organized in packages to help organize and share programs and projects. Examples: java.util, java.io The import keyword extends the scope of the program to contain (part of) a specific package. We can build our own packages, using these guidelines: –Locate all package classes in a subdirectory with the same name as the package. –The first line of a class of some package should be: package package_name; –Set the CLASSPATH variable to point to the directory where the package subdirectory resides. For example, to use the package dast.util that resides in the subdirectory ~dast/www/public/dast/util you should add the path ~dast/www/public to your CLASSPATH variable.

15 Stacks & Queues Stack – implements the Last In First Out (LIFO) order. Usually the two basic operations are called push() and pop(). Queue – implements the First In First Out (FIFO) order. Usually the two basic operations are called enqueue() and dequeue(). Both can be implemented efficiently by using either an array or a linked list. More details about Queues in the next tirguls.

16 Using Stacks to Eliminate Recursion Towers of Hanoi, without recursion… push( Hanoi(‘s’, ‘t’, ‘m’, k) ) while ( stack not empty ) x = pop(); if ( x is of type Hanoi ) if ( x.discs > 1 ) push( Hanoi(x.middle, x.to, x.from, x.discs-1) ) push( Move(x.from, x.to) ) push( Hanoi(x.from, x.middle, x.to, x.discs-1) ) else push( Move(x.from, x.to) ) else // x is of type Move print( x.from + ‘ -> ’ + x.to); Elements in stack are of two types: Hanoi(from, to, middle, discs) Move(from, to)

17 Example H(s,t,m,3)H(m,t,s,2) M(s,t) H(s,m,t,2 Top of stack H(m,t,s,2) M(s,t) H(t,m,s,1) H(s,t,m,1) M(s,m) Output s  t s  m t  m s  t m  s m  t s  t H(s,t,m,1) M(m,t) H(m,s,t,1)


Download ppt "Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder."

Similar presentations


Ads by Google