Chapter 2 - The First Program: Little Crab

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

The if-else Statements
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Getting to know Greenfoot
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.
Chapter 8 - Creating Images and Sound Bruce Chittenden.
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.
Program: Little Crab Mr Gano.
Greenfoot Asteroids Mrs. C. Furman August 16, 2010.
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.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Test review. When? Monday 9/27 in class Know Greenfoot How many classes? Top-most classes? What does each button do? Lowest child class?
Abstract Classes and Interfaces
Games and Simulations O-O Programming in Java The Walker School
Chapter 5 - Making Music: An On-Screen Piano
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
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
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.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Chapter 1 - Getting to know Greenfoot
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Greenfoot Game Programming Club.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Introduction To Greenfoot
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
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.
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 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 Assignment Statement
Interlude 2 - The Greeps Competition
Programming: Simple Control Structures
Chapter 3 – Improving the Crab Game
Interfaces.
Inheritance and Polymorphism
Programming: Simple Control Structures
Chapter 3 Assignment Statement
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Creating Games with Greenfoot
The Little Crab Scenario
LRobot Game.
Conditional Statements
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Chapter 5, Conditionals Brief Notes
CS139 October 11, 2004.
Methods, Data and Data Types
Greenfoot November 8, 2009.
More on Creating Classes
More on Creating Classes part 3
1.7 A Second Example.
Presentation transcript:

Chapter 2 - The First Program: Little Crab Bruce Chittenden

2.1 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

Exercise 2.1 Click Run and Nothing Happens

Exercise 2.1 Right Click the Crab Class Click On Open Editor

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

2.2 Making the Crab Move 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();

Code 2.1 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(); }

Exercise 2.2 Add the call to move( ); Click Compile

Exercise 2.2 Right Click on the Crab Class Drag a Crab into the World Click the Act and Run Buttons

Exercise 2.2 Crab Moves to Edge of the World

Exercise 2.3 Place Multiple Crabs into the World Click the Act and Run Buttons

Exercise 2.3 Crabs All Move to Edge of the World

2.3 Turning public void act () { turn (5); }

Exercise 2.4 Set turn to 5 and recompile

Exercise 2.4 Crab Spins to the Right

Exercise 2.5 Set turn to -5 and recompile

Exercise 2.5 Crab Spins to the Left

Code 2.2 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); }

Exercise 2.6 move and turn

Exercise 2.6 Crab moves in a circle

Syntax Error - Expected Semicolon Exercise 2.7 Syntax Error - Expected Semicolon

Exercise 2.8 ‘;’ expected cannot find symbol - method Move() turn(int) in Animal cannot be applied to (int,int) turn(int) in Animal cannot be applied to (double) act() in Crab cannot override act() in Animal; attempting to use incompatible return type cannot find symbol - class voids unclosed comment class, interface, or enum expected illegal start of type illegal start of expression

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

Exercise 2.8

2.4 Dealing with Screen Edges Right Click on Animal Class and Click on Open editor

Source and Documentation Documentation View Method Summary Switch Between Source and Documentation

Exercise 2.9

Method Signatures Return Type Method Name Parameters void turn (int angle) boolean atWorldEdge ( ) void move ( )

Source for atWorldEdge ( ) /* * Test if we are close to one of the edges of the world. Return true is we are. */ public boolean atWorldEdge() { if(getX() < 20 || getX() > getWorld().getWidth() - 20) return true; if(getY() < 20 || getY() > getWorld().getHeight() - 20) else return false; }

Right Click on the Crab and Select inherited from Animal Exercise 2.10 Right Click on the Crab and Select inherited from Animal Then Click on atWorldEdge ( )

Exercise 2.10

Move the Crab to the Edge of the World and Repeat the Experiment Exercise 2.11 Move the Crab to the Edge of the World and Repeat the Experiment

Exercise 2.11

Code 2.3 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 ( );

Exercise 2.12

Crab Runs Around the World Turning when it Encounters a Wall Exercise 2.12 Crab Runs Around the World Turning when it Encounters a Wall

Set the Turn to 180 and the Crab Should Reverse Direction Exercise 2.13 Set the Turn to 180 and the Crab Should Reverse Direction

Crab Moves from Side to Side Exercise 2.13 Crab Moves from Side to Side

Place the move () Inside the Exercise 2.14 Place the move () Inside the if Statement

Exercise 2.14 If the Crab is Not Near the Edge Nothing Happens. If the Crab is at the Edge, Then It Turns Around.

2.5 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