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

37
CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th , 2011 Instructor: Scott Settembre

Upload: reynard-wade

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

CSE 113Introduction to

Computer Programming

Lecture slides for Week 5

Monday, September 26th, 2011Instructor: Scott Settembre

Page 2: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

2

COURSE ADMINISTRATIONSection 1

Monday, Sept. 26th, 2011

Page 3: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 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 9/26/2011: (Also come to additional labs if you need help)

Monday, Sept. 26th, 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.

Page 4: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 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-3 and plan to finish chapter 4 before your lab, then you are behind the rest of the class.

• Please do the following:– Complete chapter 1-4 in Bell 101.– If you cannot finish up chapter 4 by your lab, then:

• Finish chapters 1-3, AND THEN• Stay up late at night and finish chapter 4 before your lab.• You can DO it!

Monday, Sept. 26th, 2011

Page 5: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 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:

– Lab Quizzes for Week 3 and 4– Chapter 5 concepts and more formal definitions

• mathematical and boolean expressions• code abstraction• loops• local variables• arrays (or lists of objects)• strings (and string manipulation)• icons, jpg, gif, png file differences (not in book yet)

• 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 lab– Working on your Project 1

Monday, Sept. 26th, 2011

Page 6: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

6

QUIZ ANSWER AND DISCUSSIONSection 2

Monday, Sept. 26th, 2011

Page 7: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

7

Quiz Week 3 and 4 Review

• I will be going over the answers to the Lab Quizzes from week 3 and 4.

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

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

Monday, Sept. 26th, 2011

Page 8: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

8

MIDTERM AND FINAL EXAM DISCUSSION

Section 3

Monday, Sept. 26th, 2011

Page 9: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

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

Page 10: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

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

Page 11: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

11

CHAPTER 5Section 3

Monday, Sept. 26th, 2011

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

University at Buffalo: CSE 113 Instructor: Scott Settembre

12

Mathematical and Boolean expressions

• You will find the need to add, subtract, multiply, divide, do modulus (find the remainder after a division) in arithmetic expressions.

• You will find the need to use the logical operators: AND, OR, and NOT, in boolean expressions.

• Parenthesis are used just like in real math, to ensure precedence.

Monday, Sept. 26th, 2011

Page 13: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: EAS 230 Instructor: Scott Settembre

13

Math Expressions

• These are sometimes called “arithmetic expressions” and they use the arithmetic operators:

Thursday, June 16th, 2011

Operator Action Example

+ addition a = 1 + 1; // a == 2

- subtraction b = 2 - 1; // b == 1

* multiplication c = 4 * 4; // c == 16

/ division d = 5 / 2; // if d is type int, then d == 2

% modulo† e = 5 / 2; // e == 1

† Modulo, sometimes referred to as “mod”, returns the remainder from an integer division.

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

University at Buffalo: CSE 113 Instructor: Scott Settembre

14

Code example : using math

private int Sum = 0;private int Average = 0;

Sum = 50 + 80 + 90 + 100 + 100 + 100;Average = Sum / 6;

// or we could have done

Average = (50 + 80 + 90 + 100 + 100 + 100) / 6;Monday, Sept. 26th, 2011

Page 15: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: EAS 230 Instructor: Scott Settembre

15

Parenthesis, “and”, “or”, “not”• Boolean expressions can also include parenthesis for

precedence (just like math) and the “AND”, “OR”, or “NOT” logical operators:– “AND” is “&&”– “OR” is “||”– “NOT” is “!”

• For example:– ( a == 10 && b == 10 ) // true if a and b == 10– ( c < 10 || c > 10 ) // true if c != 10– ( d < 10 && d > 10 ) // always false– ( !(a < b) ) // false, if a is less than b

Tuesday, June 11th, 2011

Page 16: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

16

Code example : using “&&” (and)private int a = 5;private int b = 10;private int c = 20;

if ( (a < b) && (b < c) ){

// then a is less than c}else{

// then a is not less than c}

Monday, Sept. 26th, 2011

Page 17: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

17

Abstraction

• Talked about abstraction in class– Examine what it means to abstract a class– Show how to use the constructor to make an

abstract class to create a specific object

Monday, Sept. 26th, 2011

Page 18: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

18

More on Wednesday

• Keep working on your projects.

• Finish up to and including chapter 4 by your lab section.

• I will talk more about chapter 5.

Monday, Sept. 26th, 2011

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

University at Buffalo: CSE 113 Instructor: Scott Settembre

19

What is “abstraction” for programs?

• Abstraction is a technique used to make code more general.– It makes code general, so that you do not need to

change a class to create something specific.– You will use an abstract class to instantiate a specific

object.

• We have done this before, by shift+left-clicking a class onto the world, but we did not specify how each object was specific.

Monday, Sept. 26th, 2011

Page 20: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

20

Abstract example

Monday, Sept. 26th, 2011

“Dog” class

These are all instances of “Dog”, but they are all the same!• They all use the same code.• They have the same values in their instance variables.• They all were created using the code “new Dog()”

new Dog()new Dog()new Dog()

Page 21: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

21

Abstract example : continued

Monday, Sept. 26th, 2011

“Dog” class

• What if we made a Dog constructor that took a parameter list?– This way, we can specify some information when we create a new

object.– Then if we created a new dog, we can specify its name.

new Dog(“Rex”)new Dog(“Woofy”)new Dog(“Fido”)

Page 22: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

22

What does this look like in code?

public class Dog extends Actor{

private String name;

public Dog(String newName){name = newName;}

}Monday, Sept. 26th, 2011

Page 23: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

23

Another abstraction example

• Let say we want to make an OceanWorld that has many different types of fish.

– However, we only want to make one java class that covers all fish.

– We do this because we don’t want to make hundreds of different classes to cover all the different types of fish there are.

– All fish swim, and we wouldn’t want to have to write hundreds of different “swim” methods!

Monday, Sept. 26th, 2011

Page 24: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

24

OceanWorld Fish class

Monday, Sept. 26th, 2011

public class Fish extends Actor{

public String color = “blue”;public int length = 10;public int speed = 2;

public Fish(){

// Any initializing code – like loading in a picture file}

public void Swim(){

move(speed);}

}

Page 25: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

25

So to create fish…

• In the OceanWorld constructor, we would see a lot of:

addObject ( new Fish(), 100, 200 );addObject ( new Fish(), 520, 150 );addObject ( new Fish(), 302, 63 );addObject ( new Fish(), 10, 250 );

• And they would all have length == 10, speed == 2, and color == “blue”. How boring…

Monday, Sept. 26th, 2011

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

University at Buffalo: CSE 113 Instructor: Scott Settembre

26

OceanWorld Fish class using Abstraction

Monday, Sept. 26th, 2011

public class Fish extends Actor{

public String color;public int length;public int speed;

public Fish(String theColor, int theLength, int theSpeed){

color = theColor;length = theLength;speed = theSpeed;// Any other initializing code – like loading in a picture file

}

public void Swim(){

move(speed); // note: This is “speed” from the instance variable, not “theSpeed”}

}

Page 27: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

27

So to create fish with abstraction…

• In the OceanWorld constructor, we would see a lot of:

addObject ( new Fish(“red”,6,2), 100, 200 );addObject ( new Fish(“blue”,8,5), 520, 150 );addObject ( new Fish(“white”,1,1), 302, 63 );addObject ( new Fish(“blue”,8,7), 10, 250 );

• And they would all have different lengths, colors, or speeds, proving that there really are many fish in the sea!

Monday, Sept. 26th, 2011

Page 28: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

28

Control code : Loops• Besides the if..else statement, loops are the other control code

you will need to learn.

• It allows you to communicate to the computer that it should run a block of code over and over again, until some condition is met.

• Beware! You can make a mistake and have a loop run FOREVER!– This is called an infinite loop and will lock up your machine.– Greenfoot handles this (so your machine will not really lock up), but

other languages (and running your program directly on Java on another machine) may lock it up.

Monday, Sept. 26th, 2011

Page 29: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

29

What is the “while” loop.

• The “while” loop is one form of loop you will get good at.• Here is what it looks like:

while ( some-condition-is-true ){

Do statement #1Do statement #2Do statement #3…Do statement #n

}Monday, Sept. 26th, 2011

Then loop back and check the condition again to see if we need to run the code block again.

Page 30: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

30

“while” loop example #1

int x = 0;while ( x < 5 ){

x = x + 1;}

// what is the value of x after the loop???// x == 5Monday, Sept. 26th, 2011

Page 31: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

31

“while” loop example #2

int x = 0;while ( x == 5 ){

x = x + 1;}

// what is the value of x after the loop???// x == 0Monday, Sept. 26th, 2011

Page 32: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

32

“while” loop example #3

int x = 0;while ( x < 5 ){

x = x + 2;}

// what is the value of x after the loop???// x == 6Monday, Sept. 26th, 2011

Page 33: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

33

“while” loop example #4

int x = 0;while ( x <= 5 ){

x = x + 1;}

// what is the value of x after the loop???// x == 6Monday, Sept. 26th, 2011

Page 34: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

34

What is a “local variable”?

• A local variable is a variable that stores data, however, this data (and the variable) goes away after the code block it is in is not being used.

• It also does not need to use the keywords “public” or “private”.

• You can easily set its value with the assignment operator “=“ when you declare it.

Monday, Sept. 26th, 2011

Page 35: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

35

Example of a local variablepublic void Swim(){

if ( !sleeping ){

int counter = 0; // “counter” is the local variablewhile (counter < speed){

move(); // move the fish one grid spacecounter = counter + 1; // increment the counter

} // What is the value of “counter” here?

}// What is the value of “counter” here? *trick question*

}

Monday, Sept. 26th, 2011

Page 36: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

36

Which are local variables and which are instance variables?

public class A extends Z {public int myVar1;private int myVar2;

public A(){

int myVar3 = 100;}

public void B( int myVar4){

int myVar5;}

}

Monday, Sept. 26th, 2011

Instance Variables

Local Variables

It is a parameter, but it is also a local variable

Page 37: CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre

University at Buffalo: CSE 113 Instructor: Scott Settembre

37

More on Monday

• Keep working on your projects.

• Finish up chapters 1-4, and most of chapter 5.

• Arrays next!

Monday, Sept. 26th, 2011