Creating Java Programs with Greenfoot

Similar documents
Creating Java Programs with Greenfoot

Creating Java Programs with Greenfoot

Improving the Crab more sophisticated programming

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

Creating Java Programs with Greenfoot

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

Improving the crab: more sophisticated programming

Getting to know Greenfoot

The Oracle Academy Supports 1.5 million students in 95 countries

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

The first program: Little Crab

2. The object-oriented paradigm

Inheritance and Interfaces

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

2. The object-oriented paradigm!

CSE 113 A. Announcements - Lab

Programming overview

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CO Java SE 8: Fundamentals

INHERITANCE AND EXTENDING CLASSES

ISY00245 Principles of Programming. Module 7

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Session 8 Finishing Touches to the Air Raid Game

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

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

CSE 113. Announcements

Arrays Classes & Methods, Inheritance

AP CS Unit 6: Inheritance Notes

Lecture 18 Tao Wang 1

Using the API: Introductory Graphics Java Programming 1 Lesson 8

CS 1316 Exam 1 Summer 2009

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

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java. Representing Data. Representing data. Primitive data types

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Programming Exercise 14: Inheritance and Polymorphism

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Session 1 Welcome to Greenfoot & Code Breaker Authored by Brian Cullen

Chapter Two: Classes and Objects

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

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

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Chapter 14 Abstract Classes and Interfaces

Polymorphism: Inheritance Interfaces

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Object-Oriented Programming

Java Primer 1: Types, Classes and Operators

Definition of DJ (Diminished Java)

Inheritance, Polymorphism, and Interfaces

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

Lecture 4: Inheritence and Abstract Classes

Outline. More optimizations for our interpreter. Types for objects

Chapter 1: First Steps 1

Practice for Chapter 11

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Java Object Oriented Design. CSC207 Fall 2014

Introduction to Programming Using Java (98-388)

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

APCS Semester #1 Final Exam Practice Problems

Exam Duration: 2hrs and 30min Software Design

Logistics. Final Exam on Friday at 3pm in CHEM 102

Class, Variable, Constructor, Object, Method Questions

CLASSES AND OBJECTS IN JAVA

Object Oriented Programming

Inheritance and Polymorphism

c) And last but not least, there are javadoc comments. See Weiss.

About Technocamps. We go around schools and show you lots of interesting stuff! We also do things we call bootcamps during holidays!

Introduction to Computing II (ITI 1121) Final Examination

EECS2030 Week 7 worksheet Tue Feb 28, 2017

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

Chapter 10 Introduction to Classes

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Java Classes, Inheritance, and Interfaces

Inheritance (Outsource: )

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Basic Object-Oriented Concepts. 5-Oct-17

Introduction to Java. Handout-1d. cs402 - Spring

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

The Thomas Hardye School Summer Preparation Task Computer Science AS

Inheritance (Part 2) Notes Chapter 6

Java Fundamentals (II)

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

PROGRAMMING LANGUAGE 2

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Rules and syntax for inheritance. The boring stuff

CLASS DESIGN. Objectives MODULE 4

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Lecture 10: Interfaces

Transcription:

Creating Java Programs with Greenfoot Using Randomization and Understanding Dot Notation and Constructors 1 Copyright 2012, Oracle and/or its affiliates. All rights

Overview This lesson covers the following topics: Create randomized behaviors Define comparison operators Create IF-ELSE control statements Create an instance of a class Recognize and describe dot notation 2 Copyright 2012, Oracle and/or its affiliates. All rights

getrandomnumber Method The getrandomnumber method is a static method that returns a random number between zero and a parameter limit. This method is used to eliminate predictability in your program. Method signature: public static int getrandomnumber(int limit) 3 Copyright 2012, Oracle and/or its affiliates. All rights

Dot Notation New subclasses that you create do not inherit the getrandomnumber method. This method must be called from the Greenfoot class using dot notation. Example : Greenfoot.getRandomNumber(20); When you want to use a method but it is not inherited by the class you are programming, specify the class or object that has the method before the method name, then a dot, then the method name. This technique is called dot notation. 4 Copyright 2012, Oracle and/or its affiliates. All rights

Dot Notation Format The format for dot notation code includes: Name of class or object to which the method belongs Dot Name of method to call Parameter list Semicolon class-name.method-name (parameters); object-name.method-name (parameters); 5 Copyright 2012, Oracle and/or its affiliates. All rights

Dot Notation Example The getrandomnumber method shown below: Calls a random number between 0 and up to, but not including 15. Returns a random number between 0 and 14. Greenfoot.getRandomNumber(15) 6 Copyright 2012, Oracle and/or its affiliates. All rights

Greenfoot API Reference the Greenfoot Application Programmers' Interface (API) to examine additional methods to call using dot notation. The Greenfoot Application Programmers' Interface lists all of the classes and methods available in Greenfoot. 7 Copyright 2012, Oracle and/or its affiliates. All rights

Steps to View Methods in Greenfoot Class 1. In the Greenfoot environment, select the Help menu. 2. Select Greenfoot Class Documentation. 3. Click the Greenfoot class. 4. Review the method signatures and descriptions. 8 Copyright 2012, Oracle and/or its affiliates. All rights

Greenfoot API Interface 9 Copyright 2012, Oracle and/or its affiliates. All rights

Comparison Operators Use comparison operators to compare a randomized value to another value in a control statement. The example below determines if the random number is less than 20. If it is, then the object turns ten degrees. if (Greenfoot.getRandomNumber(100) < 20) { turn(10); } Comparison operators are symbols that compare two values. 10 Copyright 2012, Oracle and/or its affiliates. All rights

Comparison Operator Symbols Symbol Description < Less than > Greater than <= Less than or equal >= Greater than or equal == Equal!= Not equal 11 Copyright 2012, Oracle and/or its affiliates. All rights

Gaming Problem Solved with Random Behavior Problem: A banana object should move randomly so it is more challenging for the keyboard-controlled object, a monkey, to eat it. Solution: The banana should turn a small amount as it moves. To code this solution, turn the banana a random number of degrees, up to 20 degrees, 6% of the time as it moves. if (Greenfoot.getRandomNumber(100) < 6) { turn(greenfoot.getrandomnumber(20)); } 12 Copyright 2012, Oracle and/or its affiliates. All rights

Random Behavior Format The programming statement below includes: IF control statement with the getrandomnumber method. Parameter limit of 100. Comparison operator <. Number 6 to limit the range of values to return to 0-5. Method body with statement to indicate that the object should turn up to 20 degrees if the condition is true. if (Greenfoot.getRandomNumber(100) < 6) { turn(greenfoot.getrandomnumber(20)); } 13 Copyright 2012, Oracle and/or its affiliates. All rights

Conditional Behavior Instances can be programmed to perform specific behaviors if a condition is not met, using an IF-ELSE statement. For example, if an instance is programmed to turn 6% of the time, what does it do the other 94% of the time? An IF-ELSE statement executes its first code segment if a condition is true, and its second code segment if a condition is false, but not both. 14 Copyright 2012, Oracle and/or its affiliates. All rights

IF-ELSE Statement Execution True Evaluate the Condition False Execute IF Statement Execute ELSE Statement 15 Copyright 2012, Oracle and/or its affiliates. All rights

IF-ELSE Statement Format if (condition) { statements; } else { statements; } 16 Copyright 2012, Oracle and/or its affiliates. All rights

IF-ELSE Statement Example If a random number between 0-6 is selected, turn 10 degrees. Otherwise, turn 5 degrees. if (Greenfoot.getRandomNumber(100) < 7) { turn(10); } else { turn(5); } 17 Copyright 2012, Oracle and/or its affiliates. All rights

Automate Creation of Instances Using the World subclass, actor instances can be programmed to automatically appear in the world when a scenario is initialized. In Greenfoot, the default behavior for instances is as follows: The World subclass instance is automatically added to the environment after compilation or initialization of the scenario. The Actor subclass instances must be manually added by the player. 18 Copyright 2012, Oracle and/or its affiliates. All rights

Automate Creation of Instances in a Scenario Problem: When a Greenfoot scenario (such as leaves and wombats) is started, the instances must be manually added by the player to play the game. Solution: Program instances to be automatically added to the world when the scenario is initialized. 19 Copyright 2012, Oracle and/or its affiliates. All rights

World Class Source Code To understand how to automate creation of Actor instances, you need to understand how the World class source code is structured. The World constructor is used to automate creation of Actor instances when the scenario is initialized. Import statement Class header Comment Constructor 20 Copyright 2012, Oracle and/or its affiliates. All rights

Constructors Constructors: Define the instance's size and resolution. Have no return type. Have the same name as the name of the class. For example, a World constructor is named World. A constructor is a special kind of method that is automatically executed when a new instance of the class is created. 21 Copyright 2012, Oracle and/or its affiliates. All rights

World Constructor Example The example constructor below constructs the World superclass instance as follows: Size: x = 600, y = 400. Resolution: 1 pixel per cell. Keyword super in the constructor s body calls the superclass World for each instance of the DukeWorld subclass. public DukeWorld() { super(600, 400, 1); } Size Resolution 22 Copyright 2012, Oracle and/or its affiliates. All rights

Automatically Create Actor Instances This World constructor adds a Duke object at specified X and Y coordinates using the addobject method. public DukeWorld() { super(560, 560, 1); addobject (new Duke(), 150, 100); } 23 Copyright 2012, Oracle and/or its affiliates. All rights

addobject Method The addobject method is a World class method that adds a new object to the world at specific x and y coordinates. It includes: Keyword new to tell Greenfoot to create a new object of a specific class. Method parameters: Named object from Actor class. Integer value of X coordinate. Integer value of Y coordinate. Method signature: void addobject(actor object, int x, int y) 24 Copyright 2012, Oracle and/or its affiliates. All rights

new Keyword The new keyword creates new instances of existing classes. It starts with the keyword new, followed by the constructor to call. The parameter list passes arguments (values) to the constructor that are needed to initialize the object's instance variables. The default constructor has an empty parameter list and sets the object's instance variables to their default values. new Constructor-name() 25 Copyright 2012, Oracle and/or its affiliates. All rights

Greenfoot World Coordinate System 0 Y 0 X 26 Copyright 2012, Oracle and/or its affiliates. All rights

Add Objects Using World Constructor Example 27 Copyright 2012, Oracle and/or its affiliates. All rights

Terminology Key terms used in this lesson included: Comparison operators Constructor Dot notation new Keyword 28 Copyright 2012, Oracle and/or its affiliates. All rights

Summary In this lesson, you should have learned how to: Create randomized behaviors Define comparison operators Create IF-ELSE control statements Create an instance of a class Recognize and describe dot notation 29 Copyright 2012, Oracle and/or its affiliates. All rights

Practice The exercises for this lesson cover the following topics: Creating randomized actor behavior using comparison operators Creating instances of a class 30 Copyright 2012, Oracle and/or its affiliates. All rights