Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,

Similar presentations


Presentation on theme: "1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,"— Presentation transcript:

1 1 CS4850 Programming Languages Wuwei Shen

2 2 Aministrivia Course home page: http://www.cs.wmich.edu/~wwshen/cs485.html Reference books: –Programming Languages, Principles and Paradigms, Second Edition by A. Tucker & R. Noonan Office hours: 3-4pm, MW @B-256 or by appointment. Email: wuwei.shen@wmich.edu

3 3 Why Take This Course? Consider the following program public class Rectangle { private double width, height; Rectangle(double w, double h) { width = w height = h; } public double area() { return width * height; } public void setWidth(double w) { width = w;} public void setHeight(double h){ height = h;} public double getWidth() { return width; } public double getHeight() { return height;} } ;

4 4 We will learn another way to define what is a “correct” program. This new way has some good advantages. How we, as a programmer of the language, know the error in a program. So, we will learn

5 5 Continue Let us consider the following client program public class Rectangle_Client { void static main () { Rectangle r = new Rectangle(2,3); System.out.println(r.area()); Rectangle r1 = new Rectangle(3,4); r = r1; System.out.println(r.area()); } What happens to the object Rectangle(2,3)?

6 6 So, we will learn The semantics of a program: what is the correct output after you call r.area(); Garbage collection: how garbage objects are automatically collected by the operating system/Java runtime system.

7 OO Languages Inheritance vs Polymorphism public double totalPrice(Part[] parts) { double total = 0.0; for (int i =0; i < parts.length; i++) total += parts[i].getPrice(); return total; }

8 8 Next We consider a square object. What is the relationship between a rectangle object and square object? public class Square extends Rectangle { Square(double s) { super(s,s); } public void setWidth(double w) { super.setWidth(w); super.setHeight(w);} public void setHeight(double h){ super.setWidth(h); super.setHeight(h);} }

9 9 However, When we have the following client code: public class TestRectangle { public static void testLSP(Rectangle r) { r.setWidth(4.0); r.setHeight(5.0); System.out.println(r.area); } } What do you think of this code?

10 10 Another Example We have Class MySet which has two Method addAll(Vector v) and add(Object o) to add object to myset How can we implement an additional feature: account the number of added objects.

11 11 So We’ll Revisit OO Concepts We will re-evaluate important OO concepts: –Inheritance, –Polymorphism, –Composition Goal: to make the error-free software easy to write, maintain and test.


Download ppt "1 CS4850 Programming Languages Wuwei Shen. 2 Aministrivia Course home page: Reference books: –Programming Languages,"

Similar presentations


Ads by Google