Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Fields, Constructors, Methods
CS 112 Introduction to Programming Critters/Event-Driven Programming; Interface Yang (Richard) Yang Computer Science Department Yale University 308A Watson,
CS 112 Introduction to Programming
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Critters; Subtype Polymorphism Reading: HW9 Handout, Chapter 9.2.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters.
Games and Simulations O-O Programming in Java The Walker School
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
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.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Algorithms Writing instructions in the order they should execute.
Can I get your number? By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ) reading:
Building Java Programs Chapter 8 Lecture 8-3: Object state; Homework 8 (Critters) reading:
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
CS 112 Introduction to Programming Method Overriding; Object Hierarchy; Event-Driven Programming Yang (Richard) Yang Computer Science Department Yale University.
CS 112 Introduction to Programming Critters, Polymorphism Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Weds, Nov. 26th Reading: Section Handout
Homework 8: Critters reading: HW8 spec.
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
HW11 Assignment Specification
CS2102: Lecture on Abstract Classes and Inheritance
Lecture 8-3: Encapsulation, this
Critter exercise: Snake
Homework 8: Critters (cont.)
CSE 142 Critters (IPL) behavior: Ant Bird Hippo Vulture
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Module 4 Loops.
CS139 October 11, 2004.
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8:The For Loop AP Computer Science Principles.
Homework 8: Critters (cont.)
Building Java Programs
Building Java Programs
Homework 9: Critters (cont.)
Building Java Programs
Building Java Programs
Presentation transcript:

Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec

Copyright 2010 by Pearson Education 2 Critters A simulation world with animal objects with behavior: eat eating food fight animal fighting getColor color to display getMove movement toString letter to display You must implement: Ant Bird Hippo Vulture Husky (creative)

Copyright 2010 by Pearson Education 3 A Critter subclass public class name extends Critter {... } extends Critter tells the simulator your class is a critter an example of inheritance Write some/all 5 methods to give your animals behavior.

Copyright 2010 by Pearson Education 4 How the simulator works When you press "Go", the simulator enters a loop: move each animal once ( getMove ), in random order if the animal has moved onto an occupied square, fight ! if the animal has moved onto food, ask it if it wants to eat Key concept: The simulator is in control, NOT your animal. Example: getMove can return only one move at a time. getMove can't use loops to return a sequence of moves. It wouldn't be fair to let one animal make many moves in one turn! Your animal must keep state (as fields) so that it can make a single move, and know what moves to make later.

Copyright 2010 by Pearson Education 5 Critter exercise: Cougar Write a critter class Cougar (the dumbest of all animals): MethodBehavior constructor public Cougar() eat Always eats. fight Always pounces. getColor Blue if the Cougar has never fought; red if he has. getMove Walks west until he finds food; then walks east until he finds food; then goes west and repeats. toString"C"

Copyright 2010 by Pearson Education 6 Ideas for state You must not only have the right state, but update that state properly when relevant actions occur. Counting is helpful: How many total moves has this animal made? How many times has it eaten? Fought? Remembering recent actions in fields is helpful: Which direction did the animal move last? How many times has it moved that way? Did the animal eat the last time it was asked? How many steps has the animal taken since last eating? How many fights has the animal been in since last eating?

Copyright 2010 by Pearson Education 7 Keeping state How can a critter move west until it finds food? public Direction getMove() { while (animal has not eaten) { return Direction.EAST; } while (animal has not eaten a second time) { return Direction.EAST; } } private int moves; // total moves made by this Critter public Direction getMove() { moves++; if (moves % 4 == 1 || moves % 4 == 2) { return Direction.WEST; } else { return Direction.EAST; } }

Copyright 2010 by Pearson Education 8 Cougar solution import java.awt.*; // for Color public class Cougar extends Critter { private boolean west; private boolean fought; public Cougar() { west = true; fought = false; } public boolean eat() { west = !west; return true; } public Attack fight(String opponent) { fought = true; return Attack.POUNCE; }...

Copyright 2010 by Pearson Education 9 Cougar solution... public Color getColor() { if (fought) { return Color.RED; } else { return Color.BLUE; } public Direction getMove() { if (west) { return Direction.WEST; } else { return Direction.EAST; } public String toString() { return "C"; }

Copyright 2010 by Pearson Education 10 Testing critters Focus on one specific critter of one specific type Only spawn 1 of each animal, for debugging Make sure your fields update properly Use println statements to see field values Look at the behavior one step at a time Use "Tick" rather than "Go"

Copyright 2010 by Pearson Education 11 Critter exercise: Snake MethodBehavior constructor public Snake() eat Never eats fight always forfeits getColor black getMove 1 E, 1 S; 2 W, 1 S; 3 E, 1 S; 4 W, 1 S; 5 E,... toString"S"

Copyright 2010 by Pearson Education 12 Determining necessary fields Information required to decide what move to make? Direction to go in Length of current cycle Number of moves made in current cycle Remembering things you've done in the past: an int counter? a boolean flag?

Copyright 2010 by Pearson Education 13 Snake solution import java.awt.*; // for Color public class Snake extends Critter { private int length; // # steps in current horizontal cycle private int step; // # of cycle's steps already taken public Snake() { length = 1; step = 0; } public Direction getMove() { step++; if (step > length) { // cycle was just completed length++; step = 0; return Direction.SOUTH; } else if (length % 2 == 1) { return Direction.EAST; } else { return Direction.WEST; } public String toString() { return "S"; }