BallWorlds

  1. CSSE 120 - Fundamentals of Software Development 1
  2. Rose-Hulman Institute of Technology
  3. Computer Science & Software Engineering

Designing and implementing multple classes: inheritance and interfaces

Part 4: DudThatMoves

Learning objectives

After completing all the parts of this lab, you should ...

... Be able to Explain the following concepts:

... Be able to Do the following:

Items in bold above are learning objectives for this part of the lab.

Instructions:

Pair Programming

Lab report

Time limit

Getting Started

What you should learn

Resources

The following steps should be familiar for you by now:

If not,

What you should do

Getting started

  1. Skim through this document. Then return to this point.
  2. Open your BallWorlds, Part 4 report (on Angel)
  3. Question: Who is your partner for this report?
  4. Question: About how many minutes do you think it will take you to complete Part 4 of BallWorlds?
  5. Question: What time is it now? (Later we will ask you how long you spent on Part 4 of BallWorlds.)

    Reminder: Each time that you see a question in this document prefaced by

    "Question: yada yada ..."
    put your answer in your BallWorlds report (on Angel) at that time.
  6. Open Eclipse. Update your BallWorlds project from your SVN repository (Team ~ Update), or else check out BallWorlds from your pair's SVN repository if you don't have it on your computer yet.
  7. Compile and run BallWorlds. You should see no error messages, except that only Duds can be created yet by the BallButtons.

Reviewing Dud

What you should learn

Resources

The following concepts should be clear to you now:

If not,

What you should do

Reviewing ideas from Dud

  1. Question: What happens if you click the Dud button multiple times? Are you creating multiple Dud objects? Do they all show up on the screen? Why or why not?
  2. Question: What is the difference between a class and an instance of that class ?
  3. Question: According to BallWorld's UML class diagram, a Dud has to implement the Animate, Drawable and Relocatable interfaces? What does that tell you?
  4. Question: Your Dud's constructor containted a statement like this:
        ballEnvironment.addBall(this);
    
    
    Explain that statement, word by word.

If any of the above questions are still confusing to you, ask your instructor or an assistant for help NOW -- you will be unable to complete the rest of this exercise without understanding the answers to the above questions.

Reviewing the UML Class Diagram

What you should learn

Resources

Reading a UML class diagram should be familiar territory to you by now.

If not,

What you should do

Understanding DudThatMoves from BallWorld's UML class diagram

  1. Briefly review the specifications of the BallWorlds project.
  2. Briefly review the UML class diagram for the BallWorlds project.

    Note its color-coding:

    • The six yellow classes are the six classes that you will eventually implement.
    • Those yellow classes extend the abstract Ball class, which in turn implements the three blue interfaces. Thus, your classes must implement those three blue interfaces.
    • The Ball class, and hence each of your classes which extends Ball, have the grey BallEnvironment object.
    • Some of your Ball classes may want to refer to instances of the green classes.

    So the only part of the UML class diagram relevant to what you will implement are the colored items listed above.

  3. Question: According to the above UML class diagram, what class must a Dud extend?
  4. Question: According to the above UML class diagram, what class must a DudThatMoves extend?
  5. Question: According to the above UML class diagram, what three interfaces must a Dud implement?
  6. Question: According to the above UML class diagram, what interfaces, if any, must a DudThatMoves implement?

If any of the above questions are still confusing to you, ask your instructor or an assistant for help NOW -- you will be unable to complete the rest of this exercise without understanding the answers to the above questions.

Thinking Before Doing

What you should learn

Resources

To call a superclass' constructor, use the super keyword, as in the following example.

    public class Animal {
        protected String name;
        public Animal() {
            this.name = "No-Name animal";
        }
        public Animal(String name) {
            this.name = name;
        }
    }

    public class Cat extends Animal {

        public Cat() {
            super();
        }

        public Cat(String name) {
            super(name);
        }
    }

    public class Dog extends Animal {

        public Dog() {
            // Calls the superclass' constructor implicitly
        }

        public Dog(String name) {
            super();
        }
    }

The Cat's constructors call the respective constructors in its Animal superclass.

The Dog's constructors call the no-parameter Animal constructor, either implicitly or explicitly. If the Animal class lacked a no-parameter constructor, the Dog constructors would yield compile-time errors.

What you should do

Thinking about a DudThatMoves

  1. Question: If you wanted a DudThatMoves to behave exactly like a Dud, what Dud methods, if any, would a DudThatMoves need to override?
  2. In fact, a DudThatMoves should behave exactly like a Dud with one exception -- a DudThatMoves should move.

    The framework calls act on each Ball every 20th of a second or so. So, a DudThatMove's act method is the place to change the DudThatMove's position.

    Question: What data should a DudThatMoves store in fields? List several reasonable choices for how to store that data, that is, list several types that could reasonably be used for that data. What type seems most natural to you?

  3. Question: As explained above, a DudThatMoves must override the Dud's act method. Which, if any, of Dud's other methods must a DudThatMoves override?
  4. A subclass' constructor must call a constructor of its superclass, either explicitly (as recommended by our code conventions) or implicitly. Consider the following DudThatMoves constructor:
        public DudThatMoves(BallEnvironment ballEnvironment) {
        }
    
    

    Question: Write a statement that, placed in the above DudThatMoves constructor, would call a Dud's constructor and pass it the ballEnvironment object that is given to the DudThatMoves' constructor.

    Question: Write a statement that, placed in the above DudThatMoves constructor, would call a Dud's constructor and pass it nothing.

    Question: Why would it be a mistake in your project to call a Dud's constructor and pass it nothing?

  5. Question: What methods, if any, must a DudThatMoves have beyond those that override a Dud method?

If any of the above questions are still confusing to you, ask your instructor or an assistant for help NOW -- you will be unable to complete the rest of this exercise without understanding the answers to the above questions.

Implementing DudThatMoves

What you should learn

Resources

Questions to Consider

Some questions/issues to consider throughout this exercise are:

Notes and Reminders

Some notes/reminders from BallWorld's UML class diagram to consider throughout this exercise are:

Overriding Methods

To override a method, simply implement the method in the subclass (here, DudThatMoves).

There are two technical notes for overriding methods.

What you should do

Implementing a DudThatMoves

As you do this part of the exercise:

  1. Keeping in mind the above:

    Augment your BallWorlds project to include a DudThatMoves class that extends Dud.

    A DudThatMoves should behave just like a Dud, except that it moves on its own in a straight line (i.e., a constant velocity).

    • It can move at any reasonable speed you choose -- try numbers like 0.1 for starters.
    • It can start wherever you wish, with any reasonable size and color.
      • Its color should be different from colors of the Ball types that you previously implemented.

Wrapping Up

What you should do

Summary

Perhaps the most important ideas that you saw in Part 4 of BallWorlds are:

  • How to extend a class
  • How to override a method
  • The meaning of the keyword super
  • The difference between a class and an instance of a class
  1. Question: How many minutes did it actually take you to complete this part of this lab?
  2. Question: Compute the ratio of the time you ACTUALLY TOOK to complete this part of this lab to the time you ESTIMATED that you would take. Choose the ratio in the report that is closest to your ratio.
  3. Commit your changes to your SVN repository, being sure that any new files are added.
  4. Submit your report for this part of BallWorlds.