Creating Java Programs with Greenfoot

Similar documents
Creating Java Programs with Greenfoot

Creating Java Programs with Greenfoot

Creating Java Programs with Greenfoot

Greenfoot! Introducing Java With Games And Simulations. Workshop material. Wombats. Object Orientation. Asteroids, Ants and other creatures.

Constants are named in ALL_CAPS, using upper case letters and underscores in their names.

Improving the Crab more sophisticated programming

CSE 142, Autumn 2018 Programming Assignment #9: Critters (20 points) Due Tuesday, December 4th, 9:00 PM

Object Oriented Programming and Design in Java. Session 10 Instructor: Bert Huang

Improving the crab: more sophisticated programming

The first program: Little Crab

Getting Started with Java Using Alice. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

JAVA MOCK TEST JAVA MOCK TEST II

ISY00245 Principles of Programming. Module 7

ITI Introduction to Computing II

ITI Introduction to Computing II

Inheritance (Outsource: )

Critters. Critter #2 Attack.ROAR Attack.POUNCE Attack.SCRATCH. Critter #1

College Board. AP CS A Labs Magpie, Elevens, and Picture Lab. New York: College Entrance Examination Board, 2013.

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

D2: explain how the structure and design of a game can assist in maintenance and capacity for extension.

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

The Oracle Academy Supports 1.5 million students in 95 countries

First Practical Exercise

Assignment 6: Dodo keeps getting smarter

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

More On inheritance. What you can do in subclass regarding methods:

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

C09: Interface and Abstract Class and Method

QUESTIONS FOR AVERAGE BLOOMERS

Critter #1 Attack.ROAR random winner #2 wins #1 wins Attack.POUNCE #1 wins random winner #2 wins Attack.SCRATCH #2 wins #1 wins random winner

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Getting to know Greenfoot

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS/120 Final Exam. Name

Assignment 4: Dodo gets smarter

Chapter 1: First Steps 1

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Programming Exercise 14: Inheritance and Polymorphism

Chapter 6: Inheritance

The Liskov Substitution Principle

CS-202 Introduction to Object Oriented Programming

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

ITI Introduction to Computing II

Inheritance CSC 123 Fall 2018 Howard Rosenthal

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

OVERRIDING. 7/11/2015 Budditha Hettige 82

CS112 Lecture: Working with Numbers

asteroids-day 3 Class Space

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Object-Oriented Programming

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers

CO Java SE 8: Fundamentals

Further abstraction techniques

Java Foundations. 7-2 Instantiating Objects. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

Chapter Two: Classes and Objects

Arrays Classes & Methods, Inheritance

AP Computer Science A Course Syllabus

Interfaces. CSE 142, Summer 2002 Computer Programming 1.

Interfaces. Readings and References. Recall the discussion of ArrayLists. How can we manage lists of objects? Reading

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Building Java Programs

COMP 401 Fall Recitation 6: Inheritance

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Session 4 Starting the Air Raid Game

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Programming via Java Subclasses

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Slide 1 CS 170 Java Programming 1

CS Standards Crosswalk: CSTA K-12 Computer Science Standards and Oracle Java Fundamentals (2014)

9/19/2018 Programming Data Structures. Polymorphism And Abstract

CSE 113 A. Announcements - Lab

Object Oriented Programming. Java-Lecture 11 Polymorphism

First IS-A Relationship: Inheritance

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Basic Object-Oriented Concepts. 5-Oct-17

Chapter 1: First Steps to Greenfoot 1

Curriculum Mapping for National Curriculum Statement Grades R-12 and Oracle Academy.

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

We are on the GUI fast track path

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Lecture 4: Inheritence and Abstract Classes

Introduction to Inheritance

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

2. The object-oriented paradigm

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Syllabus Honors Java Programming 1 & 2

Programming II (CS300)

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

11 Further abstraction techniques

Java Application Development

Transcription:

Creating Java Programs with Greenfoot Defining Methods 1 Copyright 2012, Oracle and/or its affiliates. All rights

Overview This lesson covers the following topics: Describe effective placement of methods in a super or subclass Simplify programming by creating and calling defined methods 2 Copyright 2012, Oracle and/or its affiliates. All rights

Efficient Placement of Methods At times, many lines of code are required to program a behavior. For example, you may want to program an instance to eat other objects, or turn when it reaches the edge of the world. Define new methods to save time and lines of code. Define a new method for an action below the act method. Call the new method in the act method. Define the method in the superclass if you want its subclasses to automatically inherit the method. 3 Copyright 2012, Oracle and/or its affiliates. All rights

Defined Methods Defined methods are new methods created by the programmer. These methods: Can be executed immediately, or stored and called later. Do not change the behavior of the class when stored. Separate code into shorter methods, making it easier to read. Defined methods create a new method that a class did not already possess. These methods are written in the class's source code below the act method. 4 Copyright 2012, Oracle and/or its affiliates. All rights

Steps to Define a New Method 1. Select a name for the method. 2. Open the Code editor for the class that will use the method. 3. Add the code for the method definition below the act method. 4. Call this new method from the act method, or store it for use later. 5 Copyright 2012, Oracle and/or its affiliates. All rights

Turn at the Edge of the World Problem: Instances stop and are unable to move when they reach the edge of the world. Instances should turn and move when they reach the edge of the world. Solution: Define a method in the Animal superclass to give all Animal subclasses the ability to turn when they reach the edge of the world. Call the new method in the subclasses that should be able to turn and move at the edge of the world. 6 Copyright 2012, Oracle and/or its affiliates. All rights

Test an Object's Position in the World To test if an object is near the edge of the world, this requires: Multiple boolean expressions to express if one or both conditions are true or false. Logic operators to connect the boolean expressions used to evaluate a condition. 7 Copyright 2012, Oracle and/or its affiliates. All rights

Logic Operators Logic operators can be used to combine multiple boolean expressions into one boolean expression. Logic Operator Means Definition Exclamation Mark (!) Double ampersand (&&) NOT AND Reverses the value of a boolean expression (if b is true,!b is false. If b is false,!b is true). Combines two boolean values, and returns a boolean value which is true if and only if both of its operands are true. Two lines (II) OR Combines two boolean variables or expressions and returns a result that is true if either or both of its operands are true. 8 Copyright 2012, Oracle and/or its affiliates. All rights

Create the Animal Superclass Before creating the defined methods, create a new subclass of the Actor class named Animal. This class has no image and will not have instances that act in the scenario, but will hold some defined methods that other subclasses will inherit. 9 Copyright 2012, Oracle and/or its affiliates. All rights

Create the Animal Subclasses Create subclasses of the Animal superclass that will have instances that act in the scenario. In this example, a Duke subclass and Code subclass were added for a game where Duke, the Java mascot, eats Code objects. 10 Copyright 2012, Oracle and/or its affiliates. All rights

Define atworldedge Method in Superclass Open the Code editor for the Animal superclass. Write the code for the atworldedge method, below the act method. Compile the code and then close the Code editor. 11 Copyright 2012, Oracle and/or its affiliates. All rights

Methods Used in atworldedge Method The methods used in atworldedge include: getx: An Actor method that returns the x-coordinate of the actor's current location. gety: An Actor method that returns the y-coordinate of the actor's current location. getworld: An Actor method that returns the world that this actor lives in. getheight: A GreenfootImage class method that returns the height of the image. getwidth: A GreenfootImage class method that returns the width of the image. : Symbol for a conditional statement that means OR. 12 Copyright 2012, Oracle and/or its affiliates. All rights

Call atworldedge Method in Subclass Open the Code editor for an Animal subclass. Create an IF statement that calls the atworldedge method as a condition. The condition tells the instance how many degrees to turn if the condition is true. Compile the code and run the scenario to test it. 13 Copyright 2012, Oracle and/or its affiliates. All rights

Class Documentation The Animal class documentation shows the new atworldedge method after its defined. All subclasses of the Animal superclass inherit this method. 14 Copyright 2012, Oracle and/or its affiliates. All rights

Defined Method to Eat Objects You can write code in your game so a predator object is able to eat prey objects. Create a defined method in the act method of the Animal superclass called cansee to enable objects of Animal subclasses to eat other objects. To create this defined method: Declare a variable for the prey. Use an assignment operator to set the value of the variable equal to the return value of the getoneobjectatoffset method. 15 Copyright 2012, Oracle and/or its affiliates. All rights

Define cansee Method Define the cansee Method in the Animal superclass. This method returns true if the predator object (Duke) lands on a prey object (Code). 16 Copyright 2012, Oracle and/or its affiliates. All rights

getoneobjectatoffset Method The cansee Method uses the getoneobjectatoffset method. This method returns one object that is located at the specified cell (relative to this objects location). Review this method in the Actor documentation. 17 Copyright 2012, Oracle and/or its affiliates. All rights

Define eat Method Create a defined method in the Animal superclass called eat. This method will instruct Duke to eat the Code object if he lands on it. 18 Copyright 2012, Oracle and/or its affiliates. All rights

Define lookforcode Method Create a defined method in the animal subclass (Duke class) called lookforcode that checks if Duke has landed on a Code object. If so, Duke will eat the Code object. 19 Copyright 2012, Oracle and/or its affiliates. All rights

Call lookforcode in Act Method Call the new lookforcode method in Duke's act method. Run the animation to test the code. 20 Copyright 2012, Oracle and/or its affiliates. All rights

Terminology Key terms used in this lesson included: Defined methods 21 Copyright 2012, Oracle and/or its affiliates. All rights

Summary In this lesson, you should have learned how to: Describe effective placement of methods in a super or subclass Simplify programming by creating and calling defined methods 22 Copyright 2012, Oracle and/or its affiliates. All rights

Practice The exercises for this lesson cover the following topics: Examining and simplifying programming by creating and calling defined methods 23 Copyright 2012, Oracle and/or its affiliates. All rights