Midterm Exam Discussion. Lab1 Part package cs340100.homework1; abstract class Shape { public abstract double getArea(); public boolean equals(Object.

Slides:



Advertisements
Similar presentations
Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Identity and Equality Based on material by Michael Ernst, University of Washington.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Decorator Pattern & Java I/O Yoshi. Why decorator?  Once you know the techniques of decorating, you’ll be able to give your (or someone else’s) objects.
More Inheritance and LSP CS340100, NTHU Yoshi. More about Inheritance Reuse? – Q1: 你有沒有程式 ” 砍掉重練 ” 的經驗 ? – Q2: 你有沒有 ” 再造輪子 ” 的經驗 ? class Rectangle – Firstly,
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
March 2004Object Oriented Design1 Object-Oriented Design.
自動機 (Automata) Time: 1:10~2:00 Monday: practice exercise, quiz 2:10~4:00 Wednesday: lecture Textbook: (new!) An Introduction to Formal Languages and Automata,
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 6 Program Input : BufferedReader & Scanner.
課程:學位教師文憑課程 年 度 課程:學位教師文憑課程 年 度 科目: DEP5114B/G 常識科 ( 副修科 ) 科目: DEP5114B/G 常識科 ( 副修科 ) 導師:香港中文大學教育學院 麥思源教授 導師:香港中文大學教育學院 麥思源教授 學生:鄭家駒.
轉寄來的信件. Sometimes, we think of why friends keep forwarding mails to us without writing a word, maybe this could explain why.. 有時我們會想, 為什麼朋友總是不斷轉寄信件給我們但卻沒有.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
Based on OOP with Java, by D.J. Barnes 1 Review 4 View classes as modules Encapsulate operations 4 View classes as struct types Encapsulate data 4 View.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CS 307 Fundamentals of Computer ScienceInterfaces and Abstract Classes 1 Topic 7 Interfaces and Abstract Classes “I prefer Agassiz in the abstract, rather.
CS 350 – Software Design Template Method Pattern Let’s look at two objects public class Coffee { public void prepareRecipe() { public void prepareRecipe()
A PowerPoint about Algorithm’s. What is an algorithm?  a process or set of rules to be followed in calculations or other problem-solving operations,
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Inheritance, Abstract & Interface Pepper With help from and
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
Topic 4 Inheritance.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Overriding toString()
很久以前,有個孤兒叫小雪,她很喜歡畫畫,可是她很窮 沒有錢買筆 。. 有一天晚上,小雪夢見一位老奶奶送給她一枝又大又長的 毛筆。
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Type System and Object Model Horstmann ch , 7.7.
Interfaces and Inner Classes
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
A: A: double “4” A: “34” 4.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal()  0 Week.Sunday  “Sunday”
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
C11, Implications of Inheritance
Modern Programming Tools And Techniques-I
Lecture 10 Review of classes
Announcements & Review
Methods Attributes Method Modifiers ‘static’
Lecture 17: Polymorphism (Part II)
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Review Operation Bingo
null, true, and false are also reserved.
Introduction to Java Programming
Instructor: Alexander Stoytchev
Building Java Programs
Instructor: Alexander Stoytchev
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
Lecture 18: Polymorphism (Part II)
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
Instructor: Alexander Stoytchev
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Midterm Exam Discussion

Lab1 Part

package cs homework1; abstract class Shape { public abstract double getArea(); public boolean equals(Object o) { if( (o instanceof Shape) == false) { return false; } Shape otherShape = (Shape) o; if( this.getArea() == otherShape.getArea() ) { return true; } else { return false; } 在這裡, 我們不知道到 底是什麼形狀, 也依然 可以比較大小. WHY?

public abstract class DrinkMachine { Drink drink; public void boilWater() { //boil water } public abstract makeDrink(); public void pourInCup() { //pour in cup } public abstract void addFlavoring(); public Drink getDrink() { boilWater(); makeDrink(); pourInCup(); addFlavoring(); return drink; } class CoffeeMachine extends DrinkMachine { public void makeDrink() { drink = new Coffee(); } public void addFlavoring() { drink.add(new Sugar()); drink.add(new Milk()); } class TeaMachine extends DrinkMachine { public void makeDrink() { drink = new Tea(); } public void addFlavoring() { drink.add(new Lemon()); }

Advanced Part 24. String longString = “abcdefghi………..” String shortString = longString.substring(0,1); shortString causes longString cannot be freed Immutability 25. Should Square extends Rectangle?

To be inherited or not to be inherited void g(Rectangle r) { //semantic inconsistent r.setWidth(5); r.setHeight(4); if (r.getWidth() * r.getHeight() != 20) { throw new RuntimeException(); } } //some other place Rectangle square = new Square(); g(square); 1-10

26. Yes public void swap(int[] array) { int tmp = array[0]; array[0] = array[1]; array[1] = tmp; } public void m() { int[] x = new int[2]; x[0] = 0; x[1] = 1; swap(x); }

27. No Values inside array are swapped But a and b are not

28. No Reference values are copied! We cannot exchange the values of a and b in main method, only in swap method.

Overriding , 20, 15, 15, 5, 15

Compile errors Design mistakes

Challenge: Decorator Pattern This is a important design pattern which is used very often in java.io.* We will use a more detail slide to introduce it. Reference: O’Reilly Head First Design Patterns