23
PreAP Computer Science GridWorld Case Study (GWCS) GridWorld Lab 07 1-Day Minor Assignment The Actor Class and its Many Sub Classes Lab Objectives Understand the Actor super class and its methods. Understand the "is-a" inheritance relationship. Study the sub classes of the Actor class. Observe how sub classes re-define super class methods. Study multi-level inheritance. Lab Prerequisites Completed ExpoJava, Chapter 09 and completed GridWorld Lab 06 Understand fundamental "is-a" inheritance relationships. Lab Sequence of Steps # Actions 01 a Compile and Execute the GridWorldLab07 Project Create a GridWorldLab07 project in the GridWorldLab07 folder. This will result in the Figure 01 display. You will note that there are multiple files in the File View window. Figure 01 Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 1 02-26-13

AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

Embed Size (px)

Citation preview

Page 1: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

PreAP Computer Science GridWorld Case Study (GWCS)

GridWorld Lab 071-Day Minor Assignment The Actor Class and its Many Sub Classes

Lab Objectives

Understand the Actor super class and its methods.Understand the "is-a" inheritance relationship.Study the sub classes of the Actor class.Observe how sub classes re-define super class methods.Study multi-level inheritance.

Lab Prerequisites

Completed ExpoJava, Chapter 09 and completed GridWorld Lab 06Understand fundamental "is-a" inheritance relationships.

Lab Sequence of Steps# Actions

01a Compile and Execute the GridWorldLab07 Project

Create a GridWorldLab07 project in the GridWorldLab07 folder.This will result in the Figure 01 display.You will note that there are multiple files in the File View window.

Figure 01

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 1 02-26-13

Page 2: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

01b Compile and Execute the GridWorldLab07 Project

Compile and execute the GridWorldLab07 project.This will result in the Figure 02 display.

Figure 02 The focus of this lab assignment is on the inheritance relationships in the GridWorld case study.

02a Investigate the Actor Class Methods

Make sure that the Actor.java file is in the edit window.Minimize the comments and methods to get the Figure 03 view by clicking the [-] icons for those comments/methods. The icons will change from [-] to [+].

Figure 03

Right now, we are not at all interested in how any of these methods work. This is why we minimized them. Just scroll down and look at the names of all of the methods available in the Actor class.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 2 02-26-13

The first arrow is pointing to a [-] symbol which shows the Actor class is maximized.

The second and third arrows are pointing to [+] symbols which show the comment and method are minimized.

The grey word comment and the [..] symbol also show these are minimized.

Page 3: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

02b Investigate the Actor Class Methods, Constructor

Maximize the constructor of the Actor class and its comment.You should see something similar to Figure 04 below.NOTE: The comments are BEFORE the method headings, not after.The Actor class has four attributes or instance variables.The Actor class has only one constructor, which requires no information.Minimize the constructor of the Actor class and its comment after you have examined them.

Figure 04

The grid attribute stores Actor objects. It allows an Actor object to know where it belongs.

The location attribute stores grid coordinate information. It identifies the (row,col) location of the Actor object in the grid.

The direction attribute stores information of the possible directions an object can face. It is used to determine which way an Actor object is facing. Direction is significant, because when an object moves forward, it means a move in the direction that the object is facing.

The color object stores a color. It is used to store the current color of the Actor object.

All Actor objects are constructed with default information. There is no parameter constructor.

The Actor object is constructed with ...color is BLUE.direction is NORTH.grid is null. (the object has no home yet)location is null. (the object has no grid location yet)

02c Investigate the Actor Class Methods, getColor and setColor

Maximize the getColor and setColor methods and their comments.You should see something similar to Figure 05 below.Method getColor gets the current color of an Actor object.Method setColor changes the current color of an Actor object.Minimize the getColor and setColor methods and their comments after you have examined them.

Figure 05

There is a naming convention shown here that you will see frequently as you learn more programming. The methods in a class can be divided into two major groups.

First, there are the get methods. These methods are read-only methods and return requested values of object attributes.

Usually the get methods are also return methods.

Second, there are the set methods. These methods are read-and-write methods and alter the values of specified attributes with the new parameter values.

Usually the set methods are void methods.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 3 02-26-13

Page 4: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

02d Investigate the Actor Class Methods, getDirection and setDirection

Maximize the getDirection and setDirection methods and their comments. You should see something similar to Figure 06 below.Method getDirection gets the current direction of an Actor object.Method setDirection changes the current direction of an Actor object.Minimize the getColor and setColor methods and their comments after you have examined them.

Figure 06

Note that once again the methods follow the get and set naming convention, mentionedin Step 02c.

The GridWorld has the following constants declared for use with directions:

NORTH = 0;EAST = 90;SOUTH = 180;WEST = 270;NORTHEAST = 45;SOUTHEAST = 135;SOUTHWEST = 225;NORTHWEST = 315;

02e Investigate the Actor Class Methods, getGrid and getLocation

Maximize the getGrid and getLocation methods and their comments.You should see something similar to Figure 07 below.Method getGrid gets the current GridWorld of an Actor object.Method getLocation gets the current location of an Actor object.Minimize the getGrid and getLocation methods and their comments after you have examined them.

Figure 07

The Grid class will get attention in the future, after you have learned more computer science.

Right now accept the fact that a grid object stores multiple Actor objects. It is possible to create multiple Grid objects.

Method getGrid identifies the GridWorld of an Actor object.

Method getLocation is more detailed and returns the location of an Actor object in a GridWorld.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 4 02-26-13

Page 5: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

02f Investigate the Actor Class Methods, putSelfInGrid and removeSelfFromGrid

Maximize the comments for the putSelfInGrid & removeSelfFromGrid.

Do not maximize the methods themselves because the code for these methods is beyond what we are doing right now. You need to understand what these methods do. You do not yet need to understand how.

You should see something similar to Figure 08 below.

Method putSelfInGrid places an Actor object in a specified Grid object at a specified location.

Method removeSelfFromGrid changes the current direction of an Actor object.

Minimize the comments for the putSelfInGrid and removeSelfFromGrid methods after you have examined them.

Figure 08

An Actor object has the ability to add itself to an existing Grid object, at a specified location, with the putSelfInGrid method.

An Actor object can also remove itself from any Grid object.

02g Investigate the Actor Class Method, moveTo

Maximize the comment only for the moveTo method.

As in the previous step, do not maximize the actual method.

You should see something similar to Figure 09 below.

Method moveTo moves an Actor object to a new location in the current Grid object.

Minimize the comment for the moveTo methods after you have examined it.

Figure 09

Check out the preconditions for this method. It is very important to read and understand preconditions and postconditions.

The free response section of the AP Computer Science Examination contains many preconditions and postconditions. Students must carefully obey these conditions.

Frequently, students write program code that works without a problem. The code compiles and the code executes. Unfortunately, the program does not satisfy the precondition and/or the postcondition.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 5 02-26-13

Page 6: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

02h Investigate the Actor Class Methods, act and toString

Maximize the act and toString methods and their comments.

Yes, maximize the methods also.

You should see something similar to Figure 10 below.

Method act constantly reverses the direction of an Actor object.

The toString method returns a String with information about the Actor object, which includes its location, direction and color.

Minimize the act and toString methods and their comments after you have examined them.

Figure 10

It may be beneficial to check out the execution of the GridWorldLab07 project. In the top-left corner you will see the blue Actor object. Use the Step approach and you will see that with each step the Actor object reverses direction by performing a 180-degree turn.

If you examine the act method you should see that it starts by retrieving the current direction (getDirection) and then makes it rotate 180 degrees. This is done by adding a semicircle (Location.HALF_CIRCLE).

The toString method is a neat method that will get a complete treatment in some future chapter. You will do some experimenting with toString, later in this lab assignment to see some immediate benefit in handling GridWorld problems.

NOTE: The lab continues on the next page…

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 6 02-26-13

Page 7: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

03a Investigate the Rock Class

In the File View window, double click Rock.java. See Figure 11.This will cause the Rock.java file to be loaded in the edit window as shown in Figure 12.

The Rock class is the simplest example of a sub class of the Actor class.This means that a Rock is-an Actor.

Figure 11

Figure 12

A project like GridWorld can actually contain many different files.

This is the first example of inheritance.

The immediate indication of inheritance is the statement

public class Rock extends Actor

This means that Rock is a subclass of Actor and Actor is the superclass of Rock.

This also means that all the methods in the Actor superclass are available in its Rock subclass.

The Rock class has two constructors. There is a default constructor that makes a Rock object red.

The overloaded, parameter, constructor is constructed with the parameter color.

The act method is re-defined in the Rock class to do absolutely nothing.

Is this method really necessary if it is empty?

We will examine this on the next couple pages.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 7 02-26-13

Page 8: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

03b Investigate the Rock Class

Compile and Execute the project.Click on an Actor object and examine the available methods.You should see something similar to Figure 13 below.

Figure 13

Note that the very first method available for Actor objects is act.Now click on a Rock object and examine its available methods.You should see something similar to Figure 14 below.

Figure 14

Figure 06 shows the available methods of the Actor class. Several other GridWorld classes inherit from this class.

They are called the subclasses of the Actor superclass.

Figure 07 shows the available methods of the Rock class. You see plenty of methods that are available, which are not declared in the Rock class. A quick glance back at the Actor methods or your memory should verify that all these methods come from the Actor class.

If you look closely you should see a line separating the act method from the rest. All of the methods below the line have an Actor icon. This means these methods were inherited from the Actor class.

There is one method, act, which is above the line and has a Rock icon. This is a new method created specifically for the Rock class.

Even though the Actor class already has its own act method, the Rock class is not using it. It is using its own re-defined act method. You can think of this as “new and improved”.

Rock objects may stand around doing nothing, but they are tough. You will find that Rock objects cannot be removed by other objects.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 8 02-26-13

Page 9: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

03c Understand the Essence of Redefining

If you look at Figures 05, 06 and 07 you may notice something peculiar. The Rock class does not inherit the act method from the Actor class because it supposedly has its own re-defined method. What may seem weird is that this new and improved method has nothing in the method body. What is the point of this method? To answer this question comment out the act method in the Rock class only as is shown in Figure 15.

Figure 15

Now re-compile and re-execute the project.Click the Run button and look specifically at a Rock object.Do you notice that its behavior is different not that it act method is gone?Since the Rock has no act method, it is inheriting the act method from the Actor class. The means a Rock will now act like an Actor.

Uncomment the Rock’s act method, re-compile, and re-execute.Click the Run button and look specifically at a Rock object again.Now the Rock is doing nothing. It is just sitting there like a Rock should.

Does it make sense now why the Rock needs an act method with an empty body? If that empty method is not present, the Rock inherits the Actor’s act method. We do not want a Rock that does flip-flops like an Actor. We want the Rock to do nothing, so we must specifically tell the Rock to do nothing with the empty method.

You will not understand the point of this unless you are literally looking at the execution of the screen.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 9 02-26-13

Page 10: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

04 Investigate the Flower Class

In the File View window, double click Flower.java to load it in the edit window as is shown in Figure 16.

Like the Rock class, the Flower class re-defines the act method.The Flower’s act method changes the color of the Flower object(makes it slightly darker) with each step. This is meant to simulate that the Flower object is wilting.

Figure 16

Flower objects are much wimpier than Rock objects and you will find that other objects run all over them and essentially eat them along the way.

05a Investigate the Bug Class

Load the Bug.java in the edit window and look at its re-defined act method as is shown in Figure 17.

The Bug class is considerably more complex than other Rock or Flower.

Figure 17

You will need to take a close look at the Bug class. This is no simple Rock or Flower.

This class not only re-defines the act method, it also throws in three of its own brand-new methods.

These methods are move, canMove and turn.

You have used the move and turn methods in several previous GridWorld lab assignments.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 10 02-26-13

Page 11: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

05b Investigate the Bug Class

Scroll down to the act method constructors as is shown in Figure 18.The Bug class re-defines the act method.Method act seems to move if possible and turn otherwise.

Figure 18

The key issue with each sub class of the Actor class is to determine how the sub class acts. So how does a Bug object behave? You know it moves and turns, because you have observed that behavior. You also know that it removes flowers and goes around rocks.

Check the act method. It is re-defined and short. The problem is that the short redefinition involves calling three new methods.

One thing is clear, our friendly bug either moves or turns. So we need to investigate what methods move and turn do. There is also a condition, which is based on a method call to canMove. What does that mean?

First consider the turn method. It calls the setDirection method. You do not see this method inside the Bug class, because it is not re-defined, but it is an Actor method. First, method getDirection is called, which determines how the Bug object is facing. Constant attribute HALF_RIGHT adds 45 to the current direction. The bottom line? Anytime that method turn is called, the bug turns 45 degrees clock-wise.

05c Investigate the Bug Class

The Bug class defines two other new methods: move and canMove.

Figure 19

Method move is so far the most complex method you have investigated. First it says that if the Grid object does not exist (equals null) then leave the method and do nothing. Basically, you cannot move in a grid that does not exist.

Do you notice something strange here?There is a return in a void method!This is actually allowed, as long as no value is returned. In this case return (without a value) means “get me out of this method now”.

The Bug objet determines its current Location and then gets the next Location. The next Location is the next cell in the direction that the Bug object is facing.

If the next Location is a valid location, the Bug object moves to that cell. If the next Location is not valid the Bug object removes itself from the grid.

After the Bug object moves to the next cell in the grid, a new Flower object is constrcuted, with the same color as the Bug object, and placed in the Location that was just vacated.

Method canMove involves other classes that will be explained later. Here is essentially what will happen. A Bug object can move to an empty grid cell, which occurs when neighbor == null. A Bug object can also move if the neighbor is a Flower object. This means that for all other types of objects, the Bug object cannot move.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 11 02-26-13

Page 12: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

06a Investigate the BoxBug Class, the Constructor

Load the BoxBug.java in the edit window, like Figure 20.Something has changed. Look at the arrow statement.The extends keyword indicates inheritance.However, in this case BoxBug is a subclass of Bug.

Figure 20

You now see the first example of multi-level inheritance. It can be stated that the Actor class is the grand parent of the BoxBug class. A BoxBug is-a Bug and a Bug is-an Actor.

It can be stated that the Actor class is the grand parent of the BoxBug class.

This means that any BoxBug object has access to the Actor methods. However, the Bug object re-defined some methods and also defined several completely new methods.All of these methods are now available tothe BoxBug objects.

The BoxBug constructor is different from other Actor constructors. Color is not an issue, but size is. BoxBug objects move in a square pattern and the size of the square or sideLength of the square must be known. This value is assigned during the construcution of a new BoxBug object.

In the BoxBug constructor you will also note that another new attribute, steps, is initialized. The value of steps is used in the act method, which is explained in Step 06b.

06b Investigate the BoxBug Class, the act Method

Method act, shown in Figure 21, is re-defined in the BoxBug class

Figure 21

The secret of the BoxBug class is the redefinition of the act method. The Bug object was designed to keep on moving whenever possible. Now with the BoxBug object movement is determined by the number of steps that are taken.

If steps equals the entered sideLength, movement stops and two turns are made. Two turns equals 90 degrees and the pattern of drawing a square becomes reality. At the same time steps is initialized back to 0 to allow another side of the square to be drawn.

This concludes the BoxBug class, because all the other methods are the same as the Actor methods or the Bug methods.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 12 02-26-13

Page 13: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

06c Investigate the BoxBug Class

Compile and Execute the project.Click the Run button and let it run for a while until the screen looks similar to Figure 22.

Figure 22

The BoxBug gets it name from the fact that it draws squares or boxes.

07a Investigate the OctagonBug Class

Figure 23 shows that the OctagonBug class is similar to the previous BoxBug class.

Figure 23

Like the BoxBug, the OctagonBug is also an example of Multi-level Inheritance.

An OctagonBug is-a Bug anda Bug is-an Actor.

A small change can have a large impact.At first glance it may appear that the BoxBug class and the OctagonBug class are identical.

The BoxBug calls method turn twice, for a total of 90 degrees, each time that the sideLength is reached.

The OctagonBug only calls method turn once, for a total of 45 degrees.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 13 02-26-13

Page 14: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

07b Investigate the OctagonBug Class

Compile and Execute the project.As before, click the Run button and let it run for a while until the screen looks similar to Figure 24.

Figure 24

As you might expect, the OctagonBug gets it name from the fact that it draws octagon shapes.

08 Complete the GWExercises07 Sheet

Some answers come from observing the execution.Some other answers come from observing the figures in thislab assignment or reading the comments out to the side.

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 14 02-26-13

Page 15: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 15 02-26-13

Page 16: AP Computer Science GridWorld Case Study (GWCS)vernonmath.com/wp/wp-content/uploads/2014/11/GridW…  · Web viewPreAP Computer Science GridWorld Case Study ... The grey word. comment

Exposure Java 2013, PreAPCS Edition GridWorld Lab 07 Page 16 02-26-13