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 7: Shrinker

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 7 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 7 of BallWorlds?
  5. Question: What time is it now? (Later we will ask you how long you spent on Part 7 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 Balls whose classes you have implemented can be created yet by the BallButtons.

Reviewing Bouncer

What you should learn

Resources

The following concepts should be clear to you now:

If not,

What you should do

Reviewing ideas from Bouncer

  1. Question: Explain what the super(xxx) statement does in the following code.
    public void DudThatMoves extends Dud {
    
        public DudThatMoves(BallEnvironment xxx) {
            super(xxx);
            ...
        }
        ...
    }
    
    
  2. Question: Explain what the super.paint(g) statement does in the following code.
    public void BallWorldsFrame extends JFrame {
        ...
    
        public void paint(Graphics g) {
            super.paint(g);
            ...
        }
    }
    
    
  3. Question: Suppose that you already have fields in Mover for the x and y position of the Ball. Should you declare additional fields in Shrinker for the x and y position? Explain.

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

For a class to implement an interface, the class must supply definitions for each of the methods whose signatures appear in the interface. However, nothing prohibits those methods from being stubs -- while comments in the interface may describe the intended behaviors of the methods of the interface, only the form of those methods is enforced.
A class responds to mouse events by implementing the MouseListener and/or MouseMotionListener interfaces.
Reading a UML class diagram should be familiar territory to you by now.

If not,

What you should do

Understanding Shrinker 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: What fields are necessary for a Shrinker to shrink/grow?
  4. Question: Which of the methods in the Bouncer class must a Shrinker override?

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 Shrinker

What you should learn

Resources

Interative Enhancement Plans

When implementing per an iterative enhancement plan, one implements and tests each stage of the plan before continuing to the next stage of the plan.

Questions to Consider

Some questions/issues to consider throughout this exercise are:

Reminders from BallWorld's UML

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

What you should do

Implementing Shrinker

As you do this part of the exercise:

  1. Keeping in mind the above (especially think, don't just copy-and-paste and test each stage before continuing to the next stage):

    Augment your BallWorlds project to include a Shrinker class that extends Bouncer.

    A Shrinker behaves just like a Bouncer, except that as it moves/bounces, it also shrinks and expands, as follows:

    • Stage 1: It picks a random size between 0 and its initial size, and shrinks to that in small increments (through the act method).
    • Stage 2: After it reaches that random size, it picks a random size between its initial size and 4 times its initial size, and grows to that in small increments (through the act method).
    • Stage 3: The process then repeats, picking a new random number for the size to shrink/grow to, at each iteration of shrinking/growth.

    Notes:

    • See your instructor if you have any questions about the above specification.
    • A Shrinker should have a color that is different from colors of the Ball types that you previously implemented.
    • Important: Take full advantage of inheritance.
      • Your Shrinker should inherit whatever it can from Bouncer, e.g.:
        • Moving and bouncing
        • Pausing/resuming (neither moving nor shrinking/growing should occur when the Shrinker is in the paused state)
        • All the mouse interactions
      • This may require that you encapsulate (bundle) some of your Mover and/or Bouncer code into protected, "helper" methods.

Wrapping Up

What you should do

Summary

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

  • How to implement a class from its UML class diagram
  • How to construct new objects
  • How to read the Java API specification of a class and apply what you learn
  • The meaning of the keyword this
  • How to use one object to do work on behalf of another object
  • How to determine what fields are necessary or helpful
  • 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.