CS 1110 Prelim II: Review Session 1. Introduction My name: Bruno Abrahao – We have three additional TA’s in the room to help you individually Shuang Nam.

Slides:



Advertisements
Similar presentations
1 CS October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.
Advertisements

CS 1110 Prelim II: Review Session 1. (Fall’07) Question 1 (15 points). Write the body of the following function recursively. /** = n, but with its digits.
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 1110 Prelim II: Review Session 1. Exam Info Prelim 1: 7:30–9:00PM, Tuesday, Nov 9 th, Olin 155 (Last name starts with A-Lewis) and Olin 255 (Last name.
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 1110 Prelim I: Review Session. Introduction My name: Bruno Abrahao – We have four TA’s in the room to help you individually Shuang Zhao Nam Nguyen.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
1 CS100J 27 February 2006 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
LECTURE 07 Programming using C# Inheritance
Multiple Choice Solutions True/False a c b e d   T F.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Polymorphism & Interfaces
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
What is inheritance? It is the ability to create a new class from an existing class.
CS1101: Programming Methodology Aaron Tan.
CS 1110 Prelim II: Review Session 1. Exam Info Prelim II: 7:30–9:00PM, Tuesday, 8 November, Baker Lab 200, 219, 119 Look at the previous Prelims Arrive.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
Overriding toString()
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
More about inheritance Exploring polymorphism 5.0.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
OOP Basics Classes & Methods (c) IDMS/SQL News
1 CS100J 27 September 2005 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
Lecture 12 Inheritance.
CS100J 21 February 2005 Casting About
Agenda Warmup AP Exam Review: Litvin A2
CS1110 Classes, stepwise refinement 23 Sep 2009
Recursion (Continued)
Sorry these slides weren’t available last evening!
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
CS100J 28 February 2008 Casting About
The fattest knight at King Arthur's round table was Sir Cumference
CS 302 Week 11 Jim Williams, PhD.
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Building Java Programs
Recursion (Continued)
Sit next to someone. Today, we do some work in pairs.
Chapter 9 Carrano Chapter 10 Small Java
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Review of Previous Lesson
Review of Previous Lesson
CS 1110 Prelim I: Review Session Spring 2011
Presentation transcript:

CS 1110 Prelim II: Review Session 1

Introduction My name: Bruno Abrahao – We have three additional TA’s in the room to help you individually Shuang Nam Yookyung – You’re welcome to ask them questions at any time 2

Exam Info Prelim II: 7:30–9:00PM, Thursday, 12 March, Uris Hall G01 Look at the previous Prelims Arrive early! Helps reducing stress Grades released the same evening (morning…) 3

Regrade Requests Releasing grades quickly is good for you — exams serve two purposes: – Give feedback to student and teacher – Give grades That’s one reason we grade ~180 exams so quickly 4

Review session Let’s make this interactive – More fun Presentation is at slower pace than a regular lecture Ask questions – All questions are smart ones 5

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions apparent/real classes, casting, operator instanceof, function equals Abstract classes and methods 6

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions apparent/real classes, casting, operator instanceof, function equals Abstract classes and methods I’m gonna assume you can do this with your eyes closed by now 7

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions apparent/real classes, casting, operator instanceof, function equals Abstract classes and methods 8

(Fall’07) Question 1 (15 points). Write the body of the following function recursively. /** = n, but with its digits reversed. Precondition: n >= 0. e.g. n = , value is "027531". e.g. n = 12345, value is "54321". e.g. n = 7, value is "7". e.g. n = 0, value is "0".*/ public static String rev(int n) { } 9 returns a String

Recursive Function 4 Principles 1. Write the precise specification 10

/** = n, but with its digits reversed. Precondition: n >= 0. e.g. n = , value is "027531". e.g. n = 12345, value is "54321". e.g. n = 7, value is "7". e.g. n = 0, value is "0".*/ public static String rev(int n) { // base case: //{n has only one digit} // recursive case: // {n has at least two digits} } 11

Recursive Function 4 Principles 1. Write the precise specification 2. Base Case 12

/** = n, but with its digits reversed. Precondition: n >= 0. e.g. n = , value is "027531". e.g. n = 12345, value is "54321". e.g. n = 7, value is "7". e.g. n = 0, value is "0".*/ public static String rev(int n) { // base case: //{n has only one digit} if (n < 10) // recursive case: // {n has at least two digits} } 13

Let’s review some type issues What is the type of? 42 "" + 42; ‘a’ + ‘b’ ‘b’ + "anana" ‘b’ + ‘a’ + "nana" ‘b’ + (‘a’ + "nana") "" + ‘b’ + ‘a’ + "nana" 14

/** = n, but with its digits reversed. Precondition: n >= 0. e.g. n = , value is "027531". e.g. n = 12345, value is "54321". e.g. n = 7, value is "7". e.g. n = 0, value is "0".*/ public static String rev(int n) { if (n < 10) return "" + n; // recursive case: // {n has at least two digits} } 15 base case: n has 1 digit

Recursive Function 4 Principles 1. Write the precise specification 2. Base Case 3. Progress – Recursive call, the argument is “smaller than” the parameter. Ensures base case will be reached (which terminates the recursion) 4. Recursive case 16

/** = n, but with its digits reversed. Precondition: n >= 0. e.g. n = , value is "027531". e.g. n = 12345, value is "54321". e.g. n = 7, value is "7". e.g. n = 0, value is "0".*/ public static String rev(int n) { if (n < 10) return "" + n; // n has at least 2 digits return (n%10) + rev(n/10); } 17 base case: n has 1 digit recursive case:

/** = the reverse of s.*/ public static String rev(String s) { if (s.length() <= 1) return s; // { s has at least two chars } int k= s.length()-1; return s.charAt(k) + rev(s.substring(1,k)) + s.charAt(0); 18 recursive case base case Do this one using this idea: To reverse a string that contains at least 2 chars, switch first and last ones and reverse the middle.

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions apparent/real classes, casting, operator instanceof, function equals Abstract classes and methods 19

Administrivia Please remember to fill out your TA evals. – Open untill Friday 13 – Good for you, good for us – You can give feedback to any TA (not only your lab instructor) A TA who taught you something A TA that inspired you A TA who you think needs to improve some aspect … 20

CS1110 Flix 21

public class Movie { private String title; // title of movie private int length; // length in minutes /** Constructor: document with title t and len minutes long */ public Movie(String t, int len) { title= t; length= len; } /** = title of this Movie */ public String getTitle() { return title; } /** = length of document, in minutes */ public int getLength() { return length; } /** = the popularity: shorter means more popular */ public int popularity() { return 240 – length; } } public class Trailer extends Movie { /** Constructor: a trailer of movie t. Trailers are 1 minute long*/ public Trailer(String t) {super(t, 1);} } public class Documentary extends Movie { private String topic; // … /** Constructor: instance with title t, length n, and topic p */ public Documentary(String t, int n, String p) { super(t, n); topic= p; } /** = "Documentary" */ public String DocumentaryType() { return "Documentary"; } /** = popularity of this instance */ public int popularity() { return getLength(); } } public class Short extends Documentary { /** Constructor: instance with title t, length n, and topic p */ public Short(String t, int n, String p) { super(t, n, p); } /** displays acknowledgement */ public String showAck() {return "We thank our director“;} /** = "Short Doc" */ public String DocumentaryType() { return "Short Doc"; } } 22

(Fall’05) Question 4 (30 points) For each pair of statements below, write the value of d after execution. If the statements lead to an error, write “BAD” and briefly explain the error. (The question continues on the next page.) Documentary e= new Short("Man on Wire”, 5, "Bio"); boolean d= "Short Doc”.equals(e.DocumentaryType()); 23

(Fall’05) Question 4 (30 points) For each pair of statements below, write the value of d after execution. If the statements lead to an error, write “BAD” and briefly explain the error. (The question continues on the next page.) Documentary e= new Short("Man on Wire”, 5, "Bio"); boolean d= "Short Doc”.equals(e.DocumentaryType()); 24 True. method equals here is from the string object

2. Movie c= new Documentary(null, 3, "Carter Peace Center"); int d= c.popularity(); 25

public class Movie { private String title; // title of movie private int length; // length in minutes /** Constructor: document with title t and len minutes long */ public Movie(String t, int len) { title= t; length= len; } /** = title of this Movie */ public String getTitle() { return title; } /** = length of document, in minutes */ public int getLength() { return length; } /** = the popularity: shorter means more popular */ public int popularity() { return 240 – length; } } public class Trailer extends Movie { /** Constructor: a trailer of movie t. Trailers are 1 minute long*/ public Trailer(String t) {super(t, 1);} } public class Documentary extends Movie { private String topic; // … /** Constructor: instance with title t, length n, and topic p */ public Documentary(String t, int n, String p) { super(t, n); topic= p; } /** = "Documentary" */ public String DocumentaryType() { return "Documentary"; } /** = popularity of this instance */ public int popularity() { return getLength(); } } public class Short extends Documentary { /** Constructor: instance with title t, length n, and topic p */ public Short(String t, int n, String p) { super(t, n, p); } /** displays acknowledgement */ public String showAck() {return "We thank our director“;} /** = "Short Doc" */ public String DocumentaryType() { return "Short Doc"; } } 26

27 a0 Animal Cat Cat(String, int) getNoise() toString() getWeight() age Animal(String, int) isOlder(Animal) 5 QUESTION: Which method is called by Animal t= new Cat(“A”,5); t.toString() ? A.the one in the hidden partition for Object of a0 B.the one in partition Animal of a0 C.the one in partition Cat of a0 D.None of these Object Animal Cat the class hierarchy:

2. Movie c= new Documentary(null, 3, "Carter Peace Center"); int d= c.popularity(); What is the apparent class? Answer: 197. method popularity of class Documentary is called Movie DocumentaryTrailer Short 28

3. Short b= (Short)(new Documentary("", 2, "WMD")); int d= b.DocumentaryType().length(); 29

3. Short b= (Short)(new Documentary("", 2, "WMD")); int d= b.DocumentaryType().length(); Movie DocumentaryTrailer Short From documentary, can go (cast) up and back down to documentary. Think what would happen for the call b.showAck() 30

3. Short b= (Short)(new Documentary("", 2, "WMD")); int d= b.DocumentaryType().length(); Movie DocumentaryTrailer Short From documentary, can go (cast) up and back down to documentary. Think what would happen for the call b.showAck(). 31 Answer: BAD

4. Movie a= (Movie)(new Trailer("Harry Potter")); int d= a.popularity(); The cast is legal! Which popularity() method is called? Movie DocumentaryTrailer Short 32

4. Movie a= (Movie)(new Trailer("Harry Potter")); int d= a.popularity(); The cast is legal! Method popularity() from Movie is called (inherited by Trailer) Answer: 239 Movie DocumentaryTrailer Short 33

5. Movie f= new Short("War", 1, "Vietnam"); char d= f.DocumentaryType().charAt(1); The methods that can be called are determined by the apparent type: Only components in the apparent class (and above)!!! 34

5. Movie f= new Short("War", 1, "Vietnam"); char d= f.DocumentaryType().charAt(1); The methods that can be called are determined by the apparent type: Only components in the apparent class (and above)!!! f.DocumentaryType() is illegal. Syntax error. 35 Answer: BAD

Recap: equals(Object ob) In class Object – b.equals(d) is the same as b == d Unless b == null (why?) Most of the time, we want to use equals to compare fields. We need to override this method for this purpose 36

(Fall’05) Question 4 (24 points). (a) Write an instance method equals(Object obj) for class Documentary public class Documentary extends Movie { /** = "obj is a Documentary with the same values in its fields as this Documentary" */ public boolean equals(Object obj) { } 37

public class Documentary extends Movie { /** = "obj is a Documentary with the same values in its fields as this Documentary" */ public boolean equals(Object obj) { if ( !(obj instanceof Documentary) { } 38

public class Documentary extends Movie { /** = "obj is a Documentary with the same values in its fields as this Documentary" */ public boolean equals(Object obj) { if ( !(obj instanceof Documentary) { return false; } 39

public class Documentary extends Movie { /** = "obj is a Documentary with the same values in its fields as this Documentary" */ public boolean equals(Object obj) { if ( !(obj instanceof Documentary) { return false; } Documentary docObj= (Documentary)obj; } 40 Don’t forget to cast. This is a legal cast. (Why?)

public class Documentary extends Movie { /** = "obj is a Documentary with the same values in its fields as this Documentary" */ public boolean equals(Object obj) { if (!(obj instanceof Documentary) { return false; } Documentary docObj= (Documentary)obj; return getTitle().equals(docObj.getTitle()) && getLength() == docObj.getLength() && topic.equals(docObj.topic); } 41

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions apparent/real classes, casting, operator instanceof, function equals Abstract classes and methods 42

Let’s capture the essence of animals /** representation of an animal */ public class Animal { private int birthDate; // animal’s birth date private String predator; // predator of this animal private String prey; // class of animals this hunts … // move the animal to direction… public void move(…){ … } // make the animal eat… public void eat (…){ … } … } 43

Problems Animal is an abstract concept – Creating an abstract animal doesn’t make sense in the real world – Dogs, cats, snakes, birds, lizards, all of which are animals, must have a way to eat so as to get energy to move However… – Class Animal allows us to create a UFA (unidentified flying animal), i.e. instance of Animal – If we extend the class to create a real animal, nothing prevent us from creating a horse that doesn’t move or eat. 44

Solutions How to prevent one from creating a UFA? – Make class Animal abstract Class cannot be instantiated – How? Put in keyword abstract How to prevent creation paralyzed dogs or starving sharks? – Make the methods move and eat abstract Method must be overridden – How? Put in keyword abstract and replace the body with ";" 45

Making things abstract /** representation of an animal */ public abstract class Animal{ private int birthDate; // birth date private String predator; // animal’s predator private String prey; // What animal hunts … // Move the animal move in direction … public abstract void move(…); // Make the animal eat… public abstract void eat (…); } 46

Good Luck! 47