Download Administrivia - Lecture Slides | CMSC 433 and more Exams Programming Languages in PDF only on Docsity! CMSC 433, Alan Sussman, U. Maryland (via Bill Pugh) 1 CMSC433, Spring 2001 Programming Language Technology and Paradigms Basic Java, continued Alan Sussman September 27, 2001 CMCS 433, Fall 2001 - Alan Sussman 2 Administrivia • C++ project – private drivers posted soon – other student’s code will be sent to you for commentary • 2 projects, about a paragraph for each • Java project – due date moved to Friday, Oct. 5 – web server is up and running for testing – questions? • First exam moved back 2 days to Thurs., Oct. 11 – practice exam handed out next week CMCS 433, Fall 2001 - Alan Sussman 3 Administrivia (cont.) • More Java suggested readings – I/O – Chapter 11, pages 573-605 – RTTI – Chapter 12 – Distributed Computing – Chapter 15, pages 903-923 CMCS 433, Fall 2001 - Alan Sussman 4 C++ project – post mortem • IntEArray good design simplifies problem – use a separate class for the array representation • shared, with reference count • also need to keep track of which parts are shared – use list of (start, end) pairs • can’t keep a ref count for each array element – then operator= and copy constructor aren’t constant time – nor is operator() – each IntEArray has a pointer to one array representation object CMCS 433, Fall 2001 - Alan Sussman 5 Example Object Model int [] data int refCount = 2 watchNode * watchList Address a start = 0 end = 10 Address b start = 3 end = 7 IntEArray a,b; a[10] = -1; b = a(3,7); a b CMCS 433, Fall 2001 - Alan Sussman 6 C++ project (cont.) • ElementRef is just a placeholder for an element of an IntEArray – Every use of IntEArray’s operator[] returns an ElementRef • on either a left hand side, or a right hand side – It contain just a pointer/reference to its IntEArray and the index it represents – all work is done in the member functions • operator= • operator int() CMSC 433, Alan Sussman, U. Maryland (via Bill Pugh) 2 CMCS 433, Fall 2001 - Alan Sussman 7 Example Object Model Address a start = 0 end = 10 Address b start = 3 end = 7 IntEArray a,b; a[10] = -1; b = a(3,7); a[4] = b[0]; a b theArray index = 4 theArray index = 0 int [] data int refCount = 2 watchNode * watchList CMCS 433, Fall 2001 - Alan Sussman 8 Last time - Java • Arrays – are objects, with special syntax – space allocated with new, just like other objects • Classes – can extend only one class – can implement many interfaces • Methods – can be overloaded, and overridden – pretty much always use virtual method semantics – all parameters pass-by-value, even object references CMCS 433, Fall 2001 - Alan Sussman 9 Overriding Methods • Overriding – methods with same name and argument types in child class override method in parent class – you can override/hide instance variables • both variables will exist, but don’t do it class Parent { int cost; void add(int x) { cost += x; } } class Child extends Parent { void add(int x) { if (x > 0) cost += x; } } CMCS 433, Fall 2001 - Alan Sussman 10 Overloading • Methods with the same name, but different parameters (count or types) are overloaded class Parent { int cost; void add (int x) { cost += x; } } class Child extends Parent { void add (String s) throws NumberFormatException { cost += Integer.parseInt(s); } } CMCS 433, Fall 2001 - Alan Sussman 11 Dynamic Method Dispatch • If you have a ref a of type A to an object that is actually of type B (a subclass of A) – instance methods invoked on a will get the methods for class B (like C++ virtual functions) – class (static) methods invoked on a will get the methods for class A • invoking class methods on objects strongly discouraged CMCS 433, Fall 2001 - Alan Sussman 12 Simple Dynamic Dispatch Example class A { String f() {return “A.f() “; } static String g() {return “A.g() “; } } public class B extends A { String f() {return “B.f() “; } static String g() {return “B.g() “; } public static void main(String args[]) { A a = new B(); B b = new B(); System.out.println(a.f() + a.g() + b.f() + b.g()); } } java B generates: B.f() A.g() B.f() B.g()