class: middle, center ## CS2030
s
### Programming Methodology II --- class: middle, center ## Lecture 2 ### 20 January 2024 --- class: middle,left ### Announcements: - Quizzes: Lecture 2, Programming Environment, Vim - Problem Set 1 - Practical: - Setup GitHub account - Link to PE - Submit your username via the online form - Exercise 0 - Labs and Recitations start this week --- ### Important Dates .smaller[ Assessment | Date | Time :------------------|:-------------------------- |:------------- Midterm | Mon, 10 Mar | 12 - 2 PM Practical Exam 1 | Thur, 6 Mar | Time: Between 4 to 10 pm Practical Exam 2 | Thur, 17 Apr | Time: Between 4 to 10 pm Final | Wed, 30 Apr | 1 - 3 PM ] --- class: middle ### The CS2030S Ticketing Systemâ„¢ [https://mysoc.nus.edu.sg/app/cs2030s](https://mysoc.nus.edu.sg/app/cs2030s) Your first choice for administrative questions and to contact the teaching team. --- class:middle, wide ### Lab and Recitation Registration Academic staff are *not* in charge of this process so appeal using CourseReg. The add / swap period ends on Mon, 20 Jan, 5pm. If you do not secure a slot: - Please submit an appeal through CourseReg under the Add/Change Tutorial Class category. - Register for any slot that has availability and does not clash with your timetable. --- class:middle, wide ### Lab and Recitation Registration Only file a ticket on the CS2030S Ticketing System if both statements below are true for you: - Cannot secure a tutorial / lab / recitation slot AFTER the Add / Swap period - Cannot appeal for any slot with vacancy, as all remaining slots with vacancy have timetable clashes. --- class: middle,left ### Topics to cover today: - Information Hiding, Tell Don't Ask - Class fields and methods - Composition - The Heap and the Stack - Inheritance - Overriding (Unit 5 to Unit 12) --- class: middle .small[```Java class Circle { double x; double y; double r; double getArea() { return 3.141592653589793 * r * r; } } ```] --- class: middle,left ### Information Hiding New Java syntax: _access modifiers_ - private - public --- class: middle New Java keyword: **this** ```Java this.x = x; ``` --- class: middle,left ### Constructor .small[```Java public Circle(double x, double y, double r) { this.x = x; this.y = y; this.r = r; } ```] --- class: middle ### Accessors and Mutators .small[```Java public double getR() { return this.r; } public void setR(double r) { this.r = r; } ``` ] --- class: middle,left ### Checking if a point is in a circle .small[```Java double cX = c.getX(); double cY = c.getY(); double r = c.getR(); boolean isInCircle = ((x - cX) * (x - cX) + (y - cY) * (y - cY)) <= r * r; ```] --- class: middle,left ### Tell, Don't Ask .small[```Java boolean isInCircle = c.contains(x, y); ```] --- class: middle,left .small[```Java c.contains(x, y); ```] `c` is called the target of method invocation `contains` --- class: middle,left ### Class Fields and Methods Java keyword `static` --- class: middle,left ### Class Fields .tiny[```Java class Math { : public static final double PI = 3.141592653589793; : : } ```] --- class: middle,left ### Class Methods .small[```Java public static int getNumOfCircles() { return Circle.lastId; } ``` ] --- class: middle, left ### `main` .small[```Java public static void main(String[] args) { System.out.println("Hello World!"); } ``` ] --- class: middle,center ### Composition ### Models the "has-a" relationship --- class: middle,center ## Stack and Heap --- class: top,center ### Aliasing --- class: middle,center ### Inheritance ### Models the "is-a" relationship In Java, we use the `extends` keyword. --- class: middle ### Our first subtyping rule for Reference Types If `S` inherits from `T`, then `S` $<:$ `T`. Reminder: Subtyping is Reflexive and Transitive --- class: middle,wide ### Subtyping with Reference Types If `ColoredCircle` $<:$ `Circle` .small[```Java ColoredCircle c = new Circle(p, 0); // error Circle c = new ColoredCircle(p, 0, blue); // OK ```] --- class: middle,wide ### Compile-Time Type vs. Run-Time Type .small[```Java Circle c = new ColoredCircle(p, 0, blue); c = new Circle(p, 0); ```] --- class: middle,wide ### Ok, but what is at the top of this class hierarchy? --- class: middle,center ### java.lang.Object ### The mother of all classes! Now a chance to have a look at the [java documentation!](https://docs.oracle.com/en/java/javase/21/docs/api/index.html) --- class: middle,center ### java.lang.Object We will discuss two methods: `toString()` `equals(Object obj)` --- class: middle,center ### Method Signature The method name, type of arguments, order of arguments, and number of arguments. --- class: middle,center ### Overriding `@Override` --- class: middle,left ### Topics covered today: - Information Hiding, Tell Don't Ask - Class fields and methods - Composition - The Heap and the Stack - Inheritance - Overriding (Unit 5 to Unit 12) --- class: middle ### Reminders: - Quizzes: Lecture 2, Programming Environment, Vim - Problem Set 1 - Practical: - Setup GitHub account - Link to PE - Submit your username via the online form - Exercise 0 - Labs and Recitations start this week --- class: middle ### Reminders: Don't just **read** the examples given... have a **play** around with them. Try changing something and see what happens. You can not learn effectively without trying it yourself! --- class: bottom .tiny[ Version: v1.0 Last Updated: Sun Jan 19 19:45:00 +08 2025 ]