210 likes | 304 Views
CSE 113 Introduction to Computer Programming. Lecture slides for Week 9. Monday, October 24 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 9 Monday, October 24th, 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 10/24/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-5, then you are behind the rest of the class. • begun chapter 6 • Please do the following: • Finish chapter 6 in lab this week. • Begin working on project 2. University at Buffalo: CSE 113 Instructor: Scott Settembre
Lecture and Lab this Week • Lectures will go over the following: • Chapter 6 concepts • The List type or Interface • Use of the keyword “null” • for-each loop • Lab will have you do the following: • Finish chapter 6. • Start your project 1. • Lab Quiz on chapters 6. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 2 Midterm / Final exam / Project 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.4, 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. • Grades will be released later this week. 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 will span two days, 45 minutes (or less) each 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
Project 1 • Project 1 grades have been posted. • Good job! University at Buffalo: CSE 113 Instructor: Scott Settembre
Project 2 • Due date: November 11th, 2011 at or before 11:59:59 PM • We will design the abstract class we will be using in class, together. • The project description will be discussed on Monday and Wednesday. • The abstract class and Greenfoot project you will use will be distributed in lab. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 3 Chapter 6 University at Buffalo: CSE 113 Instructor: Scott Settembre
What is a “generic type”? • A generic type is like a template. • You need to provide it with a second type so that it knows what it is going to be using. • The template is the code with “spaces” where the second type you give it is placed. University at Buffalo: CSE 113 Instructor: Scott Settembre
Real life example • If I have a paragraph that reads: “Open the ______. Take a book and put it inside the ____. Close the _____.” • Then I ask you to give me a noun, like “box”, and I fill in the blanks with that word. This word is akin to the second type of a generic type. • The primary type is the template. The secondary type is the noun “box”. University at Buffalo: CSE 113 Instructor: Scott Settembre
Example of a generic type • A “List” is a generic type. • It is technically a “interface”. (At this point in the course, you should know that a class is a type, but not a “base” type. An interface is like an incomplete class.) • It takes a second type as an argument. • When you create a variable of type “List”, you must provide the second type so that the compiler knows how to “fill in the template”. • A generic type cannot be used without a second type. *Test question* (i.e. An object cannot be created directly from a generic type, you must provide the second type.) University at Buffalo: CSE 113 Instructor: Scott Settembre
The “List” type • Look it up! (Yes, do this in lab) – “java.util.List” • If you want to use it to create a list of integers, you can do this: List<int> grades = newArrayList<int>(); grades.add(50); //add new grade • You cannot do: List grades; This will not work University at Buffalo: CSE 113 Instructor: Scott Settembre
How can I use the “List” type? • You will be using it mostly to declare a variable that can be used with Greenfoot method calls. • For example, when you chapter 6, you will need to create a List that can be used to store the return value from the GreenfootgetWorld.getObjects method. // This will get all the objects of type “Body” // and place them in a List called bodies // that stores objects of type Body List<Body> bodies = getWorld().getObjects(Body.class); University at Buffalo: CSE 113 Instructor: Scott Settembre
The primitive “for-each” loop • Once we have a list, we need an easy way to loop through it. • We could do the following: • Create a variable for an index counter • Create a while loop • Access each element of the array that is holding the items of the second type • Increment the counter • Loop and check to see if we are done with all the items University at Buffalo: CSE 113 Instructor: Scott Settembre
Primitive “for-each” loop • In code, that may look like this: int x=0; intsizeOfArray=5; int [] grades = {70,80,100,80,90}; while (x < sizeOfArray) { int grade = grades[x]; // DO SOMETHING WITH “grade” x = x + 1; } University at Buffalo: CSE 113 Instructor: Scott Settembre
Easy for-each loop • Or we can use a shortcut, and just do: for (int grade : grades) { // DO SOMETHING with “grade” } • Does it save you coding time? Not really… • But it helps in several ways including: • It makes the code cleaner, easier to debug perhaps. • It eliminates the need for you to keep an index counter. University at Buffalo: CSE 113 Instructor: Scott Settembre
How can I use the for-each loop? • You will use it in chapter 6 to iterate through the returned list from the method getObjects. • For example: List<Body> bodies = getWorld().getObjects(Body.class); for (Body body : bodies) { if (body != this) Ask me about what “this” does { applyGravity (body); // pass the current item to the method } } University at Buffalo: CSE 113 Instructor: Scott Settembre
For lab this week • Make sure you do section 6.11 BEFORE the quiz. • In particular, make sure that you take a serious look at the code as asked in Exercise 6.28. • But make sure that you do Exercise 6.30 and 6.35. • I would like it that if you do not understand how to do 6.36-6.38, that you make sure that you do it outside of lab. • I have asked the TA’s NOT to help you with 6.38 at this time. • I want you to make a serious attempt at it. • If you have questions email the TA’s and CC me on the weekend and we will address it during lecture and lab. University at Buffalo: CSE 113 Instructor: Scott Settembre