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

Similar documents
Creating Java Programs with Greenfoot

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

Creating Java Programs with Greenfoot

asteroids-day 3 Class Space

Creating Java Programs with Greenfoot

Chapter Two: Classes and Objects

Improving the Crab more sophisticated programming

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

Assignment 4: Dodo gets smarter

Improving the crab: more sophisticated programming

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

Session 4 Starting the Air Raid Game

JAVA MOCK TEST JAVA MOCK TEST II

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

Java Primer 1: Types, Classes and Operators

Creating Java Programs with Greenfoot

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

CS112 Lecture: Working with Numbers

3.1 Class Declaration

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

3. Java - Language Constructs I

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

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

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

ITI Introduction to Computing II

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

ITI Introduction to Computing II

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

CSE 113 A. Announcements - Lab

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

Building Java Programs

ITI Introduction to Computing II

COMP 401 Fall Recitation 6: Inheritance

ISY00245 Principles of Programming. Module 7

Lecture 3. Lecture

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

Inheritance Systems. Merchandise. Television Camcorder Shirt Shoe Dress 9.1.1

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

Slide 1 CS 170 Java Programming 1

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

JFrame In Swing, a JFrame is similar to a window in your operating system

Practice Midterm 1. Problem Points Score TOTAL 50

Chapter 9 Worksheet. public class AntHill extends Actor { private int ants = 0; private int maxants = 40; private Counter foodcounter;

CSE 214 Computer Science II Java Classes and Information Hiding

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

Chapter 24. Graphical Objects The GraphicalObject Class

Assignment 6: Dodo keeps getting smarter

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS 307 Midterm 1 Fall 2007

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Reviewing OO Concepts

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

Chapter 6: Inheritance

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

CS 1316 Exam 1 Summer 2009

STUDENT LESSON A12 Iterations

CS1150 Principles of Computer Science Objects and Classes

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

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

Name CS/120 Sample Exam #1 -- Riley. a) Every program has syntax, which refers to the form of the code, and, which refers to the meaning of the code.

AP Computer Science Homework Set 3 Class Methods

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

1 Shyam sir JAVA Notes

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

CS/120 Final Exam. Name

Objects and Classes (1)

CS 314 Exam 2 Spring

CS 231 Data Structures and Algorithms, Fall 2016

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

Java Classes & Primitive Types

Lecture 4: The class hierarchy; static components

CS 1302 Chapter 9 (Review) Object & Classes

Java Classes & Primitive Types

Problem 1. Multiple Choice (choose only one answer)

Lecture 17. For Array Class Shenanigans

Computer Components. Software{ User Programs. Operating System. Hardware

Please show all of your work.

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

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

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

Decaf Language Reference Manual

Classes. Integer Division. Thursday, October 23, March 13, 2008

CS260 Intro to Java & Android 03.Java Language Basics

CMSC 132: Object-Oriented Programming II

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

An introduction to Java II

Topic 7: Algebraic Data Types

CIS 120 Midterm II November 16, 2012 SOLUTIONS

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Classes as Blueprints: How to Define New Types of Objects

An Introduction to C++

Chapter 4 Defining Classes I

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

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

Java Bytecode (binary file)

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Transcription:

Naming conventions in Java The method signature Invoking methods All class names are capitalized Variable names and method names start with a lower case letter, but every word in the name after the first word starts with an upper case letter. Method names are followed by round brackets. Variable names are not. Constants are named in ALL_CAPS, using upper case letters and underscores in their names. Crab, World, Actor, MyWorld act(), move(10), atworldedge(), wormcount, score In the examples above, act, move and atworldedge are all methods, while wormcount and score are variables. EAST, BLACK, RED The method signature defines a method. It contains four parts: 1. The access level - usually public or private, but could be protected public void movearound() 2. The return type - int, boolean, String, public boolean atedge() double or void are the most common data public void turn( int angle ) types. 3. The name of the method. 4. The formal parameters. Invoking or calling a method involves using the method name and providing the required actual parameter values. If the method has a movearound(); void return type, then that method is a command and is if( atedge() ) { used as a separate statement in your code. If the method has a return type other than void (int, turn( 15 ); double, String or boolean) then the method is providing information which your code must use in some way.

How the world is measured Adding objects to the world Where is an object? The world is made up of square cells arranged to rows and columns. The world's dimensions are established in the constructor of the World subclass. Objects are added to the world by invoking the addobject method. This method has three parameters. the first is the object to be added to the world and the second and third parameters are the x and t coordinates of the location where the object is to be added to the world. The position of an object in the world is the position of the center of the object's image and is given by the methods getx (the column the object is in) and gety (the row the object is in). super( 800, 600, 1); int w = getwidth(); // w will be 800 int h = getheight(); // h will be 600 int w = getwidth(); // w will be 40 int h = getheight(); // h will be 30 // add a Crab object to the center of the world addobject( new Crab(), getwidth()/2, getheight()/2 );

There are two ways to make an object move: 1. The move method makes an object move in the direction it is facing. Making things move 2. The setlocation method Makes an object move in any direction, independent of where it is facing. The setlocation method ahs two parameters. the first one is the x- coordinate of the location you want the object to move to and the second parameter is the y-coordinate of the location you want the object to move to. You can make the object move left or right by subtracting or adding to its x- coordinate. You can make the object move up and down by subtracting or adding to its y-coordinate. // moves 10 cells in the direction it is facing move( 10 ); // moves to cell at column 200 and row 300 of the world setlocation( 200, 300 ); // moves the object 5 cells to the right setlocation( getx()+5, gety() );

Making things turn The Greenfoot classes To change that you can use the setrotation method or the turn method. 1. setrotation( int angle ) - sets the direction the object is facing to the angle parameter. 2. turn( int degree ) - turns the object from the direction it is currently facing by the number of degrees in its parameter. The Greenfoot class contains several useful methods for doing things in a Greenfoot scenario. These methods are all static methods which means they are invoked by using the class name and the method call in dot-notation. setrotation( 90 ); // face down turn( 15 ); Greenfoot.isKeyDown("left") Greenfoot.getRandomNumber(100) Greenfoot.stop();

Calling World methods from an Actor subclass. When the Java compiler seeing an method call in a line of your code, it looks for that method, first inside your class. If it's not there, it looks in each of your class' super classes, starting with the closest one in the class structure. If it can't find the method in any of these classes, you get an error message that says, for example, "Can not find symbol: method getwidth" In order to call methods that are in other classes you need to tell Java what class the method is in. If the method is a static method (see "The Greenfoot classes" above), you name the class, followed by a dot and the method call. If the method is non-static, then it belongs to an object, so you must reference the object, followed by a dot and the method call. So, to invoke a World method, you must get a reference to the world the object lives in using the getworld method. So, to call the getwidth method of the World class, from an Actor class, you'd use getworld().getwidth(). to invoke the same method inside the World class, you'd just use getwidth(). int x = getworld().getwidth()/2; int y = getworld().getheight()/2; getworld().addobject( new Ant(), x, y );

The if-statement The for-loop The if-statement allows your code to make true or false decisions and select different statements to execute based on those decisions. The for-loop is used to repeat a block of code more than once. The statement has three parts to it: 1. The initialization part declares an int variable and sets it to its initial value, usually 0. 2. The condition checks this variable against some condition. As long as the condition is true, the loop continues to repeat 3. The increment part, where the variable's value is changed, usually by 1. It is important to note that the for-loop automatically checks the condition before each iteration of the loop and changes the variable after every iteration of the loop. // adds a bug to the world 3% of the time int n = Greenfoot.getRandomNumber(100); if( n < 3 ) { // the bug is positioned randomly 25 cells from the top of // the world int x = Greenfoot.getRandomNumber(getWidth( )); int y = 25; addobject( new Bug(), x, y); move(); if( atworldedge() ) { turn(15); for(int r=0; r<15; r++) { // body of the loop The statements in the body of the loop will be repeated 15 times.

Collision Detection Detecting the edges of the word This method has one parameter, the class of objects you're looking for. It returns any object of that type whose image is intersecting the object's image. If no object is intersecting this object's image, the method returns a null pointer. You can use this fact to determine if an object is intersecting this object: An object is close to the top and left edges of the world when its x or y coordinate gets close to 0. It is close to the left or bottom edge when its x or y coordinate is close o the width or height of the world, respectively. Actor thing = getoneintersectingobject(thing.class); if( thing!= null ) { if( getx() < 5 ) { if( gety() < 5 ) { if( getx() > getworld().getwidth()-5 ) { if( gety() > getworld().getheight()-5 ) {