Adding and Eating Worms Mrs. C. Furman August 23, 2010.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Getting to know Greenfoot
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.
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
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.
Information Hiding and Encapsulation
1 Doc Comment Conventions. 2 Write for your audience Rule 32: Write documentation for– those who must use your code Users should not need to care how.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Test review. When? Monday 9/27 in class Know Greenfoot How many classes? Top-most classes? What does each button do? Lowest child class?
Games and Simulations O-O Programming in Java The Walker School
As you come in…  DOWNLOADS FOR TODAY:  CarGameTeacherStarter.a2w  Online student textbook.
Chapter 8: String Manipulation
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Visual Basic Chapter 1 Mr. Wangler.
Lecture 2: Static Methods, if statements, homework uploader.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
The Java Programming Language
Chapter 1 - Getting to know Greenfoot
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
22/11/ Selection If selection construct.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Programming - Motion Intro to Robotics. Motors and Sensors Setup The first thing we need to do is tell ROBOTC that we have motors on our robot. Choose.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Improving the Crab Mrs. C. Furman August 19, 2010.
Netbeans QuickStart. Creating a project File->New Project –For now you want General->Java Application –Then fill in the project details.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Chapter 4 - Finishing the Crab Game
Chapter 4 - Finishing the Crab Game
4. Java language basics: Function
More Sophisticated Behavior
Working with Java.
Module Road Map Refactoring Why Refactoring? Examples
Chapter 3 – Improving the Crab Game
Chapter 7 Top-Down Development
Creating Games with Greenfoot
CS2102: Lecture on Abstract Classes and Inheritance
The Little Crab Scenario
Overview of Eclipse Lectures
Unit-1 Introduction to Java
If selection construct
If selection construct
Programming - Buttons Intro to Robotics.
Anatomy of a Java Program
Programming - Buttons Intro to Robotics.
More on Creating Classes
Barb Ericson Georgia Institute of Technology Oct 2005
Presentation transcript:

Adding and Eating Worms Mrs. C. Furman August 23, 2010

Let’s add a Worm Class  What class should we extend to add a Worm?  Worm is-a Animal  Select New Subclass from the Animal menu Click here

Setting up the new Subclass  Enter the name as Worm  Class names in Java should always start with a capital letter  Rules for class names:  Start with a capital letter  Contain only letters, numbers or underscores  Can’t be a keyword  As a convention it should describe the class.

Name and add a picture  After you name the class  Add a picture. Choose the worm.png  Select ok

Exercise  Compile  Add some worms and crabs to the world.  Run the scenario  What happens to the worms?  What happens when a crab meets a worm?

How can we get Crabs to Eat Worms?  What would we need to be able to do to get Crabs to eat Worms?

Making Crab Eat Worms  Lets see if there are any classes in Animal that can help us…  Open the source code, and choose documentation.  Any methods???

boolean canSee(java.lang.Class clss)  What is the return type?  What is the method name?  What is the parameter?

void eat (java.lang.Class clss)  What is the return type?  What is the method name?  What is the parameter?

Using canSee and eat to eat worms.  canSee - will tell us is there is a worm near us.  it returns true or false  eat – eats the worm

The code if (this.canSee(Worm.class)) { this.eat(Worm.class); this.eat(Worm.class);}  Worm.class – this specifies the class we are looking for and eating.  Add the above if statement to the act method.

Adding Methods  Our methods should specify a single “behavior” that we want to perform.  act is starting to have too many behaviors that it is charged with.

lookForWorm method  We would like to create a lookForWorm method that contains the if statement we just added to act.  It will look for worms and then eat them.

Comments  When we add a new Method, we will want to add a description of what the method does.  What does lookForWorms do?

Different Types of Comments  // - used to generate a single line of code  /* - starts a multiple line of comment and ends with */  /** - starts a javadoc comment. This will generate the documentation we see when we open the code. It also ends with */

Comment /** * Check whether we have found a worm * If we have found a worm, we eat it. * If we haven’t, we do nothing. * PostCondition: After this method has * been executed worms near the crab * have been removed. */

Adding the method  Is our method an action or a question?  What should our return type be?

Add the method public void lookForWorm() { if (this.canSee(Worm.class) ) if (this.canSee(Worm.class) ) { this.eat(Worm.class); this.eat(Worm.class); }}

Modify Act method  Remove the eating if statement from act  Replace with a call to the lookForWorm method  this.lookForWorm();  Compile and Run the code.

Other Methods  Are there other behaviors in Act that can be pulled out into a method?

Exercise  Create method randomTurn.  What would the return type be? Is it a question, or an action?  Move the code that does the random turning out of Act and put it into randomTurn.  Replace the code in Act with a call to this.randomTurn();

Exercise  Create another method for turning at the edge called turnAtEdge  What is the return type? Is it a question, or an action?  Remove the code from Act and move it into turnAtEdge. Replace the code in Act with a call to this.turnAtEdge();