02 - Inheritance and Polymorphism - Subclasses and Methods
02 - Inheritance and Polymorphism - Subclasses and Methods
Due
Sep 5 by 11:59pm
Points
100
Submitting
an external tool
Available
until Sep 10 at 11:59pm
https://canvas.vt.edu/courses/135890/assignments/1280223 1/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
High-level algorithm:
Select a card
Purchase a card
“How will I get there: walk, drive, ride my bicycle, take the
bus?”
Note
The final step is to review the algorithm. What are we looking for?
First, we need to work through the algorithm step
by step to determine
whether or not it will solve the original problem. Once we are
satisfied that the algorithm does
provide a solution to the problem,
we start to look for other things. The following questions are typical
of ones that
should be asked whenever we review an algorithm. Asking
these questions and seeking their answers is a good way
to develop
skills that can be applied to the next problem.
any circle
(formula πR ) solves a more general problem.
2
Start
Finish
Hop 3 times
Turn right
Hop 2 times
Plant a flower
Hop East
Turn left
Hop once
Note
https://canvas.vt.edu/courses/135890/assignments/1280223 5/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
The recommended first build contains three things:
this.addObject(bobby, 0, 0);
This build adds the logic to “get the flower”, which in the detailed
algorithm (step 4 above) consists of hopping 3
times and then picking
the flower. The new code is indicated by comments that wouldn’t appear
in the original (they
are just here to call attention to the additions).
The blank lines help show the organization of the logic.
this.addObject(bobby, 0, 0);
By taking a moment to run the work so far, you can confirm whether
or not this step in the planned algorithm works
as expected. Right-click
on your island subclass in BlueJ’s main window and use “new” (the first menu
entry) to
create a new instance
https://canvas.vt.edu/courses/135890/assignments/1280223 6/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
create a new instance.
This build adds the logic to “put the flower”. New code is indicated
by the comments that are provided here to mark
the additions.
this.addObject(bobby, 0, 0);
bobby.hop(3);
bobby.pick();
this.addObject(bobby, 0, 0);
bobby.hop(3);
bobby.pick();
bobby.turn(RIGHT);
bobby.hop(2);
bobby.plant();
There are two Jeroos. One Jeroo starts at (0, 0) facing North with
one flower in its pouch. The second starts at (0, 2)
facing East with
one flower in its pouch. There is a net at location (3, 2). Write a
program that directs the first Jeroo
to give its flower to the second
one. After receiving the flower, the second Jeroo must disable the net,
and plant a
flower in its place. After planting the flower, the Jeroo
must turn and face South. There are no other nets, flowers, or
Jeroos
on the island.
Start
Finish
4. Jeroo_2 will have two flowers after receiving one from Jeroo_1.
Let’s name the first Jeroo Ann and the second one Andy.
Face South
Let’s name the first Jeroo Ann and the second one Andy.
Find Andy
Give ahead
Toss
https://canvas.vt.edu/courses/135890/assignments/1280223 9/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
Plant a flower
Face South
Turn right
This build creates the main method, instantiates the Jeroos, and
outlines the high-level algorithm. In this example,
the main method
would be myProgram() contained within a subclass of
Island.
this.addObject(ann, 0, 0);
this.addObject(andy, 0, 2);
https://canvas.vt.edu/courses/135890/assignments/1280223 10/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
This build adds the logic for Ann to locate Andy and give him a
flower.
this.addObject(ann, 0, 0);
this.addObject(andy, 0, 2);
ann.turn(LEFT);
ann.turn(LEFT);
ann.hop();
This build adds the logic for Andy to locate and disable the net.
this.addObject(ann, 0, 0);
this.addObject(andy, 0, 2);
ann.turn(LEFT);
ann.turn(LEFT);
ann.hop();
andy.toss();
https://canvas.vt.edu/courses/135890/assignments/1280223 11/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
y ();
This build adds the logic for Andy to place a flower at (3, 2) and
turn South.
this.addObject(ann, 0, 0);
this.addObject(andy, 0, 2);
ann.turn(LEFT);
ann.turn(LEFT);
ann.hop();
andy.toss();
andy.hop();
andy.plant();
andy.turn(RIGHT);
2.1.4.5. Self-Check
3
Whi h f th f ll i i NOT t
https://canvas.vt.edu/courses/135890/assignments/1280223
t id h d i i l ith ? 12/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
Which of the following is NOT a step to consider when designing an algorithm? Answer
https://canvas.vt.edu/courses/135890/assignments/1280223 13/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
Note
The name of a method should be a verb or a short verb phrase that describes what the method14/29
https://canvas.vt.edu/courses/135890/assignments/1280223
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
The name of a method should be a
verb or a short
verb phrase that describes what
the method
does.
// ----------------------------------------------------------
/**
* direction.
*/
this.turn(LEFT);
this.turn(LEFT);
// ----------------------------------------------------------
/**
*/
this.hop();
this.hop();
this.hop();
// ----------------------------------------------------------
https://canvas.vt.edu/courses/135890/assignments/1280223 15/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
// ----------------------------------------------------------
/**
*/
this.plantFour();
this.turn(RIGHT);
this.hop();
this.turn(RIGHT);
this.plantFour();
A Jeroo method is used just like any other method. In our island’s
myProgram() method, we just have to be sure to
create
a jeroo from our special subclass that contains the new methods we
want to use. Then we send a message
to a specific Jeroo object,
requesting that Jeroo to perform the task associated with the method.
this.addObject(ali, 5, 5);
ali.plantRowsOfFour();
We know
that when we create a subclass
that it inherits all of the
methods and attributes from the class that it
extends. If you create a
subclass of Jeroo called PlantingJeroo, then
any PlantingJeroo object can perform
all of the methods
that any Jeroo knows–because a PlantingJeroo
is a special kind of Jeroo. The
PlantingJeroo
class inherits all of
the methods and attributes from the class Jeroo, and also
understands any
new ones you write, such as the
platRowsOfFour() method. Computer scientists sometimes
call this an is-a
relationship,
because every PlantingJeroo object is a
Jeroo at the same time–just a Jeroo that can do more.
Note
Note
Every time you create a subclass, you are responsible for defining
all of the constructors it supports.
Constructors are not
inherited from superclasses.
// ----------------------------------------------------------
/**
*/
super(flowers);
While we have not yet covered all of the features in this small
piece of code, the gist is straightforward. A constructor
is
declared like a regular method, except that
we omit the word void
and its name is exactly the same as the class
name.
Here, we are defining a constructor for our
PlantingJeroo subclass that takes one number (integer)
as an
argument, representing the number of flowers in its pouch.
The body of this constructor contains only a single line that uses
the special Java keyword super. This word can
only
be used as the first word inside a subclass constructor, and it allows
us to invoke a superclass constructor,
passing it any information it
might need. So here, we are saying that the first (and only) action
in our
PlantingJeroo constructor is to call the
constructor for its superclass (Jeroo), passing the
number of flowers.
This allows the superclass
to initialize all of its attributes correctly with the given information.
If our subclass needed
more initialization, we would perform that in
following statements in the subclass constructor’s body.
Location Location
Direction
Direction
Flowers
Flowers
// ----------------------------------------------------------
/**
*
* @precondition The three spaces directly ahead of the Jeroo are clear.
*/
this.hop();
this.hop();
this.hop();
Start
Finish
https://canvas.vt.edu/courses/135890/assignments/1280223 19/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
Toss
Toss
Pick
Note
Method: turnAround()
Preconditions:
none
Postconditions:
Note
Method: tossAndHop()
Preconditions:
https://canvas.vt.edu/courses/135890/assignments/1280223 21/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
There is a net ahead
Postconditions:
Note
this.addObject(kim, 4, 1);
// ----------------------------------------------------------
/**
*/
// ----------------------------------------------------------
/**
* direction.
*
* @precondition None.
*
* @postcondition The Jeroo has turned 180 degrees.
*/
https://canvas.vt.edu/courses/135890/assignments/1280223 23/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
// ----------------------------------------------------------
/**
*
* @precondition There is a net ahead.
*
* @postcondition The net has been disabled.
*/
this.addObject(kim, 4, 1);
// ----------------------------------------------------------
/**
* direction.
*
* @precondition None.
*
* @postcondition The Jeroo has turned 180 degrees.
*/
// ----------------------------------------------------------
/**
*
* @precondition There is a net ahead.
*
* @postcondition The net has been disabled.
*/
this.addObject(kim, 4, 1);
kim.turnAround();
ki t A dH ()
https://canvas.vt.edu/courses/135890/assignments/1280223 // d 25/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
kim.tossAndHop(); // new code
// ----------------------------------------------------------
/**
* direction.
*
* @precondition None.
*
* @postcondition The Jeroo has turned 180 degrees.
*/
this.turn(LEFT);
this.turn(LEFT);
// ----------------------------------------------------------
/**
*
* @precondition There is a net ahead.
*
* @postcondition The net has been disabled.
*/
this.addObject(kim, 4, 1);
kim.turnAround();
kim.tossAndHop();
kim.tossAndHop();
2.1.5.4. Self-Check
3
return a + b + c;
} Need help?
I'd like a hint
return
https://canvas.vt.edu/courses/135890/assignments/1280223 27/29
9/7/21, 1:30 PM 02: Inheritance and Polymorphism: Subclasses and Methods
public
int
add3Ints
a+b+c
X859: Jeroo facing SOUTH wit…
You are writing a subclass of
Jeroo called SunnyJeroo 0
/ 1.0
and the constructor appear… Check my answer!
Reset
Java (/gym/exercises/search?s…
Next exercise (/gym/exercises/858/practice?lti_launch=true&workout_id=1343
https://canvas.vt.edu/courses/135890/assignments/1280223 29/29