34
CSE 113 Introduction to Computer Programming Lecture slides for Week 6 Monday, October 3 rd , 2011 Instructor: Scott Settembre

CSE 113 Introduction to Computer Programming Lecture slides for Week 6 Monday, October 3 rd, 2011 Instructor: Scott Settembre

Embed Size (px)

Citation preview

CSE 113Introduction to

Computer Programming

Lecture slides for Week 6

Monday, October 3rd, 2011Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

2

COURSE ADMINISTRATIONSection 1

Monday, Oct. 3rd, 2011

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 9/26/2011: (Also come to additional labs if you need help)

Monday, Oct. 3rd, 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.

University at Buffalo: CSE 113 Instructor: Scott Settembre

4

You are behind the class if…

• If you have not completed chapter 1-4, then you are behind the rest of the class.

• Please do the following:– Complete chapter 1-4 in Bell 101.– Chapter 5 must be finished by the end of this

week.

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

5

Lecture and Lab this Week

• Lectures will go over the following:– Lab Quiz for Week 5– Chapter 5 concepts and more formal definitions

• arrays (or lists of objects)• strings (and string manipulation)• icons, jpg, gif, png file differences (not in book yet)

– Starting chapter 6.

• Lab will have you do the following:– Finish completely chapters 1-4 if you haven’t– Start on chapter 5, finish what you can’t before the next week– Work on your Project 1

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

6

QUIZ ANSWER AND DISCUSSIONSection 2

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

7

Quiz Week 5 Review

• I will be going over the answers to the Lab Quiz from week 5.

• I will answer any questions about the quiz you have.

• Each quiz is worth 2% of final grade (the quizzes may have a curve applied if appropriate)

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

8

MIDTERM AND FINAL EXAM DISCUSSION

Section 3

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

9

Midterm exam (15%)

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

• It will cover chapters 1-6, 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.

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

10

Final Exam (25%)

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

• It may span two days (I may give half on one day and half the next day).

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

• More on this in November.Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

11

CHAPTER 5Section 3

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

12

What is an “array”?

• “An array is an object that holds multiple variables. These can be accessed using an index.”

• What is an array???– It is just a numbered list.

• What is an index then???– It is the number that refers to an item on the list.

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

13

array Example

int [] grades;

grades = { 80, 50, 100 };

// the index starts at 0, so…// grades[0] == 80// grades[1] == 50

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

14

array Example 2

private String[] TA = { “Bich”, “Troy” };

// If it was then asked, who is TA[1]?// You then should answer…// “Troy”

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

15

array Example 3

Cars[] obstacles;

obstacles[0] = new Car();obstacles[1] = new Car();

// But what is the computer really doing?

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

16

Behind the “array”

• In general, the arrays you will be using will be arrays of objects.

Monday, Oct. 3rd, 2011

0

1

2

3

4

String[] friends = { “Scott”, “Bich”, “Troy”, “Carmen”}

Computer memoryfriends array

“Scott”

“Troy”

“Bich”“Carmen”

[friends array]

???

University at Buffalo: CSE 113 Instructor: Scott Settembre

17

Things to know about arrays

• Arrays start with index 0.

• Each item in the array can also be called an element.

• Each item/element points to an object.

• Getting rid of (or clearing) an array does not necessarily get rid of the elements. The objects in the array may still exist.

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

18

array Example 4

int [] grades = { 100, 80, 100, 50 };int x;

x = grades[1] + grades[2] + grades[3];

// What is the value of “x”// If you answered, x == 280, then you forgot that// grades[0] == 100!!!// An index of an array starts at 0!Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

19

array Example 5

int [] ages = {18, 20, 22, 20, 20}int sum = 0;int average;int index = 0;

while (index <= 4) {sum = sum + ages[index];index = index + 1;

}average = sum / 5;Monday, Oct. 3rd, 2011

Note: The size of the array is 5 elements, but the last index is 4.

University at Buffalo: CSE 113 Instructor: Scott Settembre

20

array Example 6

// What is the final result of “sum”?int sum = 0;int [] paychecks = { 180, 190, 180, 230, 220 };int index = 0;

while (index < 4) {index = index + 2;sum = sum + paychecks[index];

}

// If you got sum == 400, you were right.

Monday, Oct. 3rd, 2011

(index 2 + index 4)

University at Buffalo: CSE 113 Instructor: Scott Settembre

21

array Example 6 – with error// What is the final result of “sum”?int sum = 0;int [] paychecks = { 180, 190, 180, 230, 220 };int index = 0;

while (index <= 4) {index = index + 2;sum = sum + paychecks[index];

}

// This is an example of an out-of-bounds// error, trying to access an index of an array.Monday, Oct. 3rd, 2011

The error here is subtle, but this is exactly what trips students up for hours debugging. Note how index will be 4 IN the second loop, but 6 IN the third loop! This will be an error.

University at Buffalo: CSE 113 Instructor: Scott Settembre

22

Quick note on the String class

• A “String” is just another type of object.

• It is made up of all the characters between double quotes “”.

• Only tricky thing is that the backslash “\” cannot be used alone. It denotes an “escape” character and is used in various ways.

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

23

Typical StringsString name = “Scott”;

String a = “is A”;String z = “zing!”;String mommy = “ma”;

// You can “concatinate” strings together …// in other words add the strings together.

String myInstructor;

// Homework: What is the value of myInstructor???myInstructor = name + a + mommy + z;Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

24

Lab this week

• Please make sure you complete chapters 1-5 this week.

• Work on your projects.

• You will be shown how to submit your project.– Includes “zipping” up your project directory– Submitting it through the Assignments page on UBLearns– There is a document on UBLearns – Course Documents

section that shows you how to submitMonday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

25

CHAPTER 1-5 SUPPLIMENTSection 4

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

26

There are methods in crab-world that I cannot use in my project? Why?

• Let’s take a look at the crab world class hierarchy:

Monday, Oct. 3rd, 2011

The code in here, comes with Greenfoot. You cannot see or edit this code, BUT you can subclass the class and use the methods from that class.

University at Buffalo: CSE 113 Instructor: Scott Settembre

27

Let’s take a look at the Actor class

Monday, Oct. 3rd, 2011

OMG, do I have to know what to do with all this now???

No, instead let’s reuse another person’s work!!!

University at Buffalo: CSE 113 Instructor: Scott Settembre

28

So if I don’t use Actor, what can I use?

• Let’s look again at the crab-world hierarchy:

Monday, Oct. 3rd, 2011

Interesting, maybe the Animal subclass has methods I could use and then don’t have to write myself!!!

University at Buffalo: CSE 113 Instructor: Scott Settembre

29

I don’t see the Animal classin the java docs?!

• The Animal class is not part of Greenfoot, it is just a subclass of Actor (which is a part of Greenfoot).

• It uses (inherits) methods from Actor, that may be more complicated to understand, and simplifies them for use.

• If you want to take advantage of this already written and debugged code, you have two choices:– You can cut/paste code from “Animal” into your classes.– Or you can sub-class the Animal class into a new class,

thereby inheriting all the hard work someone else has done!Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

30

Let’s take a look at the Animal class

Monday, Oct. 3rd, 2011

Oooh, now this is a useful method. I can detect if my “Frogger” hero is overlapping an obstacle. And I don’t need to write any code to use it, I can just inherit it!! Woot!

University at Buffalo: CSE 113 Instructor: Scott Settembre

31

ADDITIONAL MATERIALSection 5

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

32

What picture file formats are we supposed to use?

• You can use any of the supported file formats: JPEG, PNG, GIF, BMP, or TIFF

• These should be placed in your project directory in a subdirectory called “images”

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

33

What file type would you use?

• If you are using or creating your own files, you will want to use the PNG file format.– It allows transparency, so that you don’t have a

case of the “blockies”:

– but instead have this:

Monday, Oct. 3rd, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

34

How does PNG work then?

• GIF and PNG files use a “transparency color”.– One color value is designated as a transparent pixel.– That color value is usually a color that goes unused,

like magenta.

• The problem is that you may still get “jaggies” around your image, since the edge pixels do not blend with the background perfectly, but at least you won’t get a big white box around your image.

Monday, Oct. 3rd, 2011