(PUDG) Exploring Classes and the Code Editor Lesson

Exploring Classes and the Code Editor

class is a blueprint or template for creating objects. It defines the variables and the methods common to all objects of a certain kind. The Greenfoot API has 7 pre-defined classes.  

Actor

Greenfoot

GreenfootImage

GreenfootSound

MouseInfo

UserInfo

World

Classes specify how to create an object from that class and define the methods and properties available to the object.    

An object represents a model of a real-world object. When an object is created from the class it is called an instance of the class. It has attributes that are stored in variables and behaviors or actions called methods which are things the object can do. 

For example, a Dog class would create dog objects. The class Dog defines basic attributes and behaviors common to all dogs. A Dog has a color, height, weight and name. This is information a Dog object would know about itself and can be stored in variables. A Dog has basic behaviors or actions it can do such as bark, eat, growl, run. These are actions a Dog object could do and are defined in methods.

A class is designed with the following elements:

  1. Class Name
  2. Field Data Information:  variables used by the class
  3. Constructor: method that creates the object but does not return anything
  4. Methods of the class - two types
  • void: actions performed
  • return methods: information it tells or returns  

Constructor

A constructor specifies how the object can be created. The World has two constructors. You must use one of the constructors to create the world.

Constructor Summary
Constructors
Constructor and Description
World(int worldwidth, int worldHeight, int cellSize) Construct a new world.
World(int worldWidth, int worldHeight, int cellSize, boolean bounded) Construct a new world.

World(int worldWidth, int worldHeight, int cellSize)

World(int worldWidth, int worldHeight, int cellSize, boolean bounded)

The constructor from FlyingBirdWorld uses the same constructor for its parent World.

Constructor for objects
public FlyingBirdWorld()
// Create a new work super (600, 400, 1);
Syntax for Constructor Visibility is public Class name with parameter()
with 6 
The world's height, width and resolution are defined in the World superclass of FlyingBirdWorld. To pass these values to the World superclass, use super().

The word super means refer to the parent's constructor.  super can be used to call the constructor, methods and properties of the parent class.

super(600, 400, 1) tells the program to go the parent class World and use the constructor World(int worldWidth, int worldHeight, it cellSize) and assign the values of 600, height of 400 and cellSize of 1.  

super is a keyword used in the sub class when you want to invoke a method from the parent class that you are creating in the sub class.

Creating Objects

Before you can add an object to the world, it must be created.

Objects in java are created with the keyword new.

If I wanted to create a new Bird1 object, the syntax would be:

Bird1 b = new Bird1();  b is the name of the object.

Adding Objects to Your World

After an object has been created it can be added to the world.

addObject(Actor object, int x, int y) is a method in the World class. It is used to add objects to the world. It is void because it performs an action.

code block:
void
addObject (Actor object, int x, int y) Add an Actor to the world.

To add the Bird1 object b to the world you would type:

addObject(b, 40, 200);   The object is b, the x position is 40 and the y position is 200.

Creating Methods in Java

There are two basic method types:  void methods and value-returning methods.

All methods require a parameter, ( ). However, methods don't need to have values passed to them. You can just execute some code.

A  Java method is a collection of statements that are grouped together to perform an operation. More generally, method declarations have five components, in order:

  1. Modifiers —such as public, private, and others you will learn about later.
  2. The return type —the data type of the value returned by the method, or void if the method does not return a value.
  3. The method name   —By convention, should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized.
  4. The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. The method body - enclosed between braces; this is where the method's code goes.

In the flyingbirdWorld create a method called:  public void populate()  

This method will create a Bird object and use the addObject method to add it to the world.

code block:
public void populate()
{
Bird1 b = new Bird1 ();
addObject (b, 40, 200);
}

Now add the populate () method to the constructor. When the flyingbirdWorld is created it calls the constructor.  The constructor has two lines of code.

code block:
public FlyingBirdWorld(),
// Create a new world with 600x400 super (600, 400, 1); populate();
super(600, 400, 1);
This calls the constructor from the parent and assigns creates the world.
populate();
This calls the populate method in the class and adds the objects to the world.

Exploring the Code Editor

More Java Syntax

  • All classes begin and end with a curly brace.
  • All methods begin and end with a curly brace.
  • All statements end with a semicolon.
  • All methods must have a ( ) parameter at the end of the name. Sometimes this will be empty, whereas other times, it will include parameters inside."
  • All classes begin with an upper case letter and use camel case.  
  • All variables begin with a lowercase letter and use camel case.
  • According to Java convention all variables should begin with a lower case letter.      
  • The only symbols you can use in a variable name are: letters, numbers, and "_" or "$".
  • You can start a variable with a _ (underscore) and $ (dollar sign) but it is discouraged by Java.  
  • Never use a number to start a variable.

Game Development Project

After reviewing the video and the materials in Exploring Classes and the Code Editor module, you will make additions to the flyingbirdWorld class. Use the source code editor to create objects in the flyingbirdWorld. After the object is created, use the addObject method to add them to the world. You will assign the appropriate image to the Class.

Do the following in the flyingbirdWorld.

  1. Create a populate method in the flyingbirdWorld.      
  2. Create an object from each of the classes inside the populate method.
  •                 Top_Pipe, Bottom_Pipe and Ground. Select the correct image for each object.
  1. Use the addObject () method to add the objects to the world in an x and y location. Choose an appropriate location in the world.
  2. Add the populate method to the constructor.  
  3. Compile the class. Your world should display the objects.

Now Create a video of your project. Include the following in your video.

  1. Begin with the flyingbirdWorld source code editor open.  
  2. Show the populate method from the class and explain what the method does and the code in the method.
  3. Explain the process of how to create an object from a class. (use correct terminology)
  4. Show the addObject method added to the class and explain how the code works.
  5. Show the Constructor for the class.  Explain what it does and what super means.
  6. Add the populate method to the constructor. Explain why you did this.
  7. Compile the source code.
  8. Show the flyingbirdWorld with the objects added to the class.

Use correct terminology as you explain the process. (new, parameter, method, constructor, etc.). The video should have a logical order and sequence as well as evidence of an understanding of constructors, classes, objects, parameters and methods.

You may want to type the information as a script and rehearse before you create the video.

[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION