Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Lecture 3: Initialization & Cleanup.

Similar presentations


Presentation on theme: "Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Lecture 3: Initialization & Cleanup."— Presentation transcript:

1 Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Lecture 3: Initialization & Cleanup

2 Initialization In “C”-style programming, structures were glued-together primitive types, and functions were separate. In “C”-style programming, structures were glued-together primitive types, and functions were separate. If a structure needed initialization, the programmer had to remember to do it. If a structure needed initialization, the programmer had to remember to do it. We often forgot… We often forgot… Just as bad, we also forgot to “clean up” Just as bad, we also forgot to “clean up”

3 What Needs to be Initialized? A stream for file reading needs to be attached to the file. A stream for file reading needs to be attached to the file. An array of Vectors needs to have the Vectors created (and themselves initialized). An array of Vectors needs to have the Vectors created (and themselves initialized). A Checkbox needs to have its state set, and perhaps be associated with an ActionListener. A Checkbox needs to have its state set, and perhaps be associated with an ActionListener. A Socket needs to have its IP address set. A Socket needs to have its IP address set. A Rectangle needs to have its dimensions and location set. A Rectangle needs to have its dimensions and location set. Etc. Etc.

4 What If We Forget? Things don’t act the way we expect them to! Things don’t act the way we expect them to! We only learn about problems at runtime. We only learn about problems at runtime. Maybe we don’t find out until it’s too late. Maybe we don’t find out until it’s too late. Common culprits: Common culprits: –references that lead nowhere –garbage values

5 How Does Java Help? Java initializes all class member variables to zero whenever we create an object. Java initializes all class member variables to zero whenever we create an object. Java allows us to write constructors, special methods, one of which will be called on object creation. Java allows us to write constructors, special methods, one of which will be called on object creation. Java refuses to create an object (compile error) if we haven’t provided the right kind of constructor. Java refuses to create an object (compile error) if we haven’t provided the right kind of constructor.

6 Constructors A constructor method has the same name as the class. It has no return type. A constructor method has the same name as the class. It has no return type. There can be many different constructors, each with a distinct argument signature. There can be many different constructors, each with a distinct argument signature. (This implies that overloaded methods are OK in Java.) (This implies that overloaded methods are OK in Java.) You specify the particular constructor you want when you create an object. You specify the particular constructor you want when you create an object.

7 Example Constructor class Book { String title; String author; int numPages; Book() { }// default constructor Book(String t, String a, int p) { title = t; author = a; numPages = p; }

8 Making Books Book uselessBook = new Book(); Book uselessBook = new Book(); –title is an empty character sequence –author is an empty character sequence –numPages is 0 Book usefulBook = new Book(“The TeXBook”, “Donald Knuth”, 483); Book usefulBook = new Book(“The TeXBook”, “Donald Knuth”, 483);

9 Method Overloading Methods with the same name, but different sets of arguments. Methods with the same name, but different sets of arguments. A natural idea (carWash the car? shirtWash the shirt? dogWash the dog? Nah…) A natural idea (carWash the car? shirtWash the shirt? dogWash the dog? Nah…) Constructors can be overloaded; so can any function. Constructors can be overloaded; so can any function. This is OK, but not recommended: This is OK, but not recommended: –void print(String s, int i) –void print(int i, String s) You can’t overload on the return type alone. You can’t overload on the return type alone.

10 Overloading With Primitives The compiler tries to find an exact match, but will promote (“widen”) a value if necessary. The compiler tries to find an exact match, but will promote (“widen”) a value if necessary. The compiler won’t narrow without an explicit cast. The compiler won’t narrow without an explicit cast. void doSomething(long l) { // whatever } : int i = 13; doSomething(i);

11 The Default Constructor “But Steve, how come I didn’t have to write constructors for the last homework?” “But Steve, how come I didn’t have to write constructors for the last homework?” The compiler will write one for you! The compiler will write one for you! But only if you haven’t written any constructors at all (for this class). But only if you haven’t written any constructors at all (for this class). A default constructor has no arguments (but still has the same name as the class). A default constructor has no arguments (but still has the same name as the class).

12 A Common Error The compiler gives an error. The compiler gives an error. Normally, you always provide a default constructor that does as much as possible (but not too much!). Normally, you always provide a default constructor that does as much as possible (but not too much!). class Book { String title; String author; int numPages; Book(String t, String a, int n) { title = t; author = a, numPages = n; } : Book b = new Book();

13 The this Keyword A common “C” idiom: A common “C” idiom: MusicFile f = new MusicFile(“Yardbirds”) play(&f, 4);// play the 4th track In object-oriented style, we want to “send a message” to an object, so in Java we say In object-oriented style, we want to “send a message” to an object, so in Java we sayf.play(4); The compiler knows which object (f in this case) the method is being called for. The compiler knows which object (f in this case) the method is being called for. The compiler sends this information to the method, in the form of a reference to f. The compiler sends this information to the method, in the form of a reference to f.

14 The this Keyword (cont.) If necessary, we can get a reference to the “current” object; it’s called this. If necessary, we can get a reference to the “current” object; it’s called this. public class Leaf { int i = 0; Leaf increment() { i++; return this; } void print() { System.out.println(“i = ” + i); } public static void main(String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); }

15 Other Uses of this public class Flower { int petalCount = 0; String s = new String(“null”); Flower(int petals) { petalCount = petals; } Flower(String ss) { s = ss; } Flower(String s, int petals) { this(petals); //! this(s); // can’t do it twice this.s = s; } Flower() { this(“hi”, 47); }// default constructor }

16 So, What Is A static Method? It’s a method that belongs to the class but not to any instance. It’s a method that belongs to the class but not to any instance. It’s a method “with no this”. It’s a method “with no this”. You can’t call non-static methods from within a static method. You can’t call non-static methods from within a static method. You can call a static method without knowing any object of the class. You can call a static method without knowing any object of the class.

17 Cleanup Java has a garbage collector that reclaims memory. Java has a garbage collector that reclaims memory. If an object “can’t be reached” by a chain of references from a reference on the stack (or static storage), it is garbage. If an object “can’t be reached” by a chain of references from a reference on the stack (or static storage), it is garbage. There is no guarantee that such an object will be garbage collected. There is no guarantee that such an object will be garbage collected. Garbage collection is not like destruction (in the C++ sense). Garbage collection is not like destruction (in the C++ sense).

18 Member Initialization Unitialized variables are a common source of bugs. Unitialized variables are a common source of bugs. –Using an unititialized variable in method gets a compiler error. –Primitive data members in classes automatically get initialized to “zero”. Is the initialized value (zero) any better than a “garbage value”? Is the initialized value (zero) any better than a “garbage value”?

19 Member Initialization (cont.) You can initialize in a class definition: You can initialize in a class definition: This is very surprising to C++ programmers! This is very surprising to C++ programmers! class Notebook { long ram = 1048576; String name = new String(“IBM”); float price = 1995.00; Battery bat = new Battery(); Disk d; // a null reference int i = f(); : }

20 Constructors Again You can have both class initialization and constructor initialization: You can have both class initialization and constructor initialization: The order of initialization follows the order of the initialization statements in the class definition. The order of initialization follows the order of the initialization statements in the class definition. It’s done before any constructor initialization, so it may be done twice (as Counter illustrates). It’s done before any constructor initialization, so it may be done twice (as Counter illustrates). class Counter { int i = 1; Counter() { i = 7; } :

21 Static Member Initialization Same story; primitives get zero unless initialized, references get null unless initialized. Same story; primitives get zero unless initialized, references get null unless initialized. Static initialized either Static initialized either –when the first object of the type is created, or –at the time of the first use of the variable. If you never use it, it’s never initialized. If you never use it, it’s never initialized.

22 Arrays Arrays in Java are not aggregates, as in C/C++. They are objects. Arrays in Java are not aggregates, as in C/C++. They are objects. An array is a sequence of homogeneous primitives or objects, labeled by a single name. An array is a sequence of homogeneous primitives or objects, labeled by a single name. Square brackets [ ] are used to define and access them. Square brackets [ ] are used to define and access them. Every array has a built-in read-only length attribute. Every array has a built-in read-only length attribute.

23 Array Initialization Arrays are objects, so if you say Arrays are objects, so if you say int[] idNumbers; all you get is a reference (named idNumbers). You don’t say how big the array is when you create the reference, only when you initialize it. You don’t say how big the array is when you create the reference, only when you initialize it.

24 Using Arrays Because even arrays of primitive types are objects, “strange behavior” can result: Because even arrays of primitive types are objects, “strange behavior” can result: public class ArrayTest { public static void main(String[] args) { int[] a1 = {1,2,3,4}; int[] a2; a2 = a1; for (int i = 0; i < a1.length; i++) a2[i]++; for (int i = 0; i < a1.length; i++) System.out.println("a1[" + i + "] = " + a1[i]); }

25 Using Arrays (cont.) The memory picture can be visualized like this: The memory picture can be visualized like this: 1 2344 array elements array length a1a2 before a2 = a1 1 2344 array elements array length a1a2 after a2 = a1

26 Using Arrays (cont.) If you want a copy, do it yourself: If you want a copy, do it yourself: If you index outside of the array: If you index outside of the array: for (int i = 0; i < a1.length + 1; i++) for (int i = 0; i < a1.length + 1; i++) you get a runtime exception: java.lang.ArrayIndexOutOfBoundsException: 4 at ArrayTest.main(ArrayTest.java:11) int[] a3 = new int[a1.length]; for (int i = 0; i < a1.length; i++) a3[i] = a1[i];

27 Arrays of Strings String[] names;// just a reference names = new String[3];// an array of references names[0] = "Alzadjala"; names[1] = "Areepurath"; names[2] = "Ariffin"; System.out.println("The number of students” + “in Java class is ” + names.length);

28 Variable-Length Argument Lists class A { int i; } public class VarArgs { static void f(Object[] x) { for (int i = 0; i < x.length; i++) System.out.println(x[i]); } public static void main(String[] args) { f(new Object[] {"one", "two", "three" }) ; f(new Object[] {new A(), new A(), new A() } ); f(new Object[] { new Integer(47), new VarArgs(), new Float(3.14), new Double(11.11) } ); }

29 Variable-Length Argument Lists This prints This prints47VarArgs@fee6172e3.1411.11onetwothreeA@fee61874A@fee61873A@fee6186a

30 Add toString() to Class A class A { int i; public String toString() { return new String("" + i); // or this: // return “” + i; // but not this: // return i; }

31 Multi-Dimensional Arrays From simple: From simple: int[][] a1 = { {1, 2, 3}, {4, 5, 6} }; to complex: to complex: int[][][] a4 = new int[2][][]; for (int i = 0; i < a4.length; i++) { a4[i] = new int[i+1]; a4[i] = new int[i+1]; for (int j = 0; j < a4[i].length; j++) for (int j = 0; j < a4[i].length; j++) a4[i][j] = new int[i+j]; a4[i][j] = new int[i+j];}

32 Fill The Last Array Like This for (int i = 0; i < a4.length; i++) for (int j = 0; j < a4[i].length; j++) for (int j = 0; j < a4[i].length; j++) for (int k = 0; k < a4[i][j].length; k++) for (int k = 0; k < a4[i][j].length; k++) a4[i][j][k] = i*j*k; // silly values a4[i][j][k] = i*j*k; // silly values

33 Example of a Simple Time Class public class Time { int hour; int minute; int second; Time() { setTime(0, 0, 0); } Time(int h) { setTime(h, 0, 0); } Time(int h, int m) { setTime(h, m, 0); } Time(int h, int m, int s) { setTime(h, m, s); }

34 Time Class (cont.) Time setTime(int h, int m, int s) { setHour(h); setMinute(m); setSecond(s); return this; } Time setHour(int h) { hour = (( h >= 0 && h < 24 ) ? h : 0 ); return this; }

35 Time Class (cont.) Time setMinute(int m) { minute = (( m >= 0 && m < 60 ) ? m : 0 ); return this; } Time setSecond(int s) { second = ((s >= 0 && s < 24 ) ? s : 0 ); return this; } int getHour() { return hour; } int getMinute() { return minute; } int getSecond() { return second; }

36 Time Class (cont.) public String toString() { return ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ) + ":" + ( minute < 10 ? "0" : "" ) + minute + ":" + ( second < 10 ? "0" : "" ) + second + ( hour < 12 ? " AM" : " PM" ) ; }

37 Time Class Driver public class TestTime { public static void main(String[] args) { Time t1 = new Time(); Time t2 = new Time(20, 3, 45); t1.setHour(7).setMinute(32).setSecond(23); System.out.println("t1 is " + t1); System.out.println("t2 is " + t2); }

38 Miscellaneous Topics: Recursion Joan Rivers says “I hate cleaning my house. A month later I just have to do it again!” Joan Rivers says “I hate cleaning my house. A month later I just have to do it again!” // Joan Rivers’ algorithm (pseudo-code) cleanTheHouse() { static String message = “I’m ” static String message = “I’m ” wait one month wait one month message = message + “so ” message = message + “so ” shout(message + “tired of this!”) shout(message + “tired of this!”) cleanTheHouse() cleanTheHouse()}

39 Recursion A method that calls itself. A method that calls itself. At each call, new local variables are created. At each call, new local variables are created. There must be a stopping condition! Joan doesn’t have one… There must be a stopping condition! Joan doesn’t have one… Often a natural way to express a problem. Often a natural way to express a problem. Iteration might be better, because of the overhead of function calls and extra storage. Iteration might be better, because of the overhead of function calls and extra storage. It’s not always easy to convert recursion into iteration. It’s not always easy to convert recursion into iteration.

40 Recursion (cont.) Factorials are easy: n! = n(n-1)(n-2)    1 Factorials are easy: n! = n(n-1)(n-2)    1 long factorial( long number) { if (number <= 1) // base case if (number <= 1) // base case return 1; return 1; else else return number * factorial(number - 1); return number * factorial(number - 1);}

41 Deitel & Deitel’s Illustration 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 1 returned 2! = 2*1=2 returned 3! = 3*2 = 6 returned 4! = 4*6 = 24 returned 5! = 5*24 = 120 returned Recursive calls Recursive returns

42 Let’s Try An Applet import java.awt.Graphics; import java.applet.Applet; public class FactorialTest extends Applet { public void paint(Graphics g) { int yPosition = 25; for (long i = 0; i < 10; i++) { g.drawString(i + "! = " + factorial(i), 25, yPosition); yPosition += 15; } public long factorial(long number) {…} }

43 The Output in Together In the Run Configuration dialog, select the Applet tab!

44 In the Command Line World In an editor, create a file containing: code=“FactorialTest.class” width=235 height=250> Save it as Factorial.html

45 The Output in AppletViewer D:\Together5.02\out\classes\FactorialApplet>appletviewer Factorial.html


Download ppt "Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Lecture 3: Initialization & Cleanup."

Similar presentations


Ads by Google