1: Course Introduction

What We Will Cover

1.1: Introduction to Programming Using Games and Simulations

Learner Outcomes

At the end of the lesson the student will be able to:

  • Set your goals for the course
  • View examples of what you will learn
  • Decide on interest in the course content

1.1.1: Game Programming and Simulations

Simulation: the imitation of the operation of a real-world process or system over time.

Video game: an electronic game that involves human interaction with a user interface to generate visual feedback on a video device.

  • How many people are interested in programming simulations?
  • How many people are interested in programming video games?
  • They are closely related:
    • Both model the behavior of something
    • Both animate the modeled behavior with graphics
    • Both apply music or sound effects to enhance the model
  • Since they are similar we use the more general term: scenario
  • We will learn how to produce both simulations and video games in this course
  • Along the way we will learn how to program using the Java language

Goal Setting Exercise

  • Take out a piece of paper and write the answers to the following questions:

What do you want to get from this course?

Why is it important to you?

1.1.2: Examples of What You Will Learn

  • Let us take a look at some examples of what you will learn in this course
  • First, we need to make sure we have a useable browser:
    • Firefox
    • Internet Explorer
    • Safari (Mac)
  • Chrome no longer supports NPAPI (technology required for Java applets)
  • Here are some scenarios we will work with:
    • Leaves and Wombats: introducing object-oriented programming
    • Bugs (): a first program to learn basic programming techniques
    • WBC (): how to make side-scrolling games
    • Piano (): making music with an on-screen piano
    • Newton's Lab (): studying interacting objects
    • Asteroids (): a classic game to study collision detection

Student Projects

More Information

1.1.3: Greenfoot and Programming

  • To make learning how to program simulations and video games we use a software framework named Greenfoot
  • A software framework is a set of classes (libraries) that implements the standard structure of a program
  • Greenfoot has built-in code to produce visualizations on a 2D grid
  • In addition, Greenfoot provides simple commands for playing sounds and interacting with users
  • Because Greenfoot does all the hard work, we can focus on the fun part: making simulations and games

Greenfoot and Java

  • Greenfoot was created in a programming language named Java
  • A programming language lets a programmer write a series of commands for a computer to follow
  • Java is a widely used language for games and other programming projects
  • By learning Java you will get a good start in modern programming techniques
  • Note that Greenfoot has software tools (programming aids) to help beginners learn Java more easily
  • We will use these tools to make your learning easier during the course

More Information (Optional)

Other Cabrillo Courses Useful in Game Development

  • ETECH 140: Introduces theories and techniques of 3D computer animation.

Check Yourself (hover to check answers)

  1. To help develop games and simulations, we will use a software framework named ________.
  2. To make learning Java easier, Greenfoot has ________ tools
  3. The programming language we will learn during the course is ________.
  4. True or false: Greenfoot is another word for Java.

1.1.4: Why Learn to Program?

  • In this course you will learn how to program
  • Why learn computer programming?
  • The following are a few good reasons

Computing Enhances Your Creativity

  • Computer Science gives you the power to be creative in the digital world
  • Why merely play a game when you can create the next awesome game?
  • Studying computers lets you bring into existence new worlds from your imagination
  • Even if you are not interested in games, learning to program is a powerful tool
  • Most major applications and devices, from cell phones to MS Excel, are programmable
  • By creating a custom program you can improve your life, or other people's lives, by putting better ideas into practice
  • Programming gives you the power to solve problems and eliminate repetitive, boring tasks

Computing Empowers You to do Good

  • Computers are a tool to help solve problems
  • With computers, you can connect technology to your community and make a world of difference, like:
  • David Patterson in a New York Times article said:
    "Computer scientists may have the best skills to fight cancer in the next decade"
  • Also, computers give us new ways to learn things, some of which you will experience in this course

Computing Opens Doors

  • There are about two billion personal computers in use (source: Reference.com, Worldometers)
  • In addition, there are many billions more computers embedded in everything from cell phones to car engines, iPods and video games
  • Because the number of computers is growing so fast, few careers as rewarding as computing
  • Computer science pays well with average starting salaries of $71,534 for new grads in 2015 vs. $52,569 for the average College Graduate (source: NACE preliminary 2016)
  • Computers are used in every industry, making computing an important skill in any career
  • In game development teams, the game programmer is the highest-paying job (source: Game Industry Career Guide)

Coding = Your New Superpower

More Information

Exercise 1.1: Introductions and Questions (5m)

Use the next 5 minutes to complete the following.

  1. Join a group of 3-4 people and introduce yourself to each other.
  2. Discuss which scenarios you personally found the most interesting and least interesting.
  3. As a group, determine the most interesting and least interesting scenarios for the entire group.
  4. Write the two scenarios on the board.
  5. Select one member from your group as a spokesperson to describe your reasons for selecting the scenarios to the class.

Note: You do not have to turn in the answers to this exercise. However, you are expected to submit answers to subsequent exercises.

1.2: Exploring the Course

Learner Outcomes

At the end of the lesson the student will be able to:

  • Navigate the course web site
  • Discuss how the course operates
  • Know where quests (assignments) are posted and when they are due

1.2.1: Course Design

  • The course is designed as a multiplayer game
  • Students are the players and the instructor is the Game Master
  • Course events will be divided among:
    • Fighting or finessing monsters (quizzes, exams etc.)
    • Completing quests (assignments, challenges)
    • Crafting (writing computer code for games and simulations)
  • Everyone in the class will choose and name their avatars
  • Everyone must register their avatars on the Greenfoot site
  • We use avatars when publishing games online, among other activities


  • The course is organized into three parts with a Boss (exam) at the end of each:
    1. Starting game: Learning basic programming skills while producing your first game.
    2. Middle game: Learning 1337 skillz to empower your games in a variety of prepared scenarios.
    3. End game: Learning how to apply 1337 skillz to your final project.
  • Students will be organized into guilds based on their 1337 skillz, game interests and other factors
  • Guilds will have between five and eight players
  • Each guild will choose a name agreed to by all guild members
  • Some quests will be completed solo, some in pairs and some in guilds


  • Each player (student) begins the first day of class as a Level 0 avatar
  • Levels are based on individual player experience points (XP) as shown in the Grading Policies of the Syllabus
  • Twenty is the highest level a player can achieve
  • Sources of XP include:
    • Labs: assignments to introduce techniques and practice crafting code
    • Quests: assignments to challenge and further refine coding skills
    • Boss events: quizzes and exams
    • Other: Easter eggs, arcane studies

Special Boss Basic (First Midterm) Opportunity

  • As part of collecting XP, students may play Jumbled Games
  • The game presents a number of challenges
  • After completing each challenge perfectly (no errors), student may participate in a Boss event quiz
  • Completing each Boss event is worth XP
  • Boss attempts may be repeated on a later day and questions are drawn randomly from question pools

More Information

1.2.2: How to be Successful in the Course

Even Einstein Had to Try

Don't be a scrub!

  • Being successful in a course is like becoming good at a game
  • It requires dedication to your game (course), and to becoming a master gamer (successful student)
  • As the gamer, you take responsibility for your own learning process
  • As the Game Master, I present ideas, examples and help when requested
  • As the gamer, you will need to practice to truly master your game
  • Avoid being a scrub and claiming everything is overpowered, broken or too hard
  • Instead, learn how to overcome the problems and become a winner despite problems

Improving Your Game

  1. Go to class and participate.

    Turn off your cell phones and pay attention to the lecture and participate in the activities. Do not become distracted or try to multitask. Ask questions in class. Establish good rapport with your classmates and the instructor. Get to know other students in the class during activities and breaks. Eighty percent of success is showing up.

  2. Keep up with the course.

    Complete all your homework on time. Expect assignments to take time to complete, so start playing with them long before they are due. Every class has a slippery slope, where falling behind causes you to fall even further behind. Falling behind makes the course material seem overwhelming.

  3. Practice

    Realize that just because this is a game programming class does not mean that it does not take practice to master the material.

  4. Start at the lowest level.

    Beat the prepared lab scenario first. Practice until it becomes too easy and then move to the next level. After finishing the basics you are ready to develop more creative games.

  5. Adapt your skills.

    Do not get frustrated if you do poorly at first. Figure out what you are doing wrong and fix it! If you find you are stuck, take a break and come back to it later, talk to the professor, or ask a classmate for help. The instructor wants you to succeed!

  6. Prepare for boss events.

    Always prepare for the major challenges. Keep on top of the material and have a game plan.

  7. Keep your interest.

    If you become bored then talk to the instructor for more challenging assignments. Do not be afraid to challenge yourself.

  8. Know your programming code.

    If you do not know the code forms you will get frustrated. There are many practice exercises to develop your coding skills

  9. Read the code of skilled game programmers.

    Greenfoot site has many games with source available. Look at how the programmers create each special effects. It may have taken years for them to figure out a particular technique, but you can get it quickly by reading their code. Try to adapt the code to your own game. Remember to give attribution to honor the original developer.

More Information

1.2.3: Useful Information and Course Resources

  • I provide you with a lot of information and resources for you to succeed in the course
  • Here are some of the highlights

Using the Course Website

  • www.edparrish.net has a link to the course web site and has contact information for the instructor
  • Syllabus describes the policies of the course including prerequisites and grading
  • Home Page describes the online content of the course
  • Canvas is where you turn in assignments, take tests and can look at your grades and written feedback on assignments
  • Schedule page has links to lecture notes, reading and assignments (events)
  • Read the assigned reading BEFORE class
  • Assignments are due before class
  • Date and time listed in Canvas takes precedence over the schedule page
  • Late homework is not accepted, except for a one-time exemption of up to two days
  • See Syllabus for more information on assignment grading policies

Using Classroom Computers

  • Login with a generic student login and password
    • Username: student
    • Password: Cabri11o (a.k.a. funny Cabrillo)
  • All contents of C:\ will be lost when you log out
  • The contents of D:\ are preserved
    • The D:\ drive is shared by everyone
    • Don't delete other people's stuff
    • Don't leave anything there that's precious!
  • You need your Cabrillo login and password to access Canvas, WiFi and other Cabrillo computers
  • See: How To Get Your Cabrillo Login and Password

Email and Texts

  • Please make sure your email address is correct in WebAdvisor
  • When sending me an email please state your name, course number and if you are in the day or evening section
    • I really do not know who you are from an email address like: fandango246@hotmail.com
  • Please make sure your emails are professional, that words are spelled correctly and that you use good grammar
  • You can setup Canvas to provide text (SMS) notifications

Online Lab Hours

  • The schedule of classes lists, "3 hr 5 min online lab per week".
  • These hours are in addition to the usual 2 hours preparation per unit (see Orientation FAQs)
  • You are graded, in part, on completing the online labs each week
  • These online lab hours are there to complete the lab exercises in CodeLab
  • You must sign up for and later purchase a CodeLab subscription
  • If you do not have access to a computer, you may use one in the Computer Technology Center (CTC) or STEM Center (Map)

Grading Options

  • On the first assignment, you determine your grading option: default, letter, credit/no-credit
  • To change your mind later in the course, up to the deadline, send me an email

Academic Honesty

  • Do not cheat, lie or help others to cheat
  • Its OK to get help and to help others on homework, within limits
  • However, do not share code with others or copy code from others without attribution
  • Copying any amount of code without attribution is plagiarism
  • See Scholastic Honesty Policy in Syllabus for more information

Cell Phones and Texting

  • Cell phones are wonderful devices but are disruptive in the classroom
  • Texting and cell phone use are prohibited during class

Classroom Environment

  • I ensure a safe learning environment for everybody
  • I will treat you with courtesy and respect
  • I expect the same from you
  • No sarcasm, put downs, intolerance, or demeaning remarks
  • Listen while others are speaking
  • No disruptive behaviors
  • See Student Behavior Expectations in the Syllabus for more information
  • If you need help ask; if you already know, help other people to learn.
  • It's okay to be wrong -- but you need the courage to find out

Other Useful Links

1.2.4: Using a Text Editor

  • In the following exercise you need to use a text editor
  • A text editor is a basic tool used by programmers like a word processor is used by writers
  • Programmers use a text editor for programming, rather than a word processor, because its easier:
    • A word processor saves extra characters to control formatting whereas a text editor saves text with minimal formatting
    • Not all word processor files are interchangeable but text files generally are interchangeable
    • Word processors tend to be slow to start and complex to use while text editors are fast and simple to use
    • Most word processors can save files as plain text but you have to take care to specify the plain text format
  • Some of the text editors you can use are:
  • We use TextPad in the classroom and you can download it free for evaluation at home
  • Since you need to learn how to use programming tools, we will use text editors in this course
  • Do not use word processing programs like Word, LibreOffice Writer or OpenOffice Writer for quests
  • To get you started, we will work together to use a text editor for the next exercise

Exercise 1.2: Treasure Hunt! (15m)

In this exercise we explore the course web site to see how many of our questions we can answer. We will break into teams of 3-4 people to find answers to the questions. The first team to find all the answers and to notify the instructor wins!

To find the answers, look at the section we just covered: Useful Information and Course Resources. We will discuss the questions and answers after the first team wins.


  1. With your team, find the answers to the following Exercise Questions.
  2. Prepare a text file (NOT a Word document) named: questions.txt with the following questions copied into the file.
  3. Record the answers to the questions in the text file.
  4. Add a comment to the top of the file that contains the name of the people on your team
  5. Save a copy of your file to upload to Canvas as part of quest 1. Do NOT email the answers to the instructor.

    Note: You must save your file somewhere besides a classroom computer. Your classroom computer will delete your file when you logout.

Exercise Questions

  1. What is your personal Canvas login (NOT your password)?
  2. What do you do if you are late to class?
  3. Where is the instructor's contact information located?
  4. How do you get to the instructor's office from the classroom?
  5. What is the Math class prerequisite for this course?
  6. What is the name of the textbook for this course?
  7. What is CodeLab and why must you spend $25 to purchase it?
  8. What software framework must you use for this course?
  9. How many XP do you need to get an "A" in the course?
  10. How many hours per week can you expect to spend on this course outside of the classroom?
  11. What is scholastic honesty?
  12. What is an example of acceptable help and an example of unacceptable help on quests?
  13. What is the Web address (URL) where all quests are listed?
  14. What items are due for the first quest (list them) and where do you submit them?
  15. How many late quests are accepted and how long do you have to complete a late quest?
  16. When are you allowed to use computers (even your own) in the classroom?
  17. Is cell phone use during class a disruptive behavior?
  18. What are the consequences of disruptive behavior?
  19. What food or drink is allowed in the classroom, if any?
  20. When emailing the instructor, what two pieces of information should you include at a minimum?

When finished, please verify your answers with one or more classmates.

1.3: Getting Started

Learner Outcomes

At the end of the lesson the student will be able to:

  • Install Greenfoot for use in this course.
  • Open and work with a scenario
  • Describe common Java building blocks: class, object, method
  • Call a method with any number of parameters
  • view and save source code for a class

1.3.1: Introducing Greenfoot

  • Greenfoot is a software framework that makes it easy to create 2D animations, simulations, and games
  • A software framework provides some generic functionality that we can change by writing code
  • To create games or simulations, we write Java programs using the Greenfoot IDE
  • Greenfoot provides both the tools for creating the programs and the libraries to make it easier to create scenarios
  • The following image shows an example of the main Greenfoot window

Example of Greenfoot Main Window

Greenfoot opening screen

Greenfoot Resources

1.3.2: Setting Up Your Programming Environment

  • Most of your homework quests will ask you to craft a computer program
  • To craft the programs, you will need to use a computer running the following software:
    • Java development kit
    • Greenfoot framework
  • Also, the textbook provides some scenarios for learning how to program while developing games, applications, music and simulations
  • All the software we need runs on almost any computer
  • Greenfoot | Download has a link to the installation instructions
  • If you do not have a computer at home (or work) to use for quests, then you can use the CTC and STEM Center
  • To use the school computers, you will need a login and password
  • Everyone enrolled in the course can get a login and password

Installing the Software

  1. Install Greenfoot with the JDK by downloading Greenfoot from: Download Greenfoot page.
  2. Install the Book scenarios from the link: Introduction to Programming with Greenfoot. You will receive a file named: book-scenarios.zip. This is a compressed file that you must extract. On Windows, right-click and select Extract All from the menu. On Mac and Linux systems, you can double-click the file to extract it. Remember where you extracted the files so you can use them.

More Information

1.3.3: Working with a Scenario

  • In this section we start Greenfoot and open a scenario
  • Then we place objects in the scenario
  • For the first scenario we will use a wombat, which is an Australian animal that eats leaves and other plant matter

Wombat Scenario

Wombat scenario

Starting Greenfoot and Opening a Scenario

  1. To start, click on the Greenfoot icon:

    Greenfoot icon

  2. Click on Scenario in the menu
  3. Choose Open
  4. Find the leaves-and-wombats scenario for chapter 1
  5. Click the Open button in the Open Scenario dialog
  6. The scenario starts by showing an empty wombat scenario as shown above

Adding Objects to the World

  1. Right click on Wombat in the class display to see a pop-up menu
  2. Create a Wombat by selecting: new Wombat()

    Wombat menu

  3. A picture of a Wombat will appear attached to the cursor

    Wombat dragged

  4. Move the cursor to a cell and left-click to place the Wombat object

    Wombats in the world

  • Shift-click to place more than one object
  • Adding leaves to the world is similar:
    1. Right-click the Leaf class and select: new Leaf()
    2. Move the leaf to the world and click to place the object

Exercise 1.3a: Exploring Greenfoot

Part of learning to program game and simulations is exploring the software environment. In this exercise, we explore how to use Greenfoot. Work with one other student to complete this exercise.

  1. Phase 1: Experiment by trying the steps in Specifications below. (5m)
  2. Phase 2: Answer questions that are in the Specifications. (6m)
  3. Phase 3: Review answers with another student. (3m)


  1. Start Greenfoot and open the leaves-and-wombats scenario for chapter 1 of the book, which is available here if it does open automatically when starting Greenfoot.
  2. In addition, open a text editor, create a file named wombat.txt and record answers to all the questions highlighted during this exercise.
  3. Add some wombats and leaves to the world by right-clicking the class and selecting new...
  4. Invoke the move() method on a wombat and observe what it does.

    Moving the wombat

    Try the move() method several times until you understand how the method causes the wombat to act.

    Q1: What happens when calling the move() method if a wombat is in the middle of the world?

    Q2: What happens when a wombat reaches the edge of the world while running?

    Q3: Do wombats eat leaves when they move over them?

  5. Invoke the turnLeft() method several times until you understand what it does.

    Q4: What does the turnLeft() method do?

  6. Place two wombats in your world and make them face each other.
  7. Examine the methods available on the object menu for a wombat and find the methods that cause it to eat leaves.

    Q5: What is the name of the method that causes a wombat to eat leaves?

    Notice the method names have a word in front of them like: void, boolean and int. These words are the return type of the method. For more information see section: 1.3.5: Methods and Return Types.

  8. Invoke the canMove() method on a wombat, which should show a dialog box like the following:

    Testing if a wombat can move

    Notice the word boolean highlighted above. A boolean return type means the method can return either true or false.
  9. Try invoking the canMove() method on a wombat placed a various locations in the world.

    Q6: Under what conditions does the canMove() method return false?

  10. Invoke the setDirection() method, type in a number and press the OK button.

    Notice the words inside the parenthesis. These words make up a parameter for the setDirection() method. A parameter is extra data we provide for this method to run. For more information see section: 1.3.6: Parameters.

    Q7: What happens if you enter a non-integer number, like 2.5?

    Q8: Does anything happen if you enter a number larger than 3 or smaller that 0? If so, what happens?

  11. Right click on Wombat in the class display and select the Open editor menu item.

    You should see a source code listing of the Java commands for a Wombat in a new Window. For more information see section: 1.3.7: Viewing and Saving Source Code.

  12. Remember to use a text editor to create a file named wombat.txt and answer record answers to all the questions highlighted during this exercise.
  13. Review your answers with another student in the class and correct any mistakes.
  14. Add a comment to the top of the file that contains the name of the person with whom you reviewed the questions, like:
    Reviewed with Jane Programmer
  15. Save your wombat.txt file to upload to Canvas as part of quest 1.

    Note: You must save your file somewhere besides a classroom computer. Your classroom computer will delete your file when you logout.

1.3.4: Greenfoot Objects and Class Diagrams

  • Programming in Greenfoot and Java is based on objects.

    Object: a specific entity or thing.

  • Scenarios in Greenfoot are composed of software objects.

    Software object: a collection of software elements that models an entity or thing.

  • From the definition, we see that programming in Greenfoot and Java is essentially modeling the real world in software
  • There are two general types of objects in Greenfoot: Worlds and Actors
    • A World holds actors: a "stage" for actors
    • An Actor can act: move, turn, etc.
  • To make an object, we right-click on a class such as Wombat

    Wombat menu

  • Notice the structure of the command used to create a new object
    new ObjectName()
  • The word "new" means we are asking Greenfoot to make a new object
  • When we make an object, Greenfoot executes program code defined in a class

    Class: the general concept (type) of an object.

  • The class contains the code needed for the computer to create and model the object
  • To make creating scenarios easier, Greenfoot provides code in the World and Actor classes
  • To make use of the code, we create subclasses of World or Actor

    Subclass: a class that inherits software elements from a superclass

  • The word inherits means that a subclass gets to use the code defined in the superclass
  • For example, WombatWorld is a subclass of World and thus inherits (gets) many software elements from World

    Class diagram

  • Both Wombat and Leaf are subclasses of Actor and perform in the WombatWorld
  • The collection of classes for a scenario like the one shown above is called a class diagram

Class Relationships

  • Notice the arrow (arrow for is-a realtionships) in the class diagram
  • The arrow indicates a relationship between the classes
  • For example, the World class is always present is Greenfoot
  • However, we cannot use the World class directly
  • Instead, we must create a specialized subclass of World, in this case WombatWorld
  • WormbatWorld is a World, in the sense of Greenfoot worlds
  • This type of connection between classes is called an is-a relationship
  • In the same way, we say that Wombat is an Actor
  • Is Leaf an Actor? answer
  • Any subclass has an is-a relationship with its superclass

Instances of Classes

  • Objects in programs are called "instances" (occurrences) of a class
  • When we add leaves to the world we are creating instances of the Leaf class
  • When we add a Wombat to the world, we are creating an instance of the Wombat class
  • The class provides the programming code that defines how the instance (object) appears and operates

Check Yourself (hover to check answers)

  1. A real-world object is a(n) ________, while an object in a program is a(n) ________ of a class.
  2. True or false: a class is the type of an object and contains code that defines how an object appears and operates.
  3. A subclass ________ software elements from the superclass.
  4. Wombat and Leaf are subclasses of ________.
  5. The relationship arrow (arrow for is-a realtionships) always points toward the ________.

1.3.5: Methods and Return Types

  • Once we have added an object to a world, we can access its methods

    Moving the wombat

  • To interact with an object, we invoke (or call) its methods
  • The diagram above shows several methods for the wombat object
  • One of the methods is named move:
    void move()

Return Types

  • Notice that the method name ("move") has the word void in front of it
  • The word in front of a method name tells us what type of data comes back from the method call
  • The word void, which means "nothing", tells us that no data returns from the method
  • On the other hand, the method getLeavesEaten() returns an int (which is short for integer)
  • Thus the type of data that comes back from the getLeavesEaten() method is an integer (whole number)
  • When we invoke the method, we get a dialog box showing the number of leaves eaten

    A method result

  • The number of leaves eaten is the data that returns from the method call

Check Yourself

  1. True or false: left-click on an object to access it's methods.
  2. To interact with an object, you ________ a method.
  3. The return type of a method specifies the ________ of data returned when the method is called.
  4. The return type void means ________ is returned from the method call.
  5. The return type int means ________ are returned from the method call.

1.3.6: Parameters

  • Notice the setDirection() method of a Wombat object
    void setDirection(int direction)
  • The words inside the parenthesis are for a parameter
  • A parameter is a way to send data to the method that it needs to run
  • The word int (which is short for integer) is the type of the parameter
  • The word direction is a name that gives us a hint what the parameter is used for
  • Every parameter has at least two words like this: int direction
  • When we invoke the setDirection() method, it needs a single parameter value
  • Thus, Greenfoot will prompt us for the parameter with a dialog box like:

    Input dialog for method parameter

  • To invoke the method, we must enter the information it needs to run

Parameter Lists

  • Note that every method has a parameter list, which is listed inside the parenthesis
  • Even methods that accept no parameters have an empty parameter list
  • Thus, we always see parenthesis when we see a method

Method Signatures

  • Notice that every method has a name, return type and parameter list

    Parts of a method

  • The first part is the return type, which specifies what the method returns
  • The name is in the middle and is chosen by the programmer as a hint on the action the method performs
  • The last part is the parameter list
  • Together they make up the method signature

Check Your Understanding

  1. The names of the parts of the method signature void setDirection(int direction) are:
    1. return type: ________
    2. method name: ________
    3. parameter list: ________
  2. True or false: a parameter list may be empty, meaning there is no code between the parenthesis.
  3. True or false: parameters always have a type, unless the list is empty.
  4. Using a method is known as ________ the method.
  5. To use methods that have a parameter, you must supply the necessary ________ when calling the method.

1.3.7: Viewing and Saving Source Code

  • The commands we use to tell a program what to do are know as source code
  • The commands are written in a programming language -- in our case Java
  • We can look at and change the source code of the classes in a scenario
  • The behavior of an object is defined by the methods of its class
  • For instance, to make a Wombat object move, we invoke its move() method
  • The way we specify the behavior in a method is by writing source code
  • The source code of a class specifies all the details about the class and its objects
  • Selecting Open editor from the class's menu will display the editor window

    Opening an editor

  • Another way to open the source code editor is to double-click the class box.
  • Once we open the editor, we will see a listing of all the Java source code for the class like that shown below
  • You do not need to understand all the source code at this point
  • We will be spending a lot of time learning how to write source code over the rest of the course
  • The important idea to understand now is that you change an object by changing its source code

Copying or Saving Source Code

  • You will need to turn in source code for some quests
  • One technique is to copy the source code from the editor into a text editor such as TextPad
    • Ctrl-A inside the Greenfoot editor to copy to the clipboard
    • Ctrl-V inside the text editor to paste from the clipboard
  • Another technique is to locate the source code file in the director where you save your scenarios
  • The source code is contained in plain text files that end in .java

Compiling Souce Code

  • Computers do not understand source code directly
  • For computers to understand source code, we must translate the code to machine language
  • Machine language (also machine code) can be executed (carried out) by the computer
  • To translate to machine language, we use a compiler
  • The Greenfoot IDE automatically tries to compile source code
  • However, sometimes we need to press the Compile button to start the process

Example Source Code in the Greenfoot Editor

Wombat source code

Check Yourself

  1. Written commands to a computer program are known as ________ code.
  2. True or false: the only way to write or save code is to use Greenfoot.
  3. The programming language we use for writing scenarios in Greenfoot is named ________.

1.3.8: Zipping (Compressing) Files

  • The final task for our first project is to zip the files
  • Zipping a file does two important functions:
    • Compresses a files so it takes less space
    • Maintains the folder structure of the zipped file
  • A zipped file is easier to store and turn in to Canvas
  • Most homework quests require zipping files to maintain the folder structure
  • The first step to zip a file or folder is to find the file or folder on our computer
  • Then we follow the instructions below for our type of computer
  • For example, the first lab describes the required folders here: Lab 1

Instructions for Zipping (Compressing) Files

Exercise 1.3b: Exploring Code (10m)

Part of learning to program game and simulations is learning how to use programming code. In this exercise, we explore how to read the Java code in a Greenfoot scenario.


  1. Start Greenfoot and open the leaves-and-wombats scenario for chapter 1 of the book as we did in the last exercise.
  2. In addition, open the text file named wombat.txt from the last exercise in a text editor.

    You will record answers to this exercise in the same text file as the last exercise.

  3. At the end of the wombat.txt file, add a heading that says, "Method headers".

    You will add the method headers from the following steps after this heading.

  4. Open the editor for the Wombat class.

    Opening an editor

  5. Within the source code editor, find the method that causes a wombat object to move and copy the method signature (first line) and then paste it into the wombat.txt file.

    Moving the wombat

    As an example: here is the start of the listing in the README.TXT file:

    Method signatures
    public void move()
    ... (more here in next steps)
  6. Similarly, find the method that causes a wombat to turn left and copy the method signature into the wombat.txt file.
  7. Find the method that causes a wombat to eat leaves and copy the method signature into the wombat.txt file.
  8. Finally, find the method that returns true if a wombat can move and false otherwise. Copy the method signature into the wombat.txt file.
  9. Save your updated wombat.txt file to upload to Canvas as part of Lab 1.

    Note: You must save your file somewhere besides a classroom computer. Your classroom computer will delete your file when you logout.

  10. Zip a copy of the "leaves-and-wombats" scenario to ensure you know how to zip files, which you will need to do for all quests.

    For more information see section: 1.3.8: Zipping (Compressing) Files.

As time permits, read the following sections and be prepared to answer the Check Yourself questions in the section: 1.3.10: Summary.

1.3.9: Review

  • To make developing games and simulations easy, we will use a software framework called Greenfoot
  • Greenfoot uses the Java programming language
  • Thus to set up your programming environment you need to install both Java and Greenfoot
  • Also, you need to download the book scenarios as we will use these scenarios as examples during the course
  • Programming in Greenfoot and Java is based on objects.

    Object: a specific entity or thing.

  • Scenarios in Greenfoot are composed of software objects.

    Software object: a collection of software elements that model an entity or thing.

  • When we make an object, we use program code defined in a class

    Class: the general concept (type) of an object.

  • To work with Greenfoot, you open a scenario and place objects in a "world"
  • Technically, Greenfoot has two types of objects: Worlds and Actors
    • A World holds actors: a "stage" for actors
    • An Actor can act: move, turn, etc.
  • To make a game or simulation, we create subclasses of World or Actor

    Subclass: a specialization of another class

  • For example, WombatWorld is a subclass of World that is specialized for Wombats

    Class diagram

  • Both Wombat and Leaf are subclasses of Actor and perform in the WombatWorld
  • When one class is a subclass of another class, we say they have an is-a relationship
  • Once objects are placed in the world, you can interact with them by right-clicking and invoking a method

    Moving the wombat

  • Every method has a name, return type and parameter list

    Parts of a method

  • The first part is the return type, which specifies what the method returns
    • If the method returns nothing, we use the word void.
    • If the method has a boolean return type, it can return either true or false.
  • The name is in the middle and is chosen by the programmer as a hint on the action the method performs
  • The last part is the parameter list, which is data that the method needs to run
  • Each parameter has a data type and a name
  • If no parameters are needed, then the parenthesis are empty
  • We also discussed how to view and save source code
  • Assignments will need to be zipped before turning them in to Canvas to maintain their folder structure

Check Yourself

Answer these questions to check your understanding. You can find more information by following the links after the question.

  1. True or false: Greenfoot is a software framework. (1.3.1)
  2. True or false: to use Greenfoot, you will need to install Java. (1.3.2)
  3. What is the name of the command, shown while right-clicking, to use when creating a wombat object? (1.3.3)
  4. What is an object? (1.3.4)
  5. What is a class? (1.3.4)
  6. What is a subclass? (1.3.4)
  7. True or false: many objects can be created from a single class. (1.3.4)
  8. True or false: many classes can be created from a single object. (1.3.4)
  9. True or false: The arrow arrow for is-a realtionships in the class diagram always points toward the superclass. (1.3.4)
  10. True or false: for an object to perform an action, you call (invoke) a method. (1.3.5)
  11. True or false: every method has a return type. (1.3.5)
  12. True or false: the return type of a method specifies what a method call will accept. (1.3.5)
  13. True or false: every method has a parameter list, though some lists may be empty. (1.3.6)
  14. What are two ways to view the source code for a class? (1.3.7)
  15. What is one way can you copy or save the source code for a class? (1.3.7)
  16. Java source code files end in the extension ._____. (1.3.7)
  17. Zip files end in the extension ._____. (1.3.8)

Labs and Quests

Due Next:
Lab 1: First Tutorial (1/31/17)
Q1: Getting Started (2/2/17)
  • When class is over, please shut down your computer
  • You may complete unfinished exercises at the end of the class or at any time before the next class.
Home | Canvas | Schedule | Syllabus | Room Policies
Help | FAQ's | HowTo's | Links
Last Updated: January 29 2017 @19:37:19