Intro to Java

Course Home
Syllabus
Scratch to Java
Reflections


Related Links

Announcements

November 16, 2019

I didn't get many complete project zip files last week. Here are complete versions of project you've worked on this session:

Here's one we haven't looked at yet: how to make things crash and explode

Here's little crab with lives 

Past Student Work

Here are zip files I received from students since the first day of class. You will need to unzip the file with your name on it and then unzip the projects you find inside.

AM Class

PM Class

Sounds

slurp.wav

What next?

If you would like to explore Greenfoot and Java further on your own, there are some great tutorials in the documentation section of the Greenfoot website.

If you're interested in Scratch, you can check out the tutorials on the Scratch website or look into the Scratch I (Grades 3-5) or Scratch II (Grades 3-5) courses available online through CTD's Gifted Learning Links program.

There are also these technology classes offered by CTD in their Weekend Program:

  • Inspired by Nature: Designing with LEGO┬« Robotics AM (Gr. 4-5)
  • Wearable Technology: Creativity and Innovation in Computer Science AM (Gr. 5-7)
  • Web Design: HTML, CSS and JavaScript AM (Gr. 5-7)

You might also want to browse the Winter 2020 Advanced Enrichment Programs.

Going Farther with Scratch/Snap!

Here's how Scratch is used in Harvard's CS50 course:

There's a more advanced version of Scratch call Snap! that's worth checking out.

Berkeley runs an AP Computer Science Principles course using Snap! The course is meant for non-CS majors, but it is pretty programming intensive

November 9, 2019

I'm out today. Here are some ideas for class activities. You sub will help you decide which of these to work on, or she might have other activity ideas. If there's time, you can work on an Expo! project of your own design or work through some Joy of Code videos.

Warmup 1: Following the mouse in Scratch

This script will make the sprite follow the mouse, but if you move the mouse fast enough the sprite will lag behind some.

Warmup 2: Following the mouse in Greenfoot

Below is some code to make an actor follow the mouse in Greenfoot.

Before you can add the code, you need to:

  1. Create a new Greenfoot scenario
  2. Add a subclass of the Actor class. Suggestions for class names:
    • Class names should be capitalized.
    • Class names should be related to the actor's image (for example, we used the class name Worm for the class used to create worm objects) or class's role in the scenario (for example, you might use the class name MouseFollower for the class in this scenario).
  3. Place an actor in the world.
  4. Use Save the World so the actor will always be in the world when you press Reset or the next time you open the scenario.
  5. Open the editor the class you created

Here is the code you need for your act() method:

Preparation for Pair Programming: Getting sound from Scratch

The video below will show you how to get a sound from Scratch and put it into Greenfoot. Create a scenario and make a subclass of Actor. Then follow the video. For the video, I called my subclass Sounder and gave it the image of a balloon.

Here's code for the act() method in my Sounder class:

 

Clicking on Sprites in Scratch

Here's code for doing the same thing in Scratch:

 

Clicker Game with Scoring in Scratch

This video shows how to make a complete clicker game with scoring in Scratch:

Pair Programming: Add Scoring with Counter class

Scenario for Driver: Start with the balloon-clicking scenario you built or download click-actor.zip

Instructions for Navigator: Follow the steps below.

Step 1: Import the Counter class

Choose the "Import Class..." item from the Edit menu.

 

Choose the Counter class in the Import Class dialog box. A thick black line will appear around the Counter class box and you will see documentation for the Counter class.

Click OK

The Counter class will be added to your project.

Step 2: Set up the balloon and the counter

Add a balloon and a counter to the world. The Counter class has two constructors. If you select "new Counter()" without parameters, you won't need to add an argument. If you select "new Counter(String prefix)" you'll have to supply a String argument. String arguments have quotes around them. For example, you could use the string "score ".

Open the drop-down menu on the world and choose "Save the World".

The code shown below does need to be added by you. When you "Save the World" a method definition and a a method call are automatically added to your MyWorld class. Open your editor for the MyWorld class and find this method definition:

This method declares two variables, sounder and counter, and uses them to add Sounder and Counter objects to the world.

The method call to prepare() is also added automatically to the MyWorld constructor. See if you can the method call to prepare() in the MyWorld constructor.

Step 3: Create the counter field in MyWorld

We saw how "Save the World" creates variables in the prepare() method. There is another way to create variables in Java. Add the following code to your MyWorld class:

 

This variable is declared inside of the MyWorld class, but outside of any method. So it is inside the green rectangle of the class but not inside any yellow rectangle.

Step 4: Modify prepare() in MyWorld to use the new variable

Take another look at the counter variable in the method definition for prepare():

The new counter variable that you added at the top of the class can be shared with different methods in MyWorld and can even be shared with objects made from different classes. Variables like this are called fields. The variables that get added to prepare() when you "Save the World" cannot be shared in this way. They are called local variables.

To tell Java we want to use the counter field instead of the local variable, we remove the Counter type from the counter declaration in the prepare() method definition to make it look like this::

Now Java will use the counter field to refer to the counter that is added to the world. We have to do this so we can share the reference to that counter to update the number it shows.

Step 5: Polish up prepare() in MyWorld

Clean up your prepare method a bit. With the xy coordinates (300, 200) the sounder can be placed exactly in the middle of the screen. Add a String prefix to the counter like "score ". You can use a space at the end of the String to separate the prefix from the number displayed by the counter. You can also change the xy coordinates of the counter in the prepare() method:

The changes above will make the scenario start out like this:

Step 6: Add the changeScoreBy() method definition to MyWorld

In Scratch, we saw that we could keep score by creating a score variable and adding this block:

Greenfoot doesn't have a built-in method to do this, but we can build one.

In this step, we will add a changeScoreBy() method defintion to MyWorld for changing the score in Greenfoot. In the next step, we will see how that method can be called from Sounder when the sounder with the balloon image is clicked.

Add this method definition to MyWorld:

Step 7: Add the changeScoreBy() method call to Sounder

MyWorld is in charge of the counter. It creates the counter, adds the counter to itself, and knows how to change its value with changeScoreBy(). If the sounder wants the counter to change, it needs to ask the world to make the change.

Get the world and ask it to change the counter by adding code to the act() method in the Sounder class:

 

Step 8: Try it out!

Press the Run button and start clicking the sounder with the balloon image. The score should increase by 1 each time you click!

November 2, 2019

Sub next week

TJ will be out next week. David will be here, and you will have a substitute teacher.

Frog Eats Fly: Introduction

Download: frog-eats-fly-v1.zip

In this activity, you will learn about:

  • detecting collisions
  • accessing the world from an actor
  • removing objects

Frog Eats Fly Part 1: Put the actors into the world

The frog is added to the world with a method call to addObject(). Here is the javadoc entry for addObject() method as defined in the World class:


In this step, we'll make a frog start off at x coordinate 200 and place a fly at x coordinate 400. We're giving them both the same y coordinate to make sure they collide later. So the frog will be at (200, 200) and the fly will be at (400,200).

  • Add one frog and one fly to the world.
  • Right-click on the background and Save the World
  • Open the editor for the MyWorld class. Find the method definition for the prepare class.
  • Modfy the method calls to addObject() so that the frog is placed at (200,200) and the fly is placed at (400,200).

When you are finished, your prepare() method should look like this:

 

Make sure the frog and fly show up in the proper place when you press Reset. They should look like this:

Now, open the editor for the Frog class. Add a method call to move() to method definition for act(). Give the move() method an argument of 4.

Test out your program to make sure the frog moves when you press the Run button. It should move over the fly before hitting the edge of the world.

Frog Eats Fly Part 2: Try to Get a Fly

In this part, the frog will try to get a fly with getOneObjectAtOffset(). Here is the javadoc entry for getOneObjectAtOffset() method as defined in the Actor class:


In this step, we'll add a call to getOneObjectAtOffset() so the frog will try to make contact with the fly. The parameters dx and dy will be relative to the frog's location. This means that the frog's location will have a dx of 0 and a dy of 0. If we wanted to get a fly 3 cells to the right of the frog and 7 cells below the frog, we would call the method like this:

getOneObjectAtOffset(3, 7, Fly.class);

We want to get a fly if it is in the same location as the frog, we'll use this call:

getOneObjectAtOffset(0, 0, Fly.class);

The last argument to getOneObjectAtOffset() should be Fly.class, since we're checking for a fly.

It might help to use this code from little crab as a model:

 

I removed the code for controlling the crab with the keyboard since we're not using it in this project. If you like, you can add keyboard control when you are finished.

On line 9 above, the statement

Actor worm;

creates a variable of type Actor called worm. On line 10 above, the value returned by getOneObjectAtOffset() is stored in the worm variable.

worm = getOneObjectAtOffset(0, 0, Worm.class);

Don't forget though: We're adding code to our Frog class, not a Crab class, and the frog is trying to get a fly, not a worm!

  • In the Frog class's method definition for act(), add a line declaring a variable fly of type Actor.
  • On the next line, write an assignment statement to store the return value of getObjectAtOffset() in the fly variable. In the call to getObjectAtOffset(), the dx and dy arguments should both be 0, and the clss argument should be Fly.class.

When you are finished, make sure your code compiles. Notice that the code we've added so far doesn't give the frog anything new to do. It just asks a question: Is there a fly at my location? In the next part of this project, we'll decide what to do if the answer is yes.

Frog Eats Fly Part 3: Eat the Fly

In this part, we'll need two methods from the greenfoot package. First, from the Actor class, we need the getWorld() method:


The frog will need to get the world so it can ask the world to remove the fly. Here's the removeObject() method of the World class:


Here's a version of little crab with code added to remove the worm when the crab moves over it:

Now make your frog eat your fly the way the crab eats the worm above.

Frog Eats Fly Part 4: Make it Your Own

Customize this project however you like. Here are some ideas:

Pair programming: Shooting and Scoring

Download for Driver: tut-access-p1.zip

Instructions for Navigator: How to access one object from another

This tutorial will teach you about:

  • fields
  • constructors
  • adding objects
  • accessing one object from another

Scratch at Harvard

I mentioned in an earlier class that Scratch is sometimes used in intro college courses in computer science. If you're curious to see what's done with Scratch at Harvard, check out these links:

October 26, 2019

Expo!

Expo! is on the last Saturday of class, November 16. Anything you've built in pair programming or in following videos is fine. If you have an idea for a project of your own design, that's OK too, but check with me to make sure it's something that's doable between now and the 16th.

Scratch to Java

Watch these videos to understand Java and Greenfoot more deeply by comparing them with Scratch.

You might find these downloads helpful:

Ideas from you guys

Go further with JoC

If you want to do more with trick the turtle, you might want to start with this version of trick the turtle and jump ahead to JoC #13.

13: The structure of a class
14: A first look at variables
15: Object interaction (first encounter)
16: Adding a score counter

Describing and building methods

Writing code in Java requires you to understand method descriptions and properly construct methods.

To read and share method descriptions, you need to be able to answer these questions:

  1. Is the return type void or something else?
  2. How many parameters?
  3. What are the parameter types?
  4. If the return type is void, what does the method do? Otherwise, what does the method return?

To build a method properly, you need to know how to write:

  1. The method header
  2. The method body

October 19, 2019

Today we'll finish up little crab and get started on fat cat. If you're done with little crab, you might want to:

We'll also have a new kahoot!

Method header and method body

Last week, we looked at method definitions. Today we look at the two main parts of a method definition: the method header and the method body.

Method header

The method header gives us information about the how the method will be used. For example, the act() method can be called be called with the statement

act();

Since it has a void return type, we know that it doesn't return any information.

 

Method body

The method body is the group of statements below the method header that is surrounded by opening and closing braces.

 

Parameters and parameter types

Last week, we talked about arguments, the values that some methods need to get in order to do their jobs. Arguments are used in method calls. To write a method definition for a method that needs arguments, we write parameters into the method header. For example, the act method below has a method call turn with an argument 1.

 

The method definition for turn has a method header with a parameter:

 

The parameter called angle has a parameter type of int. When we make the method call

turn(1);

the parameter angle is assigned the value 1.

If we wanted to make a turn of 180, we could call

turn(180);

and the parameter angle would be assigned the value 180.

This is a powerful feature of the Java programming language, because it allows us to reuse the same method with many different arguments.

Anything that works as a return type can be a parameter type. For example, int and boolean can both be parameter types and return types.

Three places to find method headers

Method headers are very important because they tell us how to execute method calls. For example, the method header for turn above tells us that we need to supply an int in our parameter list and no value is returned. The act has an empty parameter list, so no arguments are needed when we call the act method.

Since method headers are so important, we can find them in three different places in the Greenfoot environment.

When we use the editor in Source Code mode, we can see the method header at the head of each method definition:

 

In Documentation mode, the header appears like this:

 

In the popup menu for an Animal object, the method header looks like this:

 

Quizlet

FatCat Prep

To prepare for FatCat activities, we need to be sure that we understand how method headers relate to method calls. We'll go over some basics together and then do a Kahoot!

Here are some method headers shown in an object's popup window:

For each method header, you should be able to answer these questions:

  • What is the method name?
  • What is the method's return type?
  • How many parameters does the sleep method have?

Documentation View

I'll show everyone how to bring up the Documentation view in the Greenfoot editor. Then you can download fatcat, bring up the Documentation view for the Cat class, and answer these questions:

  • How many methods does the class Cat have?
  • How many methods return a boolean value?
  • How many parameters does the sleep method have?

Making MyCat do stuff

Download fatcat.

When that's done, you'll be ready to try out the activities below on your own. Make sure you only create objects from the MyCat class and only add code to the MyCat class.

  • Try calling some of your cat's methods interactively, by using the cat's popup menu. The interesting methods are all "Inherited from Cat".
  • Is the cat bored? How can you make it not bored?
  • Open the editor for class "MyCat". (This is where you will write the code for all the following exercises.) Make the cat eat when it acts. (That is: In the act method, write a call to the eat method.) Compile. Test by pressing the Act button in the execution controls.
  • Make the cat dance. (Don't do this interactively - write code in the act method to do this. When done, click the Act button in the execution controls.)
  • Make the cat sleep.
  • Make the cat do a routine of your choice, consisting of a number of the available actions in sequence.

More trick the turtle

Work through JoC #5. If you don't have your trick the turtle project from earlier classes you can start with this trick-the-turtle-joc4 scenario.

When you finish with JoC #5, you can go on to JoC #6. Here's a starter scenario trick-the-turtle-joc5 for that lesson if you need it.

What do I do when I'm done?

If you finish before everybody is done, you can:

  • Play around with FatCat some more.
  • Find your partner from the little crab activity and work on your crab scenario some more. If you partner isn't finished with FatCat, you can offer to help them finish, but they might want to finish on their own. If the two of you are finished with both FatCat and little crab, you can customize little crab by changing images or sounds, adding scoring, or adding a splash screen or a game over screen.
  • Work through some more Joy of Code videos. Michael Kolling's videos show you lots of cool extensions of trick the turtle, how to create a scenario starting with an empty project, and how to create a breakout game.

If there's time at the end of class, we'll play another Kahoot! with the vocabulary from last week.

October 12, 2019

This week we're going to learn about pair programming and get started on a project that will teach you how to:

  • Control the movement of actors with the keyboard with if-statements
  • Remove actors from the world and write methods
  • Play sounds
  • Make randomly moving enemies

Types and values

There are different types of information. One type of information we've seen is the int type. Another is the boolean type. Values that have type int include 1, 42, 0, and -7. The only possible values for the boolean type are true or false.

JoC #4 Vocabulary

Today's Kahoot! will include vocabulary from last week plus three new ones

method call

A request sent to an object to perform an action.

 

argument

Information passed to a method.

 

method definition

Code that describes the actions to take when a method call is made.

 

Quizlet

Here is a Quizlet with vocabulary from last week and this week. Use it to get ready for Kahoot! Try out the Shuffle option with the flash cards or choose another study mode to make sure you really know the terms!

The Crab Tutorials

Pair programming will be based on the following tutorials:

October 5, 2019

The Greenfoot environment is designed to make it easier you to see the relationships between classes, objects and methods. This will help you to read and write Java code more effectively. It will also help you make use of Greenfoot classes and objects to develop games and animated stories.

Today we will:

  • Review the vocabulary words from JoC #3
  • Play a name game with the vocabulary words
  • Play Kahoot!
  • Have an individual work period. If you complete JoC #4 today, you can:
    • Customize your project by changing images, class names, or class methods, or
    • Go on to later JoC videos, or
    • Develop a scenario of your own design
  • Write reflections

If we have time, we'll watch a video on pair programming to prepare for next week's pair programming work.

JoC #3 Vocabulary

Review terms from last week to prepare for Kahoot!

class

Used to create objects.

object

An instance of a class. Many of these can be created from one class.

method

Action that an object can perform after it has been created.

act method

A special method invoked once with the Act button and repeatedly by the Run button.

instance

Another word for object.

return value

A value returned by a method with a non-void return type. Examples: true, 7, 18, false, -3.

return type

Kind of information returned by a method. Examples: boolean, int.

void return type

Used to define a method that doesn't return a value. Methods with this return type are like commands.

int return type

Indicates an integer value. Used to define a method that answers questions about number.

boolean return type

Indicates a value of true or false. Used to define a method that answers true/false questions.

September 28, 2019

Welcome to Intro to Java! Here's what we'll do today:

  • Complete this background survey for AM students.
  • Send me an email with your full name in the subject line.
  • Get to know each other.
  • Discuss why we need special languages to communicate with computers.
  • Learn to program a wombat.
  • Join our Quizlet Class.
  • Start Joy of Code Lessons.
  • Play a game of Quizlet Live.
  • Writing a short reflection and sending it to tj@tjleone.com (I'll explain reflections later today).

In Between Times

If you've completed your survey, check out the Joy of Code Lessons to learn more about game-building with Java and Greenfoot. To watch the videos, you can ask for headphones or use your own headphones or earbuds if you brought them. If you've already worked with Greenfoot and would like to start on a game of your own, go for it!

Quizlet on JoC #3

Below are the terms you should know after working through JoC #3: Classes and Objects. Use the flashcards to review the terms before we play Quizlet Live!

At Home

Here are some things you might want to do at home:

  • Download and install Greenfoot on your home computer. Instructions for this are in JoC#2
  • Right click on the Hedgehog class in the Class Diagram. Select Set Image... How do you change the Hedgehog's image?
  • Download some sample projects ( ants, asteroids, fatcat, little-crab, marbles, piano, wave, wombats) and play around with them.

This web page was created to supplement a course offered by The Center for Talent Development. CTD This work is licensed under a Creative Commons Attribution 3.0 Unported License. Creative Commons License Terms of Use. Privacy Policy.