370 likes | 527 Views
CSE 113 Introduction to Computer Programming. Lecture slides for Week 5. Monday, September 26 th , 2011 Instructor: Scott Settembre. Section 1. Course Administration. For Project Assistance. You can find the up-to-date hours and locations in the “Contact” section of UBLearns .
E N D
CSE 113Introduction toComputer Programming Lecture slides for Week 5 Monday, September 26th, 2011 Instructor: Scott Settembre
Section 1 Course Administration University at Buffalo: CSE 113 Instructor: Scott Settembre
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) University at Buffalo: CSE 113 Instructor: Scott Settembre
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! University at Buffalo: CSE 113 Instructor: Scott Settembre
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 University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 2 Quiz answer and discussion University at Buffalo: CSE 113 Instructor: Scott Settembre
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) University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 3 Midterm and Final exam Discussion University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 3 Chapter 5 University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
Math Expressions • These are sometimes called “arithmetic expressions” and they use the arithmetic operators: † Modulo, sometimes referred to as “mod”, returns the remainder from an integer division. University at Buffalo: EAS 230 Instructor: Scott Settembre
Code example : using math privateint Sum = 0; privateint 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; University at Buffalo: CSE 113 Instructor: Scott Settembre
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 University at Buffalo: EAS 230 Instructor: Scott Settembre
Code example : using “&&” (and) privateint a = 5; privateint b = 10; privateint c = 20; if ( (a < b) && (b < c) ) { // then a is less than c } else { // then a is not less than c } University at Buffalo: CSE 113 Instructor: Scott Settembre
Abstraction • Talked about abstraction in class • Examine what it means to abstract a class • Show how to use the constructorto make an abstract class to create a specific object University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
Abstract example “Dog” class new Dog() new Dog() new Dog() • 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()” University at Buffalo: CSE 113 Instructor: Scott Settembre
Abstract example : continued • 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. “Dog” class new Dog(“Fido”) new Dog(“Woofy”) new Dog(“Rex”) University at Buffalo: CSE 113 Instructor: Scott Settembre
What does this look like in code? publicclass Dog extends Actor { private String name; public Dog(String newName) { name = newName; } } University at Buffalo: CSE 113 Instructor: Scott Settembre
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! University at Buffalo: CSE 113 Instructor: Scott Settembre
OceanWorld Fish class publicclass 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); } } University at Buffalo: CSE 113 Instructor: Scott Settembre
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… University at Buffalo: CSE 113 Instructor: Scott Settembre
OceanWorld Fish class using Abstraction publicclass Fish extends Actor { public String color; public intlength; public int speed; public Fish(String theColor, inttheLength, inttheSpeed) { 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” } } University at Buffalo: CSE 113 Instructor: Scott Settembre
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! University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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 } Then loop back and check the condition again to see if we need to run the code block again. University at Buffalo: CSE 113 Instructor: Scott Settembre
“while” loop example #1 int x = 0; while ( x < 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 5 University at Buffalo: CSE 113 Instructor: Scott Settembre
“while” loop example #2 int x = 0; while ( x == 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 0 University at Buffalo: CSE 113 Instructor: Scott Settembre
“while” loop example #3 int x = 0; while ( x < 5 ) { x = x + 2; } // what is the value of x after the loop??? // x == 6 University at Buffalo: CSE 113 Instructor: Scott Settembre
“while” loop example #4 int x = 0; while ( x <= 5 ) { x = x + 1; } // what is the value of x after the loop??? // x == 6 University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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* } University at Buffalo: CSE 113 Instructor: Scott Settembre
Which are local variables and which are instance variables? publicclass A extends Z { publicint myVar1; privateint myVar2; public A() { int myVar3 = 100; } publicvoid B( int myVar4) { int myVar5; } } Instance Variables Local Variables It is a parameter, but it is also a local variable University at Buffalo: CSE 113 Instructor: Scott Settembre
More on Monday • Keep working on your projects. • Finish up chapters 1-4, and most of chapter 5. • Arrays next! University at Buffalo: CSE 113 Instructor: Scott Settembre