Intro to JavaCourse Home Related Links |
AnnouncementsNovember 16, 2019I 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 WorkHere 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 ClassPM ClassSoundsWhat 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:
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, 2019I'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 ScratchThis 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 GreenfootBelow is some code to make an actor follow the mouse in Greenfoot. Before you can add the code, you need to:
Here is the code you need for your act() method: Preparation for Pair Programming: Getting sound from ScratchThe 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 ScratchHere's code for doing the same thing in Scratch:
Clicker Game with Scoring in ScratchThis video shows how to make a complete clicker game with scoring in Scratch: Pair Programming: Add Scoring with Counter classScenario 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 classChoose 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 counterAdd 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 MyWorldWe 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 variableTake 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 MyWorldClean 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 MyWorldIn 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 SounderMyWorld 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, 2019Sub next weekTJ will be out next week. David will be here, and you will have a substitute teacher. Frog Eats Fly: IntroductionDownload: frog-eats-fly-v1.zip In this activity, you will learn about:
Frog Eats Fly Part 1: Put the actors into the worldThe 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).
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 FlyIn 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:
We want to get a fly if it is in the same location as the frog, we'll use this call:
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
creates a variable of type Actor called worm. On line 10 above, the value returned by getOneObjectAtOffset() is stored in the worm variable.
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!
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 FlyIn 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 OwnCustomize this project however you like. Here are some ideas:
Pair programming: Shooting and ScoringDownload for Driver: tut-access-p1.zip Instructions for Navigator: How to access one object from another This tutorial will teach you about:
Scratch at HarvardI 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, 2019Expo!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 JavaWatch 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 JoCIf 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 class14: A first look at variables 15: Object interaction (first encounter) 16: Adding a score counter Describing and building methodsWriting 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:
To build a method properly, you need to know how to write:
October 19, 2019Today 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 bodyLast 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
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 typesLast 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
The method definition for
The parameter called
the parameter If we wanted to make a turn of 180, we could call
and the parameter 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, Three places to find method headersMethod headers are very important because they tell us how to execute method calls. For example, the method header for 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:
QuizletFatCat PrepTo 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:
Documentation ViewI'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:
Making MyCat do stuffDownload 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.
More trick the turtleWork 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:
If there's time at the end of class, we'll play another Kahoot! with the vocabulary from last week. October 12, 2019This week we're going to learn about pair programming and get started on a project that will teach you how to:
Types and valuesThere are different types of information. One type of information we've seen is the JoC #4 VocabularyToday's Kahoot! will include vocabulary from last week plus three new ones method callA request sent to an object to perform an action.
argumentInformation passed to a method.
method definitionCode that describes the actions to take when a method call is made.
QuizletHere 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 TutorialsPair programming will be based on the following tutorials:
October 5, 2019The 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:
If we have time, we'll watch a video on pair programming to prepare for next week's pair programming work. JoC #3 VocabularyReview terms from last week to prepare for Kahoot! classUsed to create objects. objectAn instance of a class. Many of these can be created from one class. methodAction that an object can perform after it has been created. act methodA special method invoked once with the Act button and repeatedly by the Run button. instanceAnother word for object. return valueA value returned by a method with a non-void return type. Examples: true, 7, 18, false, -3. return typeKind of information returned by a method. Examples: boolean, int. void return typeUsed to define a method that doesn't return a value. Methods with this return type are like commands. int return typeIndicates an integer value. Used to define a method that answers questions about number. boolean return typeIndicates a value of true or false. Used to define a method that answers true/false questions. September 28, 2019Welcome to Intro to Java! Here's what we'll do today:
In Between TimesIf 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 #3Below 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 HomeHere are some things you might want to do at home:
|
||||
|