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.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

1 CS October 2008 Casting About 1.Casting between classes 2.Apparent and real classes. 3.Operator instanceof Procrastination Leave nothing for to-morrow.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
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.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
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.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
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!
Java Syntax Primitive data types Operators Control statements.
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.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
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.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
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.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
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.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
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.
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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Review – Final Test Chapters 8,10,11. Locate error in following statement try i = Integer.pareseInt(str); catch(NumberFormatException e) System.out.println(“Input.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
OOP Basics Classes & Methods (c) IDMS/SQL News
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
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.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Correctness issues and Loop invariants
CS100J 21 February 2005 Casting About
Agenda Warmup AP Exam Review: Litvin A2
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Review Session.
Sorry these slides weren’t available last evening!
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
Testing change to a linked list
Polymorphism.
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
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Java Programming Language
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

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 early! Helps reducing stress Grades released the same evening (morning…) 2

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 ~290 exams so quickly 3

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 4

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

What’s in the exam? Everything you needed to know for Prelim I Vector / String class, functions Writing functions Recursive Functions Loops: for, while 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 6

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

(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) { } 8 returns a String

Recursive Function 4 Principles 1. Write the precise specification 9

/** = 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} } 10

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

/** = 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} } 12

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" 13

/** = 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} } 14 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 15

/** = 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); } 16 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); 17 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.

/** reverses a given array of ints*/ public static void rev(int[] a) { }

A word on arrays An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

//declares a variable that stores an int array name int[] a; //creates an array of size 4 a = new int[4]; //creates and initializes an array of size 3 int [] b = new int[] {1,2,3}; //stores size of array in a variable int size = b.length; //swaps first and last elements in array b int temp = b[0]; b[0] = b[1] b[1] = temp;

/** reverses a given array of ints*/ public static void rev(int[] a) { } /** reverses h..k of a given array of ints */ private static void rev_in_range(int[] a, int h,int k){ }

/** reverses a given array of ints*/ public static void rev(int[] a) { rev_in_range(a,0,a.length-1); } /** reverses h..k of a given array of ints */ private static void rev_in_range(int[] a, int h,int k){ //Base case: k-h is 0 or -1 if(k-h <= 0) return; //Recursive case }

/** reverses a given array of ints*/ public static void rev(int[] a) { rev_in_range(a,0,a.length-1); } /** reverses h..k of a given array of ints */ private static void rev_in_range(int[] a, int h,int k){ //Base case: k-h is 0 or -1 if(k-h <= 0) return; //Recursive case int temp = a[h]; a[h] = a[k]; a[k] = temp; rev_in_range(a,h+1,k-1); }

Something horribly wrong here... /** reverses a given array of ints*/ public static void rev(int[] a) { rev_in_range(a,0,a.length-1); } /** reverses h..k of a given array of ints */ private static void rev_in_range(int[] a, int h,int k){ //Base case: k-h is 0 or -1 if(k-h <= 0) return; //Recursive case int temp = a[h]; a[h] = a[k]; a[k] = temp; rev_in_range(a,h+1,k-1); }

/** reverses a given array of ints*/ public static void rev(int[] a) { rev_in_range(a,0,a.length-1); } /** reverses h..k of a given array of ints */ private static void rev_in_range(int[] a, int h,int k){ //Base case: k-h is 0 or -1 if(k-h <= 0) return; //Recursive case int temp = a[h]; a[h] = a[k]; a[k] = temp; rev_in_range(a,h+1,k-1); } Something horribly wrong here... Yes, bad indentation is horrible

Loops Part

For loops We want to write a loop that calculates the sum of squares of the elements of an array v of int s. 1) Range of integers to be processed. 2) Write postcondition. 3) Write loop. 4) Write Invariant. 5) Write Initialization. 6) Process int in the range.

Loop Invariant Invariant? Inv: x = 1+2+…+(i-1) x = 0; for (int i = 1; i <= 100; i = i+1) x = x + i;

Loop Invariant Invariant? Inv: x = sum of integers in the range 1..(i-1) x = 0; for (int i = 1; i <= 100; i = i+1) x = x + i;

Postcondition Invariant? Inv: x = sum of integers in the range 1..(i-1) Postcondition? x = 0; for (int i = 1; i <= 100; i = i+1) x = x + i;

Postcondition Invariant? Inv: x = sum of integers in the range 1..(i-1) Postcondition? Post: x = sum of integers in the range x = 0; for (int i = 1; i <= 100; i = i+1) x = x + i;

For loops We want to write a loop that calculates the sum of squares of the elements of an array of int s. // v is an array of ints. // range: 0..v.length-1 int x = 0; // inv: x = sum of squares of v[0..i-1] for (int i=0; i<v.length; i=i+1) { // process i x = x + v[i] * v[i]; } // postcondition: // x = sum of squares of all the elements of v

For loops We want to write a loop that calculates the sum of squares of the elements of an array of int s. // v is an array of ints. // range: 0..v.length-1 int x = 0; // inv: x = sum of squares of v[0..i-1] for (int i=0; i<v.length; i=i+1) { // process i x = x + v[i] * v[i]; } // postcondition: // x = sum of squares of all the elements of v

For loops We want to write a loop that calculates the sum of squares of the elements of an array of int s. // v is an array of ints. // range: 0..v.length-1 int x = 0; // inv: x = sum of squares of v[0..i-1] for (int i=0; i<v.length; i=i+1) { // process i x = x + v[i] * v[i]; } // postcondition: // x = sum of squares of all the elements of v

For loops We want to write a loop that calculates the sum of squares of the elements of an array of int s. // v is an array of ints. // range: 0..v.length-1 int x = 0; // inv: x = sum of squares of v[0..i-1] for (int i=0; i<v.length; i=i+1) { // process i x = x + v[i] * v[i]; } // postcondition: // x = sum of squares of all the elements of v

For loops We want to write a loop that calculates the sum of squares of the elements of an array of int s. // v is an array of ints. // range: 0..v.length-1 int x = 0; // inv: x = sum of squares of v[0..i-1] for (int i=0; i<v.length; i=i+1) { // process i x = x + v[i] * v[i]; } // postcondition: // x = sum of squares of all the elements of v

While loops We are given a Vector v of Integer s and a threshold t (int). We replace every value in the vector by 0 if it is <= t; by 1 otherwise. // precondition: v is a Vector of Integers // invariant: // for every j in the range 0..i-1, // v[j]=0 if the value of v at 0 was initially <= t; // v[j]=1 otherwise. int i=0; while (i < v.size()) { int x = (Integer) v.get(i); v.set(i,x <= t ? 0 : 1); i = i+1; } // postcondition: // every value in v has been replaced by 0 // if it was originally <= t, by 1 otherwise.

While loops We are given a Vector v of Integer s and a threshold t (int). We replace every value in the vector by 0 if it is <= t; by 1 otherwise. // precondition: v is a Vector of Integers // invariant: // for every j in the range 0..i-1, // v[j]=0 if the value of v at 0 was initially <= t; // v[j]=1 otherwise. int i=0; while (i < v.size()) { int x = (Integer) v.get(i); v.set(i,x <= t ? 0 : 1); i = i+1; } // postcondition: // every value in v has been replaced by 0 // if it was originally <= t, by 1 otherwise.

While loops We are given a Vector v of Integer s and a threshold t (int). We replace every value in the vector by 0 if it is <= t; by 1 otherwise. // precondition: v is a Vector of Integers // invariant: // for every j in the range 0..i-1, // v[j]=0 if the value of v at 0 was initially <= t; // v[j]=1 otherwise. int i=0; while (i < v.size()) { int x = (Integer) v.get(i); v.set(i,x <= t ? 0 : 1); i = i+1; } // postcondition: // every value in v has been replaced by 0 // if it was originally <= t, by 1 otherwise.

While loops We are given a Vector v of Integer s and a threshold t (int). We replace every value in the vector by 0 if it is <= t; by 1 otherwise. // precondition: v is a Vector of Integers // invariant: // for every j in the range 0..i-1, // v[j]=0 if the value of v at 0 was initially <= t; // v[j]=1 otherwise. int i=0; while (i < v.size()) { int x = (Integer) v.get(i); v.set(i,x <= t ? 0 : 1); i = i+1; } // postcondition: // every value in v has been replaced by 0 // if it was originally <= t, by 1 otherwise.

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

04/09/11 Apparent type: appeared type of object Animal a = new Cat(); Animal is the apparent type Real type: real type of the object Animal a = new Cat(); Cat is the real type instanceof: operator. Test the class of an object Animal a = new Cat(); (a instanceof Animal) == true (a instanceof Cat) == true

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"; } } 43

04/09/11 Movie DocumentaryTrailer Short Class Hierarchy

(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()); 45

(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()); 46 True. method equals here is from the string object

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

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"; } } 48

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 49

3. Short b= (Short)(new Documentary("", 2, "WMD")); int d= b.DocumentaryType().length(); 50 java.lang.ClassCastException: From Documentary to Short You don’t know if a documentary is a short

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

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 52

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)!!! 53

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. 54 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 55

(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) { } 56

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) { } 57

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; } 58

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; } 59 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); } 60

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

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 (…){ … } … } 62

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. 63

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 ";" 64

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 (…); } 65

Good Luck! 66