Improving the Crab Mrs. C. Furman August 19, 2010.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

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.
Microsoft Excel Setting up Constants; the Payment, Present Value, and Conditional functions; What-If analysis.
True or false A variable of type char can hold the value 301. ( F )
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Test review. When? Monday 9/27 in class Know Greenfoot How many classes? Top-most classes? What does each button do? Lowest child class?
As you come in…  DOWNLOADS FOR TODAY:  CarGameTeacherStarter.a2w  Online student textbook.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Lecture 2: Static Methods, if statements, homework uploader.
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.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Variables Tutorial 3c variable A variable is any symbol that can be replaced with a number to solve a math problem. An open sentence has at least one.
Chapter 1 - Getting to know Greenfoot
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
Solve Inequalities 1 © Evergreen Public Schools 11/1/2010.
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Mathematical Expressions, Conditional Statements, Control Structures
Algebra Equations Lesson 1-7 Pages Equations An Equation is a sentence that contains an equals = sign. The equals = sign tells you that the expression.
Algorithms Writing instructions in the order they should execute.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Solve Inequalities 1 © Evergreen Public Schools 7/28/10.
Chapter 4 Lesson 1 Inequalities and their Graphs.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Programming with LabVIEW Intro to programming and.
Creating and Using Class Methods. Definition Class Object.
Expression or Equation? 3x + 7 4y – 10 = 54 5z + 32 = 47 6x + 2y (8x – 1) 2 + y 2x = 16.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Data Structures Arrays and Lists Part 2 More List Operations.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Computer Science Up Down Controls, Decisions and Random Numbers.
Selection Statement Chapter 3. A Java Language Program Is a Class A Class has data declarations and methods A Method has statements or instructions to.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Chapter 3 – Improving the Crab Game
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Creating Games with Greenfoot
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Building Java Programs
Building Java Programs
The Little Crab Scenario
Coding Concepts (Sub- Programs)
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Building Java Programs
Chapter 5, Conditionals Brief Notes
CS139 October 11, 2004.
Adding Behaviors (methods)
Building Java Programs
Relational Operators.
Just Enough Java 17-May-19.
U3L8 Creating Functions with Parameters
Presentation transcript:

Improving the Crab Mrs. C. Furman August 19, 2010

Adding Random Behavior  Currently, our crab moves from one edge to another in a straight line, turning in the same way each time.  Is this how crabs move???

int getRandomNumber (int limit)  We will be using this method to generate a random number. Return typeMethod NameFormal Parameter List

Greenfoot.getRandomNumber (20)  The above is a method call.  It returns a value from 0 – 19. That is 20 values to choose from. Class name method name Actual Parameter dot operator

Method Calls classname.method (parameter list)  This is the general form of a method call.  When we used move() or turn() we didn’t use a class name or object name, because move() and turn() are methods of our superclass.  If you are using methods of another class, you need to specify. or object

move() and turn()  To continue the general form of: object.methodname(parameters) object.methodname(parameters)  We can use the keyword this when we are calling methods in our own classes.  So, instead of: move() try this.move();

Exercise  Modify the act method in little-crab-1 by adding this and dot in front of each call to turn() and move().  Your act method should look as follows: public void act() public void act() { if ( this.atWorldEdge() ) if ( this.atWorldEdge() ) { this.turn(17); this.turn(17); } this.move(); this.move(); }

Random turn  Say we want to turn our crab occasionally on a random call to act. if (something is true) { this.turn(5); this.turn(5);}

What can we use to replace the something is true?  Say we want to turn 10% of the time.  So if we generate 100 random numbers, we want to call this.turn(5), when the numbers 0 – 9 are generated.

Relationship Operators  < less than  > greater than  <= less than or equal  >= greater than or equal  != not equal  == equal  note: to ask if two things are equal you need 2 = signs.

Boolean Expression  An expression that can be determined to be either true or false.  To find 10%, we can generate 100 numbers and if the number is less than 10 (0 – 9),then it would be in the range of the 10% of the 100 numbers. Greenfoot.getRandomNumber (100) < 10 Greenfoot.getRandomNumber (100) < 10

Modify your code public void act() { if(this.atWorldEdge()) if(this.atWorldEdge()) { this.turn(17); this.turn(17); } if (Greenfoot.getRandomNumber(100) < 10) if (Greenfoot.getRandomNumber(100) < 10) { this.turn(5); this.turn(5); } this.move(); this.move();}

Exercise Instead of turning 5 each time, modify your program to turn a random amount.

Exercise  Our crab is still only turning to the right. That’s not normal behavior for a crab, so let’s fix this. Modify your code so that the crab turns either left or right up to 45 degrees each time it turns.

Exercise  Run your project with multiple crabs. Do they all turn the same way at the same time?