Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Topics Introduction Types of Errors Exceptions Exception Handling
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
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.
Wombats Creating Games with Greenfoot The Walker School – Games and Simulations
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Chapter 1 - Getting to know Greenfoot Bruce Chittenden.
Chapter 1 - Getting to know Greenfoot Acknowledgement: Michael Kolling & Bruce Chittenden.
ITEC200 – Week03 Inheritance and Class Hierarchies.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Introduction to C Programming
Abstract Classes and Interfaces
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
CSE 113 Introduction to Computer Programming Lecture slides for Week 4 Monday, September 19 th, 2011 Instructor: Scott Settembre.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Greenfoot. Getting Started Open the Greenfoot download site: Select Greenfoot
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
CSE 113 Introduction to Computer Programming Lecture slides for Week 2 Wednesday, September 7 th, 2011 Instructor: Scott Settembre.
Chapter 1 - Getting to know Greenfoot
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Greenfoot Game Programming Club.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lesson 1: Writing a program. Java Java is a programming language Computer cannot understand it, though it can be translated ( compiled ) so a computer.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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,
Introduction To Greenfoot
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
Greeps A programming Competition. Greeps Description Greeps Scenario (a.zip file) Project 1: modify the Greeps class to increase your score in the competition.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 4 - Finishing the Crab Game
Greenfoot.
Chapter 4 - Finishing the Crab Game
Chapter 5 – Making Music: An On-Screen Piano (Part 1 – Using Loops)
Chapter 4 – C Program Control
Chapter 4 Assignment Statement
Interlude 2 - The Greeps Competition
Chapter 3 – Improving the Crab Game
Testing and Debugging.
Chapter 3 Assignment Statement
The Little Crab Scenario
Coding Concepts (Basics)
Breakout in Greenfoot Barb Ericson
Methods, Data and Data Types
Greenfoot November 8, 2009.
Presentation transcript:

Chapter 2 – The Little Crab Program:

Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It Follows That The Crab is Also an Actor

Little Crab Scenario Click Run and observe what happens

Little Crab Scenario Right Click the Crab Class Click On Open Editor

Little Crab Scenario When Run is Clicked the Crab does nothing This is because there is no Source Code in the act Method for the Crab.

Moving an Actor import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { // Add your action code here } Replace the comment with move();

Moving an Actor import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { move(); } Causes the crab to move 5 pixel in the current direction.

Compiling your code After adding the call to move( ); click Compile

Running your code Right Click on the Crab Class Drag a Crab into the World Click the Act and Run Buttons

Running your code Crab Moves to Edge of the World

Running your code Place Multiple Crabs into the World Click the Act and Run Buttons

Running your code Crabs All Move to Edge of the World

Turning an Actor import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { turn(5); } Replace move() with turn(5) and compile.

Turning an Actor Crab Spins to the Right

Turning an Actor Set turn to -5 and recompile

Turning an Actor Crab Spins to the Left

Turning an Actor The parameter given to the turn() method defines the change of direction per call to the act() method. The value is interpreted as an arcdegree. A positive value causes clockwise spin and a negative value counter-clockwise spin.

Moving and Turning import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { move (); turn (5); }

Moving and Turning move and turn

Moving and Turning Crab moves in a circle

Errors Syntax Error - Expected Semicolon

Various Errors 1. ‘;’ expected (Terminator symbol) 2. cannot find symbol - method Move() 3. turn(int) in Animal cannot be applied to (int,int) 4. turn(int) in Animal cannot be applied to (double) 5. act() in Crab cannot override act() in Animal; attempting to use incompatible return type 6. cannot find symbol - class voids 7. unclosed comment 8. class, interface, or enum expected 9. illegal start of type 10. illegal start of expression 11. reached end of file while parsing

Various Errors

Method Signatures void turn (int angle) boolean atWorldEdge ( ) void move ( ) Return Type Method Name Parameters All these methods are technically part of the Animal superclass. The Crab subclass is able to use them due to the process called Inheritance (a Crab is an Animal an can thus act like one). To see what else an Animal can do, we can check its Documentation

Documentation View Right Click on Animal Class and Click on Open editor

Documentation View Switch Between Source and Documentation Method Summary

Documentation View

Running A Method Right Click on the Crab and Select inherited from Animal Then Click on atWorldEdge ( )

Getting A Return Value

Running A Method Move the Crab to the Edge of the World and Repeat the Experiment

Getting A Return Value

Turning at-the-world-edge import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /* * This class defines a crab. Crabs live on the beach. */ public class Crab extends Animal { public void act() { if ( atWorldEdge ( ) ) { turn (17); } move ( ); }

Turning at-the-world-edge The Crab turns only if it is in contact with the world edge

A Logic Error Place the move () Inside the if Statement

A Logic Error If the Crab is at the Edge, Then It Turns Around. If the Crab is Not Near the Edge Nothing Happens.

Staying off Edges Crab should run around the world, peforming a complete turn once it hits a wall.

Reversing Direction Set the Turn to 180 and the Crab Should Reverse Direction

Reversing Direction Crab Moves from Side to Side

Introducing Variables In the previous example, we made the Crab turn around whenever it collides with the border. Let us assume that we want to implement a way to turn around without using the border as a stopping point. Instead, we want the crab to turn around after every 40 th step (Step 40, Step 80, Step 120 …).

Introducing Variables Does not help us in this situations

Introducing Variables We obviously need a way to count “steps” (a step is 1 call to the move() method, i.e. 5 pixel). This requires that we implement a “data field” to store the current number of steps and count up by one for each step taken. Such a data field is called a Variable: int stepCount = 0

Introducing Variables We obviously need a way to count steps. This requires that we implement a “data field” to store the current number of moves and count up by one for each step taken. Such a data field is called a Variable: int stepCount = 0 Type

Introducing Variables We obviously need a way to count steps. This requires that we implement a “data field” to store the current number of moves and count up by one for each step taken. Such a data field is called a Variable: int stepCount = 0 TypeName

Introducing Variables We obviously need a way to count steps. This requires that we implement a “data field” to store the current number of moves and count up by one for each step taken. Such a data field is called a Variable: int stepCount = 0 TypeName Initial Value

Introducing Variables Note the placement of the variables (outside of the move method). It does not belong to a specific method, but to the entire class.

Introducing Variables The algorithm for turning around after every 40 th step: 1. Increase stepCount by If stepCount is equal to 40, then set stepCount to 0 and turn around. 3. Take a step.

Introducing Variables Increase stepCount by 1 Check if we are taking a 40 th step Reset stepCount and turn around Take a step

Summary of Programming Techniques In this chapter, we have seen how to call methods such as move( ), with and without parameters. This will form the basis for all further Java Programming. We have encountered a glimpse of inheritance. Classes inherit the methods from their superclasses. And, very important we have seen how to make decisions. We have used an if-statement for conditional execution.

Concept Summary