Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre."— Presentation transcript:

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

2 COURSE ADMINISTRATION Section 1 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 2

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 3 NameEmail addressOffice hours Bich (pronounced “Bic”) Vubtvu@buffalo.eduM (11-11:50am) Bell 329 F (12-12:50pm) Bell 329 Troy Kosstroykoss@buffalo.eduTues (3-5pm) Bell 329 Scott Settembress424@buffalo.eduM (10-11am) Bell 229 F (10-11am) Bell 340 Also at all times by email AND video conference by appointment.

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 4

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 5

6 QUIZ ANSWER AND DISCUSSION Section 2 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 6

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 7

8 MIDTERM AND FINAL EXAM DISCUSSION Section 3 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 8

9 Midterm exam (15%) The midterm exam will be held in lecture on October 21 st. 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 University at Buffalo: CSE 113 Instructor: Scott Settembre 9

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 10

11 CHAPTER 5 Section 3 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 11

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 12

13 Math Expressions These are sometimes called “arithmetic expressions” and they use the arithmetic operators: Thursday, June 16th, 2011 University at Buffalo: EAS 230 Instructor: Scott Settembre 13 Operator ActionExample +additiona = 1 + 1; // a == 2 -subtractionb = 2 - 1; // b == 1 *multiplicationc = 4 * 4; // c == 16 /divisiond = 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.

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 14

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 )// true if c != 10 – ( d 10 )// always false – ( !(a < b) )// false, if a is less than b Tuesday, June 11th, 2011 University at Buffalo: EAS 230 Instructor: Scott Settembre 15

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 16

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 17

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 18

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 19

20 Abstract example Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 20 “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()

21 Abstract example : continued Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 21 “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”)

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 22

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 23

24 OceanWorld Fish class Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 24 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); } }

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 25

26 OceanWorld Fish class using Abstraction Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 26 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” } }

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 27

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 28

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 #1 Do statement #2 Do statement #3 … Do statement #n } Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 29 Then loop back and check the condition again to see if we need to run the code block again.

30 “while” loop example #1 int x = 0; while ( x < 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 5 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 30

31 “while” loop example #2 int x = 0; while ( x == 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 0 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 31

32 “while” loop example #3 int x = 0; while ( x < 5 ) { x = x + 2; } // what is the value of x after the loop??? // x == 6 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 32

33 “while” loop example #4 int x = 0; while ( x <= 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 6 Monday, Sept. 26th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 33

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 34

35 Example of a local variable public void Swim() { if ( !sleeping ) { int counter = 0; // “counter” is the local variable while (counter < speed) { move();// move the fish one grid space counter = 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 University at Buffalo: CSE 113 Instructor: Scott Settembre 35

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 36 Instance Variables Local Variables It is a parameter, but it is also a local variable

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 University at Buffalo: CSE 113 Instructor: Scott Settembre 37


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

Similar presentations


Ads by Google