CS 1110 Prelim I: Review Session Spring 2011

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 Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
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!
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
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.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
1 CS100J 18 September 2007 More on Methods. Developing methods. Also: The inside-out rule; and the use of this and super. Read sec. 2.5 on stepwise refinement.
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.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
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.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
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.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Object-Oriented Concepts
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Lecture 10 Review of classes
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
Recursion (Continued)
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
CSC 113 Tutorial QUIZ I.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Call Frames Final Review Spring 2018 CS 1110
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Class Structure 25-Feb-19.
CS360 Client/Server Programming Using Java
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS100J Classes, stepwise refinement 21 September 2005
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

CS 1110 Prelim I: Review Session Spring 2011

Exam Info Prelim 1: Tuesday March 8, 7:30–9:00PM Uris Hall G01 Look at the previous prelims available on course website Arrive early! Helps reduce stress

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

We have a lot to “cover”

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

Definitions Short answers Questions ask for definitions or something to be done Let’s see 3 examples from Prelim I, spring’07.

Definitions (a) 5 pts. What is an argument? A parameter? (b) 5 pts. What is a local variable? What is its scope?

Definitions (a) 5 pts. What is an argument? A parameter? A parameter is a variable declared in the header of a method (within the parentheses). An argument is an expression that occurs in a call of a method. (b) 5 pts. What is a local variable? What is its scope?

Definitions (a) 5 pts. What is an argument? A parameter? A parameter is a variable declared in the header of a method (within the parentheses). An argument is an expression that occurs in a call of a method. (b) 5 pts. What is a local variable? What is its scope? A local variable is a variable that is declared in the body of a method. Its scope begins at its declaration and continues until the end of the block in which it is declared.

Definitions (c) 5 pts. Explain the three steps in evaluating a new-expression —e.g. new Time(c,d) . The previous sentence contains an example of a new-expression, but your answer should explain what any new-expression does and not simply the one in the example.

Definitions 1. (c) For a new-expression new C(…): (1) Create a new object of class C; (2) execute constructor call C(…); (3) yield as value the name of the newly created object. This question asks us to explain, later we will be asked to execute!

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

Assignments Execution of an assignment statement stores a value in a variable. Assignment statement. p. 27 To execute the assignment <var>= <expr>; evaluate expression <expr> and store its value in variable <var>. x= x + 1; Evaluate expression x+1 and store its value in variable x. We’ll execute the sequence of statements on the board. v1 2 v1= 5; v2= 3; v1= v1 + 1 v1= v2 + v1 + 42 v2 4

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

This class is used on the next page /** An instance represents a Student*/ public class Student { private String name; // the student’s name private String netid; // the student’s netid /** Constructor: a Person with name n and netid i*/ public Student(String n, String i) { name= n; netid= i; } /** set the Student’s name to n */ public void setName(String n) { name= n; } /** = “this Student and s have the same netid” */ public boolean equals(Student p) { return netid.equals(p.netid); … This class is used on the next page

Evaluate “new” expressions Question. Assume the following 3 assignments are executed: Student p1= new Student(“Bill”, “bk12”); Student p2= new Student(“Bill”, “bk13”); Student p3= new Student(“William”, “bk12”); What is the value of each of the following four expressions? p1.equals(p2) p1.equals(p3) p1 == p2 p1 == p3 (b) Now consider these statements: p1= new Student(“Bill”, “bk12”); p2= new Student(“Bill”, “bk13”); p3= p2; p3.setName(“Jack”); Below, first draw all three variables. Then execute the four statements —of course, draw any objects that are created during execution. Evaluate “new” expressions

A message from Prof. Gries

A message from Prof. Gries You won’t get these questions correct unless you draw the variables, objects, and execute their assignments!

So let’s draw them… Student p1= new Student(“Bill”, “bk12”); Student p3= new Student(“William”, “bk12”); n1 p1 n2 p2 n3 p3 getName() Student(String, String) setName(String) equals(Student) toString() name netid n1 Student “Bill” “bk12” getName() Student(String, String) setName(String) equals(Student) toString() name netid n2 Student “Bill” “bk13” getName() Student(String, String) setName(String) equals(Student) toString() name netid n3 Student “William” “bk12”

(a) What is the value of each of the following four expressions? p1.equals(p2) p1.equals(p3) p1 == p2 p1 == p3

(a) What is the value of each of the following four expressions? p1.equals(p2) p1.equals(p3) p1 == p2 p1 == p3 False True

(b) Now consider these statements: p3= p2; p3.setName(“Jack”); Below, first draw all three variables. Then execute the two statements. n1 p1 n2 p2 n3 p3

(b) Now consider these statements: p3= p2; p3.setName(“Jack”); Below, first draw all three variables. Then execute the two statements. n1 p1 n2 p2 n2 p3

(b) Now consider these statements: p3= p2; p3.setName(“Jack”); Below, first draw all three variables. Then execute the two statements. n1 p1 n2 p2 n2 p3 getName() Student(String, String) setName(String) equals(Student) toString() name netid n2 Student “Jack” “bk13”

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluationof “new” expressions Evaluation/Execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name: instruction counter scope box Parameters and local variables Draw parameters as variables. number of statement of method body to execute next. Helps keep track of what statement to execute next. Start off with 1. Contains name of entity that contains the method —a file drawer or object. Put example here.

To execute the call x.setAmt(50); Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 15 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

To execute call x.setAmt(50); 1. Draw a frame for the call. newAmt a0 Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 15 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

To execute call x.setAmt(50); 1. Draw a frame for the call. newAmt 50 2. Assign the value of the argument to the parameter (in the frame). a0 Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 15 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

To execute call x.setAmt(50); 1. Draw a frame for the call. newAmt 50 2. Assign arg value to parameter (in the frame). 3. Execute method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) a0 Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 50 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

To execute call x.setAmt(50); 1. Draw a frame for the call. 2. Assign arg value to parameter (in the frame). 3. Execute method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) 4. Erase the frame for the call. a0 Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 50 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

To execute call x.setAmt(50); 1. Draw a frame for the call. 2. Assign arg value to par (in frame). 3. Execute method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) 4. Erase the frame for the call —but not on the exam! a0 Account amt setAmt(int newAmt) {amt=newAmt;} getAmt() {…} 50 x a0 Account Push “7”, the arg, on the stack. Now, draw the frame for the call. T.setP refers to method setP in the object that t is holding the name of. lhc setP: 1 rhc: because setP is not statis, it’s the name of the object (folder) that contains this non-static metho, in this case a0 (you can tell by looking at t). [Move the t” box]

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/Execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

public void m(int x, double y) { int z; z= x + y; } (d) 10 pts. Draw a frame for the call a.m(2+3, 6) of procedure m. We want to see what the frame for the call looks like after the argument values are assigned to the parameters but before the method body is executed. public void m(int x, double y) { int z; z= x + y; } m(): 1 n1 n1 a x 5 y 6

What’s on the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/Execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

= the character at position i of s Return Method Purpose char s.charAt(i) = the character at position i of s int s.length() = the number of characters in s s.indexOf(n) = the index within s of the first occurrence of String n (-1 if none) String s.substring(h,k) = a String consisting of characters in s[h..k-1], ie. s[h], s[h+1], ..., s[k-1] s.substring(h) = a String consisting of characters s[h..s.length()-1]

Q. Write function fix. This might help you: Break s into pieces, store pieces in local variables and use the pieces.   /** = Date s in a more suitable form. Precondition: s contains a date in the form month/day/year, with each part separated by "/". Examples: 4/26/39 and 04/005/1939. The output should be in the form year.month.day. Examples are: 39.26.4 and 1939.04.005. Each of day, month, and year may be any length. They appear in exactly the same form in the input and output; just their order and the separator are changed. */ public static String fix(String s) {

/. See previous slide. / public static String fix(String s) { int k= s /**See previous slide*/ public static String fix(String s) { int k= s.indexOf("/"); // index of first "/" String month= s.substring(0,k); String rest= s.substring(k+1); k= rest.indexOf("/"); // index in rest of the only "/" String day= rest.substring(0,k); String year= rest.substring(k+1); return year + "." + month + "." + day; }

What’s in the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/Execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/courses/cs1110/2010fa/exams/prelim1/aboutprelim1.pdf

/** An instance represents an instrument */ public class MusicInstrument { private String name= null; // instrument name /** Constructor: an instrument with name s */ public MusicInstrument(String s) { name= s; } /** Constructor: an instrument with name "" */ public MusicInstrument() { name= "”; } /** = sound this instrument makes*/ public String play() { return “music ”; } /** = a repr of this instrument */ public String toString() { return "Instrument: " + name; } }

/** An instance represents a string instrument with no name */ public class StringInstrument extends MusicInstrument { // number of strings on this instrument private int numStrings; /** Set the number of Strings on this instrument to n public void setNumber(int n) { numStrings= n } /** = sound this instrument makes */ public String play() { return super.toString() + numStrings + “triiings”;

Question 2 (20 points) Write a class definition for a class PercussionInstrument that Is a subclass of MusicInstrument; Has suitable specifications on methods and definitions on fields; Has a field numDrums, which is the number of drums in this percussion instrument; Has a constructor with the name of the instrument and the number of drums as parameters; Overrides function play() to return the number of “druuums”, similar to the way function play in class StringInstrument works.

1. Is a subclass of MusicInstrument; /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ }

2. Has a field numDrums, which is the number of drums in this percussion instrument; /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; }

3. Has a constructor with the name of the instrument and the number of drums as parameters; /** An instance represents …*/ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; }

/** An instance represents an instrument */ public class MusicInstrument { // Member of Congress' name private String name= null; /** Constructor: an instrument with name s */ public MusicInstrument(String s) { name= s; } /** Constructor: an instrument with name "" */ public MusicInstrument() { name= ""; /** = sound this instrument makes*/ public String play() { return “music ”; /** = a repr of this instrument */ public String toString() { return "Instrument: " + name; /** An instance represents a string instrument with no name */ public class StringInstrument extends MusicInstrument { /** number of strings on this instrument */ private int numStrings; /** Set the number of Strings on this instrument to n public void setNumber(int n) { numStrings= n } /** = sound this instrument makes */ public String play() { return super.toString() + numStrings + “triiings”;

Calling a superclass constructor from the subclass constructor public class Executive extends Employee { private double bonus; /** Constructor: name n, year hired d, salary 50,000, bonus b */ public Executive(String n, int d, double b) { super(n, d); bonus= b; } Sec. 4.1.3, page 147 a0 Object name “Gries” start 1969 salary 10,000 Employee(String, int) toString() getCompensation() toString() … Employee Executive bonus Executive(String, int, double) getBonus() getCompensation() toString() 50,000 The first (and only the first) statement in a constructor has to be a call to a constructor of the superclass. Principle: Fill in superclass fields first.

3. Has a constructor with the name of the instrument and the number of drums as parameters; /** An instance represents a percussion instr */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; /** Constructor: an instance name s with n drums */ public PercussionInstrument(String s, int n) { super(s); numDrums= n; }

4. Overrides function play() to return the number of “druuums”, similar to the way function play in class StringInstrument works. /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; /** Constructor: an instance name s with n drums */ public PercussionInstrument(String s, int n) { super(s); numDrums= n; }

/** An instance represents an instrument */ public class MusicInstrument { // Member of Congress' name private String name= null; /** Constructor: an instrument with name s */ public MusicInstrument(String s) { name= s; } /** Constructor: an instrument with name "" */ public MusicInstrument() { name= ""; /** = sound this instrument makes*/ public String play() { return “music ”; /** = a repr of this instrument */ public String toString() { return "Instrument: " + name; /** An instance represents a string instrument with no name */ public class StringInstrument extends MusicInstrument { /** number of strings on this instrument */ private int numStrings; /** Set the number of Strings on this instrument to n public void setNumber(int n) { numStrings= n } /** = sound this instrument makes */ public String play() { return super.toString() + numStrings + “triiings”;

Purpose of super and this this refers to the name of the object in which it appears. super is similar but refers only to components in the partitions above. Sec. 4.1, pages 144-145 a0 Object name “Gries” start 1969 salary 50,000.00 getName() setName(String n) {…} toString() toStringUp() { …} equals(Object) toString() Employee /** = String representation of this Employee */ public String toString() { return this.getName() + ", year ” + getStart() + ", salary ” + salary; } ok, but unnecessary /** = toString value from superclass */ public String toStringUp() { return super.toString(); } necessary

/** An instance represents an instrument */ public class MusicInstrument { // Member of Congress' name private String name= null; /** Constructor: an instrument with name s */ public MusicInstrument(String s) { name= s; } /** Constructor: an instrument with name "" */ public MusicInstrument() { name= ""; /** = sound this instrument makes*/ public String play() { return “music ”; /** = a repr of this instrument */ public String toString() { return "Instrument: " + name; /** An instance represents a string instrument with no name */ public class StringInstrument extends MusicInstrument { /** number of strings on this instrument */ private int numStrings; /** Set the number of Strings on this instrument to n public void setNumber(int n) { numStrings= n } /** = sound this instrument makes */ public String play() { return super.toString() + numStrings + “triiings”;

5. Overrides function play() to return the number of “druuums”, similar to the way function play in class StringInstrument works. /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; /** Constructor: an instance name s with n drums */ public PercussionInstrument(String s, int n) { super(s); numDrums= n; } /** = sound this instrument makes */ public String play() { return super.toString() + numDrums + "druuums";

6. Has suitable specifications on methods and definitions on fields /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; /** Constructor: an instance name s with n drums */ public PercussionInstrument(String s, int n) { super(s); numDrums= n; } /** = sound this instrument makes */ public String play() { return super.toString() + numDrums + "druuums";

6. Has suitable specifications on methods and definitions on fields Done! /** An instance represents a percussion instrument */ public class PercussionInstrument extends MusicInstrument{ // number of drums in this instrument private int numDrums; /** Constructor: an instance name s with n drums */ public PercussionInstrument(String s, int n) { super(s); numDrums= n; } /** = sound this instrument makes */ public String play() { return super.toString() + numDrums + "druuums";

What’s n the exam? Definitions of terms and key concepts Execution of assignment statements Evaluation of “new” expressions Evaluation/Execution of method calls Execute sequence of statements String functions Writing class definitions See “About the prelim” on course website http://www.cs.cornell.edu/Courses/cs1110/2011sp/exams/prelim1/aboutprelim1.pdf

Question period

Good Luck!