1 / 41

CSE 113 Introduction to Computer Programming

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 .

aoife
Télécharger la présentation

CSE 113 Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 113Introduction toComputer Programming Lecture slides for Week 4 Monday, September 19th, 2011 Instructor: Scott Settembre

  2. Section 1 Course Administration University at Buffalo: CSE 113 Instructor: Scott Settembre

  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/19/2011: (Also come to additional labs if you need help) University at Buffalo: CSE 113 Instructor: Scott Settembre

  4. 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

  5. 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

  6. Section 2 Project 1 description University at Buffalo: CSE 113 Instructor: Scott Settembre

  7. 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

  8. Typical “Frogger”-style game Some obstacles move right. Obstacles Some move left. Avatar University at Buffalo: CSE 113 Instructor: Scott Settembre

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Section 3 Chapter 4 University at Buffalo: CSE 113 Instructor: Scott Settembre

  17. 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

  18. 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

  19. “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

  20. “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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. What this would do in memory University at Buffalo: CSE 113 Instructor: Scott Settembre

  34. 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

  35. 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

  36. 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

  37. “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

  38. “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

  39. “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

  40. 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

  41. 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

More Related