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

Slides:



Advertisements
Similar presentations
CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.
Advertisements

2/18/2008ITK 1681 Feb. 20, Wed. 8:00 – 9:30 pm STV Closed book, notes, and no computer is allowed. 2.Everyone is allowed to bring a self- prepared.
Games and Simulations O-O Programming in Java The Walker School
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Chapter 2 - The First Program: Little Crab
Program: Little Crab Mr Gano.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CSE 113 Introduction to Computer Programming Lecture slides for Week 10 Monday, October 31 th, 2011 Instructor: Scott Settembre.
CSE 113 Introduction to Computer Programming Lecture slides for Week 6 Monday, October 3 rd, 2011 Instructor: Scott Settembre.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to a Programming Environment
Introduction to C Programming
CIS101 Introduction to Computing Week 12 Spring 2004.
Recitation 1 Programming for Engineers in Python.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
The Java Programming Language
CSE 113 Introduction to Computer Programming Lecture slides for Week 2 Wednesday, September 7 th, 2011 Instructor: Scott Settembre.
CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CS123 Engineering Computation Lab Lab 4 Bruce Char Department of Computer Science Drexel University Spring 2010.
CSE 113 Introduction to Computer Programming Lecture slides for Week 1 Monday, August 29 th, 2011 Instructor: Scott Settembre.
CSE 113 Introduction to Computer Programming Lecture slides for Week 7 Monday, October 10 th, 2011 Instructor: Scott Settembre.
CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre.
Introduction to programming in the Java programming language.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Javadoc. Purpose of javadoc javadoc is a program that reads your Java program and produces great-looking documentation in HTML format Without any help,
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Controlling Program Flow with Decision Structures.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4 - Finishing the Crab Game
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
A variable is a name for a value stored in memory.
Chapter 2 - Introduction to C Programming
Debugging and Random Numbers
Chapter 2 - Introduction to C Programming
Let's Race! Typing on the Home Row
The Little Crab Scenario
Chapter 2 - Introduction to C Programming
Variables ICS2O.
Chapter 2 - Introduction to C Programming
Chapter 1: Computer Systems
Programming in JavaScript
Chapter 2 - Introduction to C Programming
Programming in JavaScript
Methods, Data and Data Types
Tutorial 10: Programming with javascript
Chapter 2 - Introduction to C Programming
Classes, Objects and Methods
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Introduction to C Programming
Presentation transcript:

CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre

COURSE ADMINISTRATION Section 1 Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 2

For Project Assistance You can find the up-to-date hours and locations in the “Contact” section of UBLearns. Here are the names, s, and office hours as of 9/19/2011: (Also come to additional labs if you need help) Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 3 Name addressOffice hours Bich (pronounced “Bic”) (11-11:50am) Bell 329 F (12-12:50pm) Bell 329 Troy (3-5pm) Bell 329 Scott (10-11am) Bell 229 F (10-11am) Bell 340 Also at all times by AND video conference by appointment.

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 a TA, OR Go to an additional lab section before you go to your lab. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 4

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 Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 5

PROJECT 1 DESCRIPTION Section 2 Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 6

Project 1 Create a “Frogger” style game. Submit it by October 7 th, 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). Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 7

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

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 9

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) Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 10

Avatar should move using keyboard Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 11 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.

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 12

Actor Collisions Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 13 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)

Gooooooooooal! Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 14 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.

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 15

CHAPTER 4 Section 3 Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 16

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.) Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 17

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 18

“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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 19

“import” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 20

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 21

comments in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 22

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! Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 23

A “constructor” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 24

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 25

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 26

Use of “new” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class CrabWorld extends World { /** * Create the crab world (the beach). Our world has a size * of 560x560 cells, where every cell is just 1 pixel. */ public CrabWorld() { super(560, 560, 1); addObject( new Crab(), 150, 100 ); } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 27

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) Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 28

An “instance variable” in the code import greenfoot.*; // (Actor, World, Greenfoot, GreenfootImage) public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; // constructor method would be here // act method could be here // additional methods would follow } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 29

What this does in memory Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 30 The code will create two locations in memory, one called “image1”, the other called “image2”. They will not contain any information at this time.

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(); Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 31

Using “new” and a variable together So if we write: image1 = new GreenfootImage(“crab.png”); image2 = new GreenfootImage(“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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 32

What this would do in memory Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 33

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 34

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 “==“ Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 35

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”. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 36

“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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 37

“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 Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 38

“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 } Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 39

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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 40

Midterm exam The midterm exam will be held in lecture on October 21 st. 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. Monday, Sept. 19th, 2011 University at Buffalo: CSE 113 Instructor: Scott Settembre 41