21
CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th , 2011 Instructor: Scott Settembre

CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

Embed Size (px)

Citation preview

Page 1: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

CSE 113Introduction to

Computer Programming

Lecture slides for Week 12

Monday, November 14th, 2011Instructor: Scott Settembre

Page 2: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

2

COURSE ADMINISTRATIONSection 1

Monday, Nov. 14th, 2011

Page 3: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

3

For Project Assistance• You can find the up-to-date hours and locations in the

“Contact” section of UBLearns.

• Here are the names, emails, and office hours as of 11/14/2011: (Also come to additional labs if you need help)

Monday, Nov. 14th, 2011

Name Email address Office hours

Bich (pronounced “Bic”) Vu [email protected] M (11-11:50am) Bell 329F (12-12:50pm) Bell 329

Troy Koss [email protected] Tues (3-5pm) Bell 329

Scott Settembre [email protected] M (10-11am) Bell 232F (10-11am) Bell 340Also at all times by email AND video conference by appointment.

Page 4: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

4

You are behind the class if…

• If you have not:– completed chapter 1-6, then you are behind the rest of

the class.– loaded the project 2 Greenfoot project and

experiemented with a new AIMower class.

• Please do the following:– Begin working on project 2.– Start on chapter 7 in this week’s lab.

Monday, Nov. 14th, 2011

Page 5: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

5

Lecture and Lab this Week

• Lectures will go over the following:– Project 2– switch statement– for loop

• Lab will have you do the following:– Start chapter 7.– Continue your project 2.– Lab Quiz on chapter 6 and Lecture notes from week

10 with for loops and switch statements.Monday, Nov. 14th, 2011

Page 6: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

6

MIDTERM / FINAL EXAM / PROJECTSection 2

Monday, Nov. 14th, 2011

Page 7: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

7

Midterm exam (15%)

• The midterm exam will be held in lecture on October 21st.

• It will cover chapters 1-6.4, including the exercises in the chapter that you do in lab.

• It will be a series of true/false, multiple choice, and code examination (running the code in your head), similar to the quizzes that you will have taken.

• Grades have been released.

Monday, Nov. 14th, 2011

Page 8: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

8

Final Exam (25%)

• The final exam will be during class during the last week of class.

• It will span two days, 45 minutes (or less) each day.

• It will consist of questions like the quizzes, as well as some code reading and understanding.

• More on this in November.

Monday, Nov. 14th, 2011

Page 9: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

9

Project 1

• Project 1 grades have been posted.

• Good job!

Monday, Nov. 14th, 2011

Page 10: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

10

Project 2

• Due date: November 22nd, 2011 at or before 11:59:59 PM

• We will design the abstract class we will be using in class, together.

• The project description will be discussed on Monday and Wednesday.

• The abstract class and Greenfoot project you will use will be distributed in lab.

Monday, Nov. 14th, 2011

Page 11: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

11

CHAPTER 6Section 3

Monday, Nov. 14th, 2011

Page 12: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

12

The “for” loop• Since counter loops are used so much, there is a quick and easy way

to do them, the “for” loop. (sometimes called the “for..next” loop)

• Here is an example:for (int count=1; count <= 3; count++) {

displayOutput("count == " + count);}displayOutput("Loop complete.“);

• Here is the output:

count == 1count == 2count == 3Loop complete.

Monday, Nov. 14th, 2011

Page 13: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

13

Components of a “for” loop

• You have the keyword “for” followed by an “expression” followed by a “statement” (or block).

• The expression contains 3 statements– First, a declaration or initialization that is run only once at the

beginning of the loop.– Second, a Boolean expression or condition to evaluate to see if the

loop should still run. – Lastly, an action to perform, usually an increment or iteration of

some sort, performed at the end of the loop.

• “for” loops are quite nice to use as they cut down on the code clutter.

Monday, Nov. 14th, 2011

Page 14: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

14

Compare the “while” to “for”int count=0;while (count++ < 3) {

displayOutput("count == " + count);

}displayOutput("Loop complete.");

for (int count=1; count < 3; count++) {

displayOutput("count == " + count );

}displayOutput("Loop complete." );

Monday, Nov. 14th, 2011

Page 15: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

15

Collision Detection (not on test)

• Bounding-box

• Bounding-circle

Monday, Nov. 14th, 2011

Check each of the corners to see if it is inside another bounding box

Make sure the two centers of each circle are greater than r1 + r2 away from each other

Page 16: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

16

Collision Detection (not on test)

• Using masks– First, use bounding-boxes and determine all

bounding-box collisions.

– Second, take the masks from each sprite that had a bounding-box collision and see if they overlap.

Monday, Nov. 14th, 2011

It is fast to detect if rectangles overlap.

It is slow to check before you set every pixel to see if it is part of the mask.

Page 17: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

17

Drawing

• There are many Greenfoot commands that you can use to draw on the image of an Actor or the World.

• (Look at GreenfootImage class)

Monday, Nov. 14th, 2011

Page 18: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

18

Easy way to draw on the background

• Get the World using getWorld()– (or if in the World class, just use “this”)

• Get the background image.– If in the World class, you would do

use .getBackground()

• Draw a line.– If in an actor, you could write:

• getWorld().getBackground().drawLine(0,0,100,100);

Monday, Nov. 14th, 2011

Page 19: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

19

Efficient Code

• Calling getWorld() and getBackground() each time you want to do something, can slow down performance.

• Try capturing the background in a private instance variable once, then using it later.– For example, in the class, make an instance variable

• private GreenfootImage myBackground;

– Then in your constructor method or act method, you can:• if (myBackground == Null) {

myBackground = getWorld().getBackground();}

Monday, Nov. 14th, 2011

Page 20: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

20

Writing text

// Get the background image, so we can write on itGreenfootImage scoreboard = getBackground();

// Clear what was written beforescoreboard.setColor (new Color(0,153,0));scoreboard.fillRect(0,0,120,40);

// Change color to black, then write new infoscoreboard.setColor(new Color(0,0,0));scoreboard.drawString("Time: " + iterations++, 10, 10);Monday, Nov. 14th, 2011

Page 21: CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

21

Objects and casting(more on this later)

• Objects may have more than one type!– For example, a “Dog” object is both of the “Dog”

class and the “Animal” class and the “Actor” class.

– If we want to make a list of Actors who are dogs and cats, we may say:• List<Dog> myPets;• But then if one of the pets is of class “Cat”, then we will

have a run-time error.• Instead, we may the list like so: “List <Animal> myPets;”

Monday, Nov. 14th, 2011