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
  • 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 (): White Blood Cell to study how to make side-scrolling games
    • Cannon Demo (): working with gravity and collisions
    • Piano Visualizer (): visualizing music with an on-screen piano to study graphics
    • Asteroids (): a classic game to study collision detection
  • Greenfoot switched to a new online system recently and many older scenarios do not work

Student Projects

  • During the semester we will develop several scenarios
  • These projects will give us a chance to learn the material and use our creativity to express ourselves
  • Also, we will learn how to show your work within a larger community on the Greenfoot site
  • Here are some projects by Cabrillo students from recent semesters (alphabetical order by project name):
  • To see what other students have posted, visit Greenfoot's Cabrillo Projects
    • Note that Java Applets are no longer supported by most browsers
    • Thus many older projects only work in IE or Safari
    • Newer projects are converted to JavaScript to run in a browser
  • Also, here are examples of what others have done:
  • During the course we will learn how to program so we too can develop scenarios like these
  • Which scenario looked the most interesting to you?

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 pre-written code that implements some standard structure for a type of program
  • For example, 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
  • Greenfoot also supplies software tools such as a code editor and debugger
  • 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)

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

Image

Computing Enhances Your Creativity

Image

Computing Empowers You to do Good

Image

Computing Opens Doors

  • There are about two billion personal computers in use (source: Quora, Worldometers)
  • In addition, there are many billions more computers embedded in everything from cell phones to car engines, iPods and video games
  • New types of computers continue being developed such as DNA computers and quantum computers
  • Improvements in computing such as artificial intelligence are now considered requirements for business (source: Forbes)
  • Because the number of computers and computer technologies are growing so fast, few careers as rewarding as computing
  • Computer science pays well with average starting salaries of $72,677 for new grads in 2017 vs. $50,516 for the average College Graduate (source: NACE Class of 2017)
  • 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)
  • What majors are students in this class pursuing?

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

Organization

  • 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 organize into guilds based on their 1337 skillz, game interests and other factors
  • Guilds will have between four and five players
  • Each guild will choose a name agreed to by all guild members
  • Some quests will be completed solo and some in guilds

Grading

  • 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
    • Challenges: in-class quizzes to test skillz
    • Boss events: midterm and final exams to prove skillz
    • Other: Easter eggs, arcane studies

More Information

1.2.2: How to be Successful in the Course

Even Einstein Had to Try

We can improve!

  • 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 claiming everything is overpowered, broken or too hard
  • Instead, learn how to overcome problems and become a winner
  • Realize that by study and practice you can grow your brain and improve

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 and keeping 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. Nothing is more effective than practice. You've gotta do the work. Schedule a study session within a day of a lecture to review your notes, study the material and read the textbook.

  4. Start at the lowest level and build.

    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 code and game techniques.

  5. Adapt your skills.

    Do not get frustrated if you do poorly at first. Figure out what you are doing wrong and fix it! Everyone gets stuck at times. 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. With tenacity and effort you can improve your game and succeed!

  6. Prepare for boss events.

    Always prepare for the major challenges. Keep on top of the material and have a game plan. Prepare practice problems and test yourself.

  7. Keep your interest.

    If you become bored then talk to the instructor for more challenging assignments. If you want to know how to accomplish a particular task in a game, look it up or ask the professor. Look forward to challenges!

  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. Keep up and keep learning!

  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

  • Logins for Canvas, Hawknet and most computers on Campus are the same as your WebAdvisor login
  • However, logins for this classroom are different than for other 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!

Electronic Communication

  • Please make sure your email address in WebAdvisor is one you actually use
  • Check your email at least once per day or you will miss important course information
  • When sending me an email please include your name and course number
    • I really do not know who you are from an email address like: fandango246@hotmail.com
  • Set up Canvas notifications so you get Announcements, Grading and Discussion Post notifications via email or text (SMS)

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 "homework")
  • 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
  • CodeLab gives you practice coding with immediate feedback
  • If you do not have access to a computer, use one in the Computer Technology Center (CTC) or STEM Center (Map)

Grading Options

  • By default, everyone gets a letter grade
  • If you want pass/no-pass option, new Cabrillo policy is to fill out an eForm
  • With pass/no-pass, you must complete the course work at a satisfactory ("C") or better level to pass
  • Once you choose pass/no-pass and submit the form, the choice is not reversible
  • The last day to request pass/no-pass is shown on the A&R Dates and Deadlines

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
  • When helping someone, do not to just give them the answer but show them how to find or figure out the answer
  • When you help someone, do not touch their keyboard as this immediately stops their learning
  • When someone helps you, do not let them touch your keyboard
  • 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 other 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 we use are:
  • 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 discuss the questions and answers after you finish answering.

Specifications

  1. Find the answers to the following Exercise Questions.

    Hint: not everything is on this page. You will need to follow links to other pages of the course web site. See lesson 1.2.3: Useful Information and Course Resources for a list of possible links.

  2. Prepare a text file (NOT a Word document) named: questions.txt
  3. Copy the following Exercise Questions into the text file and then record the answer after each question.
  4. When finished, have another student verify your answers. See Assessing the Treasure below.
  5. At the top of the file, list the name of person the who verified your answers are correct.
  6. After verification, save your file to submit as part of assignment 1. Do NOT email the answers to the instructor.

    Remember that you must save your file somewhere besides a classroom computer. Your classroom computer will delete your file when you logout.

Exercise Questions

  1. What do you do if you are late to class?
  2. Where is the instructor's contact information located?
  3. How do you get to the instructor's office from the classroom?
  4. What is the name of the textbook for this course?
  5. What is CodeLab and why must you spend $25 to purchase it?
  6. What software framework must you use for this course?
  7. What are three free support services offered to students (list them)?
  8. How many hours per week can you expect to spend on this course outside of the classroom?
  9. What is scholastic honesty?
  10. What is an example of acceptable help and an example of unacceptable help on quests?
  11. What is the Web address (URL) listing all quests and labs?
  12. What items are due for the quest assignment (list them)?
  13. When is the first quest due and how do you turn it in?
  14. How many late quests are accepted and how long do you have to complete a late quest?
  15. When are you allowed to use computers (even your own) in the classroom?
  16. Is cell phone use during class a disruptive behavior?
  17. What are the consequences of disruptive behavior?
  18. What food or drink is allowed in the classroom, if any?
  19. When emailing the instructor, what two pieces of information should you include at a minimum?
  20. What is the last day to request pass/no-pass grading and how to you request it?

Assessing the Treasure (5m)

  • Display your answers on your computer screen.
  • Find another student to review your answers while you review their answers.
  • Review the answers and discuss any errors you found.
  • Put your name at the top of the Treasure Hunt file of the student you are reviewing as follows:
    Reviewed by: Your name
    

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 in Greenfoot

1.3.1: How a Computer Works

  • The computer is often called an electronic brain or referred to as intelligent
  • However, a computer is really just a machine that stores data and follows instructions
  • Because a computer is a machine, it can be powerful but is stupid: Click to show message
  • A computer is powerful because it flawlessly executes (performs) billions of instructions per second
  • A computer is stupid because it only mechanically follows simple instructions
  • Unlike humans, a computer has no innate understanding of what it is doing

Parts of a Computer

  • Computers have two main sections: hardware and software
  • Hardware is all the physical components like chips, circuits and wires
  • Software is the sets of instructions that tells a computer how to do something

(Take notes during in-class videos)

How Computer Programming Works

  • A computer is a machine the simply follows instructions like:
    instruction 1: Get number x from memory location Y
    instruction 2: x + 1
    instruction 3: x < 0?
    instruction 4: Store x in memory location Z
    instruction 5: ...
    ...
    
  • These instructions are entered, stored, processed and output all as binary numbers
  • Instead of working with binary, programmers work with higher-level commands
  • Each high-level command may then execute hundreds or thousands of machine level commands

Check Yourself

  1. True or false: one thing that makes computers powerful is they can execute a large number of instructions in a fraction of a second.
  2. Computer hardware includes ________.
    1. applications (apps)
    2. circuits
    3. games
    4. web pages
  3. Computer software includes ________.
    1. chips
    2. circuits
    3. games
    4. wires
  4. CPU instructions are performed ________.
    1. algebraically
    2. left to right
    3. randomly
    4. sequentially
  5. True or false: Program instructions are stored in binary but programmers write code with English-like commands.

More Information

1.3.2: Programming Games and Simulations

  • To develop our games and simulation, we need to provide detailed instructions to a computer
  • The high-level programming language we will use is called Java
  • Java is a general-purpose computer programming language that can produce programs that run on any computer
  • Writing code in Java is easier than machine code, but games and simulations are complicated programs
  • To make creating games easier, we sill use a software framework named Greenfoot

Introducing Greenfoot

  • Greenfoot is a software framework that makes it easy to create 2D animations, sounds, games, and simulations
  • 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

Check Yourself (hover to check answers)

  1. The programming language used in this course is ________.
    1. C++
    2. Greenfoot
    3. Java
    4. JavaScript
  2. True or false: programming games and simulations can be complicated.
  3. The reason for using the Greenfoot software framework is to make it easier to program ________.
    1. animations
    2. games and simulations
    3. sounds
    4. all of these

1.3.3: Setting Up Your Programming Environment

  • Most of your homework quests will ask you to craft a computer program
  • The textbook provides some scenarios for learning how to program while developing animations, games, simulations and playing sounds
  • To craft the programs, you will need to use a computer running the following software:
    • Java development kit (JDK)
    • Greenfoot software framework
  • All the software we need runs on most computers
  • Greenfoot | Download has a link to the installation instructions
  • If you do not have a computer at home (or work) to use for quests, you may 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. Also 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.4: 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 our character is a wombat
  • A Wombat 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)

Specifications

  1. Download the leaves-and-wombats scenario gfar file and double click the gfar file to start Greenfoot. (zip as backup)
  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 (right-click on a wombat and select) the move() method on a wombat 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: When the Run button is pressed, what happens when a wombat reaches the edge of the world?

    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. We will cover this in more detail in section 1.3.6: 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. We will cover this in more detail in section 1.3.7: 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. We will cover this in more detail in section 1.3.8: 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.5: 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 definitions, we see that programming in Greenfoot and Java is modeling the real world in software
  • In the real world we may have an object like a dog that has certain properties like four legs and furry
  • In Java, we would model a dog by describing the number of legs and if it has fur

Making Objects

  • 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

  • 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.
  • Notice the arrow (arrow for is-a realtionships) in the class diagram above
  • 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. The best example of a real-world object is ________.
    1. a craving for cookies
    2. a recipe for making cookies
    3. a thing like a cookie
    4. the size of a cookie
  2. A real-world object is a(n) ________, while an object in a program is a(n) ________ of a class.
  3. The key word that tells us we are making of an object from a class in Java is ________.
    1. class
    2. inherits
    3. new
    4. subclass
  4. True or false: a class is the type of an object and contains code that defines how an object appears and operates.
  5. Software elements are passed from a superclass to a subclass using ________.
    1. genetics
    2. inheritance
    3. parents
    4. supertypes
  6. The relationship arrow (arrow for is-a realtionships) always points toward the ________.
    1. Actor class
    2. World class
    3. subclass
    4. superclass
  7. Wombat and Leaf are subclasses of ________.

1.3.6: 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. True or false: right-clicking an object and calling methods with non-void return types, like int or boolean, causes a window to popup and display the return value.
  6. The return type int means ________ are returned from the method call.
  7. The first line of a method is known as its ________.

1.3.7: 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.8: 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.9: 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.

Specifications

  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.9: Zipping (Compressing) Files.

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

1.3.10: Review

  • A computer is a machine that only does what it is programmed to do
  • A computer carries out instructions exactly (no interpretation)
  • Computers carry out their instructions quickly
  • Computer programs are the instructions for the computer
  • Programming is about developing a list of instructions for a computer to follow
  • 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: a code module defining the general concept of an object.

  • To work with Greenfoot, we 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

Reflection

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

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

Adios

Due Next:
B1: Boss Event (Canvas) (9/4/18)
Lab 1: First Tutorial (9/4/18)
Q1: Getting Started (9/6/18)
  • When class is over, please shut down your computer
  • Clean up around your desk area
  • You may complete unfinished exercises at the end of the class or at any time before the next class.
  • For help: email me or stop by during office hours (see home page)
Last Updated: September 10 2018 @19:36:31