410 likes | 478 Views
CSE 113 Introduction to Computer Programming. Lecture slides for Week 4. Monday, September 19 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 4 Monday, September 19th, 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/19/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-2 and at least started chapter 3, then you are behind the rest of the class. • Please do the following: • Attempt setting up and doing chapter 1-3 in Bell 101. • If you have too much trouble getting started, then: • Go to a TA office hour, OR • Email a TA, OR • Go to an additional lab section before you go to your lab. University at Buffalo: CSE 113 Instructor: Scott Settembre
Lecture and Lab this Week • Lectures will go over the following: • Project 1 description • Chapter 4 concepts and more formal definitions • constructors • variables • assignment statements using variables • comparing variables • if/else statement • Lab will have you do the following: • Finish chapters 1-3 • Start on chapter 4 • Working on your Project 1 University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 2 Project 1 description University at Buffalo: CSE 113 Instructor: Scott Settembre
Project 1 • Create a “Frogger” style game. • Submit it by October 7th, 2011 by 11:59:59pm. • See .PDF for explanations of: • Late submission • Grading • Grade protesting • How to submit • The lab TA will explain how to submit in a demonstration during lab the week the project is due (I will also show how in Lecture). University at Buffalo: CSE 113 Instructor: Scott Settembre
Typical “Frogger”-style game Some obstacles move right. Obstacles Some move left. Avatar University at Buffalo: CSE 113 Instructor: Scott Settembre
Tips to set up a scenario • Create at least two subclasses of “Actor” • The first class will represent your Avatar, of which you should make only 1 object of that class and place it in the world. • The second class will describe your obstacle, of which you should make at least 5 objects of that class and place it in the world. • Use the “Save the world” menu item to have Greenfoot write the placement code for you. University at Buffalo: CSE 113 Instructor: Scott Settembre
Include randomized directions • Make sure you edit your constructor of your obstacle class to randomly assign an initial direction. • Random (Chapt 3 page 27) • Constructor (Chapt 4, page 43 and 50) University at Buffalo: CSE 113 Instructor: Scott Settembre
Avatar should move using keyboard This avatar is moving up towards the top of the screen. This avatar is moving to the left of the screen. Yes, it is upside-down, but we do not mind this for this project. University at Buffalo: CSE 113 Instructor: Scott Settembre
Tips to use the keyboard (page 36) • You can look at the code for chapter 2 and 3 Crab Scenario (I recommend looking at the final version of Crab) • See where the Crab class uses the keyboard and read and reuse that code for your own purposes. • This has been done in lecture twice already, so hopefully you can find this in the code when needed. University at Buffalo: CSE 113 Instructor: Scott Settembre
Actor Collisions Here, there are two actors colliding. It is ok if obstacles collide, but when the main avatar collides with an obstacle, this should reset the avatar back where he started and play a sound. Colliding (Chapter 3.4 page 35) Playing sounds (Chapter 3.8 page 40) University at Buffalo: CSE 113 Instructor: Scott Settembre
Gooooooooooal! When the avatar reaches the goal line, then you should play a sound and place the avatar back at the beginning. You can compare getY() with some number for the goal. You can use atWorldEdge() for the goal. You can use the collision code with some goal object. University at Buffalo: CSE 113 Instructor: Scott Settembre
Getting the Avatar to the goal line • You may decide that the top of the screen is the goal or perhaps create another object (like a building or pond, etc. for the avatar to get to) • Use your imagination and do something different than the rest of the class. • When goal is reached, play a sound and set the location of the avatar to the place where it began. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 3 Chapter 4 University at Buffalo: CSE 113 Instructor: Scott Settembre
Chapter 4 • We will start on Wednesday. • I recommend being in class if you do not know the following: • What is a variable? • How does the computer store information? • How do I use variables? • How do I do animation? (More than just moving an image around the grid.) University at Buffalo: CSE 113 Instructor: Scott Settembre
Examining Class Code • We can examine the details of the class code again, by double clicking on the class diagram. • The code has several parts which you will need to be familiar with, even though Greenfoot writes it for you. University at Buffalo: CSE 113 Instructor: Scott Settembre
“import” statement • The “import” statement tells the compiler the “package” that we want to use. • A “package” is just a collection of files that contain Java code. • A “package” has class definitions in them, therefore, “importing a package” is the same as telling the computer that we will be using those classes in our program. University at Buffalo: CSE 113 Instructor: Scott Settembre
“import” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorldextends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ publicCrabWorld() { super(560, 560, 1); } } University at Buffalo: CSE 113 Instructor: Scott Settembre
Comments • Comments in Java come in two flavors • Comments that span multiple lines • These comments start with “/*” and end with “*/” • “/*” and “*/” can be placed anywhere (even within another comment) • Anything between “/*” and “*/” is totally ignored by the compiler. • Comment that span to the end of the current line • These comments start with “//” and end at the end of the line • Anything after “//” to the end of the line is ignored. University at Buffalo: CSE 113 Instructor: Scott Settembre
comments in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorldextends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ publicCrabWorld() { super(560, 560, 1); } } University at Buffalo: CSE 113 Instructor: Scott Settembre
What is a “constructor”? • “A constructor of a class is a special kind of method that is executed automatically whenever a new instance is created.” • This means that: • When an object is created, this particular method is used and at no other time. • It can only be used once. • It is usually used to “set up” or “initialize” variables, position, load images, or basically prepare the object to exist. • It also is not required, and may or may not be there! University at Buffalo: CSE 113 Instructor: Scott Settembre
A “constructor” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorldextends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ publicCrabWorld() { super(560, 560, 1); } } University at Buffalo: CSE 113 Instructor: Scott Settembre
What is a “keyword”? • A keyword is a word that is reserved only for compiler use. • It is often called a “reserved word”. • This means that you cannot name methods, classes, or variables with that name. • Keywords are special words that the compiler uses to run Java code. • We already have used some like “class”, “public”, “int”, “if”, etc. University at Buffalo: CSE 113 Instructor: Scott Settembre
What does the “new” keyword do? • “Java objects can be created programmatically (from within your code) by using the new keyword.” • Greenfoot has been doing this for you when you click and place a new object of a class on the screen. • It puts this information in the constructor of your “World” subclass. University at Buffalo: CSE 113 Instructor: Scott Settembre
Use of “new” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorldextends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ publicCrabWorld() { super(560, 560, 1); addObject( new Crab(), 150, 100 ); } } University at Buffalo: CSE 113 Instructor: Scott Settembre
What is an “instance variable”? • An instance variable is can be used to store information (objects of values) for later use. • Sometimes referred to just as a “variable”, “field”, or “property”. • It is a location in the computer’s memory where some data will be stored. • This data can be a value of some type, like a number. • It can be the location of the start of some data, like a list of characters (what we will call a “string”) • It may even be the entire location of another object (which itself contains many instance variables) University at Buffalo: CSE 113 Instructor: Scott Settembre
An “instance variable” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class Crab extends Animal { privateGreenfootImage image1; privateGreenfootImage image2; // constructor method would be here // act method could be here // additional methods would follow } University at Buffalo: CSE 113 Instructor: Scott Settembre
What this does in memory The code will create two locations in memory, one called “image1”, the other called “image2”. They will not contain any information at this time. University at Buffalo: CSE 113 Instructor: Scott Settembre
What is an “assignment statement”? • An assignment statement assigns an object to a variable. (Or a value to a variable). • This is often called an “assignment” or “assignment expression”. • It uses the assignment symbol “=“ to tell the compiler to fill the spot in memory (which is on the left-hand side) with the object or value (which is on the right-hand side). • MyAge = 33; • MyImage = new imageObject(); University at Buffalo: CSE 113 Instructor: Scott Settembre
Using “new” and a variable together • So if we write: image1 = newGreenfootImage(“crab.png”); image2 = newGreenfootImage(“crab2.png”); • This will create an object of type “GreenfootImage” and place its reference (or location) into the variable image1. • Note: It does not put the image itself in the variable “image1” only a reference (or value denoting the location in memory) of the object “GreefootImage” which DOES hold the image itself. University at Buffalo: CSE 113 Instructor: Scott Settembre
What this would do in memory University at Buffalo: CSE 113 Instructor: Scott Settembre
Comparing values • We have seen that there are Boolean comparison symbols. • One which you will be using in chapter 4 is the “==“ symbol (read “ is equal to” when you see “==“) • For example, you will be comparing the existing image that an actor has to one of the stored images that the actor will use. In the code: If ( getImage() == image1 ) compares the current image of the actor with the stored image that the instance variable “image1” points to. University at Buffalo: CSE 113 Instructor: Scott Settembre
TIP: What is the difference between “=“ and “==“? • If you accidentally use “=“ in place of “==“, you may not get an syntax error! • This can make you spend hours of time to debug your code!!! • Make sure when you are assigning a value to a variable, you use “=“ and when you are comparing a value to a variable you use “==“ University at Buffalo: CSE 113 Instructor: Scott Settembre
Control statement : “if/else” statement • You are a little familiar with the “if/else” statement, but we have really been only using the “if” portion. • This is often called a “rule”. University at Buffalo: CSE 113 Instructor: Scott Settembre
“if/else” clauses • In the “if/else” statement, there are actually two sections (but one is optional) • The “if-clause” is the first code block after the condition is checked to be true. • Both the comparison and the “if-clause” must exist. • The “else-clause” follows the “if-clause” and is the code block that is used if the condition is false” • Both the “else” keyword and the “else-clause” is optional. • If the “else” and “else-clause” is not there, then nothing is done. University at Buffalo: CSE 113 Instructor: Scott Settembre
“if without the else” in code If ( check the Boolean value of some condition ) { // If the condition is true, // then run the code in this block } // If the condition is false, // then just run the code after the block University at Buffalo: CSE 113 Instructor: Scott Settembre
“if/else” in code If ( check the Boolean value of some condition ) { // If the condition is true, // then run the code in this block } else { // If the condition is false, // then run the code in this block } University at Buffalo: CSE 113 Instructor: Scott Settembre
Lab for Week 4 • Please finish chapter 3 and 4. • If you do not finish chapter 4, then do so at home or during another lab. • Be ready to start chapter 5 next week. University at Buffalo: CSE 113 Instructor: Scott Settembre
Midterm exam • 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