Lab Exercise for Assignment 6

COMP112 2020 Tri 1: Lab Exercise 6

Goals

This assignment will give you experience in defining classes for objects with fields and constructors. It will also give you some examples of programs with simple event-driven input (mouse and buttons).

Preparation

Download the zip file for lab exercise 6 and extract it to the COMP112-2020T1-Lab6 folder in your home folder. It should contain templates for the exercises program. Read through the exercises and run the demo so that you know what you need to do.

Summary

  • Objects exercise:
    right Complete three classes defining three simple kinds of objects.

Objects Exercise

The ObjectsExercise project contains three little programs, each of which has a class defining a different kind of object. You need to complete the classes.

Watch the demo video to see what the programs are supposed to do.

PartyBalloon

The RunPartyBalloons program has a button that calls the flyPartyBalloon method. flyPartyBalloon will make two PartyBalloon objects and make one of them repeatedly rise up to the left and the other repeatedly rise up to the right. This method is written for you; you have to define the PartyBalloon class.

A PartyBalloon object represents a small round balloon drawn on the graphics pane. A PartyBalloon object must remember its current position and its color. Its initial position and its color are set when it is constructed. It has three methods.

You need to
  • declare fields to hold the position and the color.
  • define a constructor that has three parameters for the initial position and the color, stores that information in the fields.
  • define the three methods
    • draw() which draws the balloon in the appropriate color at its current position.
    • riseLeft() which changes the position of the balloon by 20 units up and 5 units to the left.
    • riseRight() which changes the position of the balloon by 20 units up and 5 units to the right.
  • Note: The last two methods do NOT erase or redraw the balloon.

Lamp

The RunLamps program has a button that creates two Lamp objects and sets up the mouse. When the user clicks on the bulb of a lamp, it should change color (which will turn the lamp on first if it is currently off); when the user clicks on the stem of a lamp, it should turn on or off. These methods are written for you; you have to define the Lamp class.

A Lamp object is a simple object that stores information about a graphical representation of a table lamp. It has methods to draw the lamp, to report whether a mouse position (x, y) is on the bulb of the lamp or on the stem of the lamp, and methods that change the state of the lamp (turn it off, and turn it on/change its color).

You need to
  • declare fields to hold the position (xPos, yPos) of the lamp, its current Color, and whether it is on or off.
  • define a constructor that copies its parameters (the position of the lamp) into the fields.
  • define methods:
    • draw, draw the lamp: a filled circle (the bulb) on a dark gray stem.
      If the light is off, the bulb should be black;
      if the light is on, the bulb should be the current color of the lamp.
    • onBulb(x,y), return true if the point (x,y) is on the bulb of the lamp.
    • onStem(x,y), return true if the point (x,y) is on the stem of the lamp (the off switch).
    • turnOff(), turn the light off
    • changeColor(), if the lamp is off, turn the light on; if the light is on, change its color to a random bright colour.

Note Color.getHSBColor((float)(Math.random()), 1.0f, 1.0f) will return a random bright Color.

FlagStaff

The RunFlags program has a button that creates two FlagStaff objects, and then raises and lowers the flags. This program is written for you; you have to define the FlagStaff class.

A FlagStaff object represents a flagstaff with a flag that can be raised or lowered. A FlagStaff is drawn using a vertical line for the flag pole and a rectangle for the flag.

You need to
  • declare fields to store the position of the flagstaff and the height of the flag above the base of the flagstaff.
  • define a constructor to initialise the fields. The flag should start at the bottom.
  • define three methods:
    • redraw which will redraw the flagstaff (a line for the pole and a rectangle for the flag at its current height)
    • raise which should raise the flag by the amount specified in the parameter (except it should never let the flag go over the top of the flagstaff).
    • lower which should lower the flag by the amount specified in the parameter (except it should never let the flag go below the bottom of the flagstaff).