Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
Think Java:
How to Think Like a Computer
Scientist
5.1.2
by Allen B. Downey
Chapter 5:
Introduction to GridWorld
Agenda
• History and motivation
• Getting started with GUI controls
• Coding in GridWorld
• Hint for Ex1
• Hint for Ex2
• Hint for Ex3
• Start Assignment 5
• Review Practice Test Solutions
• Questions on Assignment 3 and 4
2
3
GridWorld is a Case Study for HS AP Exam
• Give students a larger program to study and extend– context to apply inheritance and polymorphism
• Serve as an example of good design and coding practices– and a context for discussion of issues
• An early study showed that students who had a case study performed better on the AP exam than those who didn't– women perform better than men usually on case study
questions
4
Goals of GridWorld
• Provide open-ended framework for more advanced coding projects
• Be rich enough for a variety of exam questions
• Engage students in a more dynamic problem domain
• Help illustrate more abstract object-oriented programming concepts
GridWorld Demo
• 2D Arcade Game
• Objects: Bug
– Flower
– Rock
• Controls to run/stop
• Ability to add or
modify objects
Setting Up in BlueJ
• First, download the zip file linked from the lab5 handout. Extract this and place on your desktop.
• In BlueJ, choose Tools > Preferences > Libraries
– Then ADD, Navigate to GridWorldCode folder
– Choose gridworld.jar
– Shut down BlueJ and restart
Start Lab 5
• Explore GridWorld's GUI interface by following the instructions on page 1 and 2 of the lab5 handout.
Agenda
• History and motivation
• Classes, Objects and Methods
• Coding in GridWorld
• Hint for Ex1
• Hint for Ex2
• Hint for Ex3
• Start Assignment 5
• Review Practice Test Solutions
• Questions on Assignment 3 and 4
8
Classes, Objects and Methods
• Objects are software entities
that model real world things
• Objects have attributes
– like color (red), direction—facing N,
– health status, etc.
• Objects have methods to allow you to see attributes (getColor, getLocation…) and modify attributes (move, turn, …)
Objects vs Classes
• A class is like a blueprint or design used for creating objects.
• One class can generate thousands of objects.
• "Class" is kind of like "Data Type"
• "Object" is kind of like "Variable"
Agenda
• History and motivation
• Classes, Objects and Methods
• Coding in GridWorld
• Hint for Ex1
• Hint for Ex2
• Hint for Ex3
• Start Assignment 5
• Review Practice Test Solutions
• Questions on Assignment 3 and 4
14
Coding in GridWorld
• We are using a complex software framework already developed for us
• Very typical of real software development
• Have to import software packages we are using
– are in gridworld.jar
– Documentation
Objects are created in code using newfollowed by a constructor method
• To make an object, we use the new operator
Referring to and Modifying Objects
• Some objects can be created anonymously and passed to other methods, like:
•world.add(new Bug());
•world.add(new Rock());
• But if we want to access an object, we must store it in a variable of that class
•Bug redbug = new Bug();
•redBug.move();
Hints on Exercise 1
• We can invoke move() directly on redBug
– redBug.move();
• Or we can pass redBug to a method and have it implement more complex commands– moveBug(redBug);
Hints on Exercise 1
public static void moveBug(Bug someBug, int n)
{
if (n>0)
{
if ( someBug.canMove() )
{
someBug.move();
// invoke moveBug again with n-1
}
}
}
Hints on Exercise 2
import info.gridworld.actor.Actor; // add this at top
• ActorWorld world =
new ActorWorld (new UnboundedGrid<Actor>());