340 likes | 436 Views
CSE 113 Introduction to Computer Programming. Lecture slides for Week 6. Monday, October 3 rd , 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 6 Monday, October 3rd, 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-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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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 University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 2 Quiz answer and discussion University at Buffalo: CSE 113 Instructor: Scott Settembre
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) 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
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
array Example int [] grades; grades = { 80, 50, 100 }; // the index starts at 0, so… // grades[0] == 80 // grades[1] == 50 University at Buffalo: CSE 113 Instructor: Scott Settembre
array Example 2 private String[] TA = { “Bich”, “Troy” }; // If it was then asked, who is TA[1]? // You then should answer… // “Troy” University at Buffalo: CSE 113 Instructor: Scott Settembre
array Example 3 Cars[] obstacles; obstacles[0] = new Car(); obstacles[1] = new Car(); // But what is the computer really doing? University at Buffalo: CSE 113 Instructor: Scott Settembre
Behind the “array” • In general, the arrays you will be using will be arrays of objects. String[] friends = { “Scott”, “Bich”, “Troy”, “Carmen”} Computer memory friends array “Scott” [friends array] “Troy” “Carmen” ??? “Bich” University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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! University at Buffalo: CSE 113 Instructor: Scott Settembre
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; Note: The size of the array is 5 elements, but the last index is 4. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. (index 2 + index 4) University at Buffalo: CSE 113 Instructor: Scott Settembre
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. 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
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
Typical Strings String 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; University at Buffalo: CSE 113 Instructor: Scott Settembre
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 submit University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 4 Chapter 1-5 suppliment University at Buffalo: CSE 113 Instructor: Scott Settembre
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: 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
Let’s take a look at the Actor class 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
So if I don’t use Actor, what can I use? • Let’s look again at the crab-world hierarchy: 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
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! University at Buffalo: CSE 113 Instructor: Scott Settembre
Let’s take a look at the Animal class 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
Section 5 Additional material University at Buffalo: CSE 113 Instructor: Scott Settembre
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” University at Buffalo: CSE 113 Instructor: Scott Settembre
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: University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre