cse 113 introduction to computer programming lecture slides for week 4 monday, september 19 th, 2011...

41
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th , 2011 Instructor: Scott Settembre

Upload: marianna-chambers

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

CSE 113Introduction to

Computer Programming

Lecture slides for Week 4

Monday, September 19th, 2011Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

2

COURSE ADMINISTRATIONSection 1

Monday, Sept. 19th, 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/19/2011: (Also come to additional labs if you need help)

Monday, Sept. 19th, 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 229F (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-2 and at least started chapter 3, then you are behind the rest of the class.

• Please do the following:– Attempt setting up and doing chapter 1-3 in Bell 101.– If you have too much trouble getting started, then:

• Go to a TA office hour, OR• Email a TA, OR• Go to an additional lab section before you go to your lab.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

5

Lecture and Lab this Week

• Lectures will go over the following:– Project 1 description– Chapter 4 concepts and more formal definitions

• constructors• variables• assignment statements using variables• comparing variables• if/else statement

• Lab will have you do the following:– Finish chapters 1-3– Start on chapter 4– Working on your Project 1

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

6

PROJECT 1 DESCRIPTIONSection 2

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

7

Project 1

• Create a “Frogger” style game.• Submit it by October 7th, 2011 by 11:59:59pm.• See .PDF for explanations of:

– Late submission– Grading– Grade protesting– How to submit

• The lab TA will explain how to submit in a demonstration during lab the week the project is due (I will also show how in Lecture).

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

8

Typical “Frogger”-style game

Monday, Sept. 19th, 2011

Avatar

Obstacles

Some obstacles move right.

Some move left.

University at Buffalo: CSE 113 Instructor: Scott Settembre

9

Tips to set up a scenario

• Create at least two subclasses of “Actor”

• The first class will represent your Avatar, of which you should make only 1 object of that class and place it in the world.

• The second class will describe your obstacle, of which you should make at least 5 objects of that class and place it in the world.

• Use the “Save the world” menu item to have Greenfoot write the placement code for you.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

10

Include randomized directions

• Make sure you edit your constructor of your obstacle class to randomly assign an initial direction.– Random (Chapt 3 page 27)– Constructor (Chapt 4, page 43 and 50)

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

11

Avatar should move using keyboard

Monday, Sept. 19th, 2011

This avatar is moving up towards the top of the screen.

This avatar is moving to the left of the screen. Yes, it is upside-down, but we do not mind this for this project.

University at Buffalo: CSE 113 Instructor: Scott Settembre

12

Tips to use the keyboard (page 36)

• You can look at the code for chapter 2 and 3 Crab Scenario (I recommend looking at the final version of Crab)

• See where the Crab class uses the keyboard and read and reuse that code for your own purposes.

• This has been done in lecture twice already, so hopefully you can find this in the code when needed.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

13

Actor Collisions

Monday, Sept. 19th, 2011

Here, there are two actors colliding. It is ok if obstacles collide, but when the main avatar collides with an obstacle, this should reset the avatar back where he started and play a sound.

Colliding (Chapter 3.4 page 35)Playing sounds (Chapter 3.8 page 40)

University at Buffalo: CSE 113 Instructor: Scott Settembre

14

Gooooooooooal!

Monday, Sept. 19th, 2011

When the avatar reaches the goal line, then you should play a sound and place the avatar back at the beginning.

You can compare getY() with some number for the goal.You can use atWorldEdge() for the goal.You can use the collision code with some goal object.

University at Buffalo: CSE 113 Instructor: Scott Settembre

15

Getting the Avatar to the goal line

• You may decide that the top of the screen is the goal or perhaps create another object (like a building or pond, etc. for the avatar to get to)

• Use your imagination and do something different than the rest of the class.

• When goal is reached, play a sound and set the location of the avatar to the place where it began.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

16

CHAPTER 4Section 3

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

17

Chapter 4

• We will start on Wednesday.• I recommend being in class if you do not know

the following:– What is a variable?– How does the computer store information?– How do I use variables?– How do I do animation? (More than just moving

an image around the grid.)

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

18

Examining Class Code

• We can examine the details of the class code again, by double clicking on the class diagram.

• The code has several parts which you will need to be familiar with, even though Greenfoot writes it for you.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

19

“import” statement

• The “import” statement tells the compiler the “package” that we want to use.

• A “package” is just a collection of files that contain Java code.

• A “package” has class definitions in them, therefore, “importing a package” is the same as telling the computer that we will be using those classes in our program.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

20

“import” in the codeimport greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World{ /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); }}Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

21

Comments

• Comments in Java come in two flavors– Comments that span multiple lines• These comments start with “/*” and end with “*/”• “/*” and “*/” can be placed anywhere (even within

another comment)• Anything between “/*” and “*/” is totally ignored by the

compiler.

– Comment that span to the end of the current line• These comments start with “//” and end at the end of the

line• Anything after “//” to the end of the line is ignored.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

22

comments in the codeimport greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World{ /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); }}Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

23

What is a “constructor”?

• “A constructor of a class is a special kind of method that is executed automatically whenever a new instance is created.”

• This means that:– When an object is created, this particular method is used

and at no other time.– It can only be used once.– It is usually used to “set up” or “initialize” variables, position,

load images, or basically prepare the object to exist.– It also is not required, and may or may not be there!

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

24

A “constructor” in the codeimport greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World{ /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); }}Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

25

What is a “keyword”?

• A keyword is a word that is reserved only for compiler use.

• It is often called a “reserved word”.• This means that you cannot name methods,

classes, or variables with that name.• Keywords are special words that the compiler

uses to run Java code.– We already have used some like “class”, “public”,

“int”, “if”, etc.Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

26

What does the “new” keyword do?

• “Java objects can be created programmatically (from within your code) by using the new keyword.”

• Greenfoot has been doing this for you when you click and place a new object of a class on the screen.

• It puts this information in the constructor of your “World” subclass.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

27

Use of “new” in the codeimport greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)

public class CrabWorld extends World{ /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); addObject( new Crab(), 150, 100 ); }}Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

28

What is an “instance variable”?

• An instance variable is can be used to store information (objects of values) for later use.– Sometimes referred to just as a “variable”, “field”, or

“property”.– It is a location in the computer’s memory where some

data will be stored.• This data can be a value of some type, like a number.• It can be the location of the start of some data, like a list of

characters (what we will call a “string”)• It may even be the entire location of another object (which

itself contains many instance variables)

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

29

An “instance variable” in the codeimport greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage)

public class Crab extends Animal{ private GreenfootImage image1; private GreenfootImage image2; // constructor method would be here

// act method could be here

// additional methods would follow}

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

30

What this does in memory

Monday, Sept. 19th, 2011

The code will create two locations in memory, one called “image1”, the other called “image2”.

They will not contain any information at this time.

University at Buffalo: CSE 113 Instructor: Scott Settembre

31

What is an “assignment statement”?

• An assignment statement assigns an object to a variable. (Or a value to a variable).– This is often called an “assignment” or “assignment

expression”.

• It uses the assignment symbol “=“ to tell the compiler to fill the spot in memory (which is on the left-hand side) with the object or value (which is on the right-hand side).– MyAge = 33;– MyImage = new imageObject();

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

32

Using “new” and a variable together

• So if we write:

image1 = new GreenfootImage(“crab.png”);image2 = new GreenfootImage(“crab2.png”);

• This will create an object of type “GreenfootImage” and place its reference (or location) into the variable image1.

• Note: It does not put the image itself in the variable “image1” only a reference (or value denoting the location in memory) of the object “GreefootImage” which DOES hold the image itself.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

33

What this would do in memory

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

34

Comparing values• We have seen that there are Boolean comparison symbols.

• One which you will be using in chapter 4 is the “==“ symbol (read “ is equal to” when you see “==“)

• For example, you will be comparing the existing image that an actor has to one of the stored images that the actor will use. In the code:

If ( getImage() == image1 )

compares the current image of the actor with the stored image that the instance variable “image1” points to.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

35

TIP: What is the difference between “=“ and “==“?

• If you accidentally use “=“ in place of “==“, you may not get an syntax error!

• This can make you spend hours of time to debug your code!!!

• Make sure when you are assigning a value to a variable, you use “=“ and when you are comparing a value to a variable you use “==“

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

36

Control statement : “if/else” statement

• You are a little familiar with the “if/else” statement, but we have really been only using the “if” portion.

• This is often called a “rule”.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

37

“if/else” clauses• In the “if/else” statement, there are actually two

sections (but one is optional)

– The “if-clause” is the first code block after the condition is checked to be true.• Both the comparison and the “if-clause” must exist.

– The “else-clause” follows the “if-clause” and is the code block that is used if the condition is false”• Both the “else” keyword and the “else-clause” is optional.• If the “else” and “else-clause” is not there, then nothing is

done.Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

38

“if without the else” in code

If ( check the Boolean value of some condition ){

// If the condition is true,// then run the code in this block

}

// If the condition is false,// then just run the code after the blockMonday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

39

“if/else” in code

If ( check the Boolean value of some condition ){

// If the condition is true,// then run the code in this block

}else{

// If the condition is false,// then run the code in this block

}Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

40

Lab for Week 4

• Please finish chapter 3 and 4.• If you do not finish chapter 4, then do so at

home or during another lab.• Be ready to start chapter 5 next week.

Monday, Sept. 19th, 2011

University at Buffalo: CSE 113 Instructor: Scott Settembre

41

Midterm exam

• 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, Sept. 19th, 2011