Lab Exercise for Assignment 3

COMP102/112 2022 Tri 1: Lab Exercise 3

Goals

This assignment will give you practice at writing programs that:
  • create new objects and call methods on them
  • use methods that call methods in the same class

Preparation

Download the zip file for lab exercise 3 and extract it to the COMP-102-112-2022T1-Lab3 folder in your home folder. It should contain templates for the programs you are to complete for the lab exercise part only.

You can run the demo programs on the ECS lab computers (they are in the COMP102 folder on your desktop). Note that the demo programs cannot be run outside of the labs (since that would make the answers available), but you can view the demo videos.

Later when the model answer to assignment 2 becomes available, make sure you understand all the components of the programs.

Summary

Exercise Series 1: New Objects exercise

The first series of exercises is given in the NewObjectsExercise class. It contains three methods that create new objects and then call a sequence of methods on them. You need to complete these methods. Each method creates objects from a different class. The other "object classes" (Flower , Balloon, and Car ) are all provided and you do not need to modify them.

  • If you are using a computer in a lab, you can run the NewObjectsExercise demo to see what your program should do (look in the assig3-demos folder). Note that your methods should do similar things, but don't have to be identical.
  • Or, you can watch the demo video.

The file NewObjectsExercise.java contains the following methods to complete. (Remember, you do not need to complete all of them - you may stop when you have completed one of them by yourself.)

  • growFlowers() should create two or three Flower objects and make them grow and bloom.
  • blowUpBalloons() should create two Balloon objects and make them expand several times and pop.
  • driveCars() should create two Car objects and make them drive towards each other then turn to the side and drive some more.

When you write the statements to create new objects and call methods on them, you will need to provide the correct arguments to the constructor and to the methods. To see the details of what those arguments are, you should open up the Flower , Balloon and Car classes in BlueJ and select the Documentation option (instead of Source Code ) at the top right. You will then see, for example, that the Flower constructor requires two double arguments - the position of the Flower.

Exercise Series 2: Defining and Using Methods with parameters

The MethodParametersExercise illustrates how a method can perform some repetitive task by calling a separate method several times (with different arguments each time). The MethodParametersExercise folder contains three classes that each have a method that calls a separate method. There are three classes rather than having one single file containing three methods, so that you can work on each part separately without worrying about the parts of the code that do not compile yet.

  • If you are using a computer in a lab, you can run the MethodParametersExercise demo to see what your program should do (look in the assig3-demos folder).
  • Or, you can watch the demo video.

The three classes in the MethodParametersExercise folder are:

  • NameTags: it contains a doNameTags() that prints out a series of nametags for workshop participants. Each nametag has the participant's name and workshop ID number.
    You must complete the printNameTag method including defining the parameters.

  • BottleSong: it contains a printBottleSong() method that prints out the words to a traditional repetitive song.
    You must complete the bottleSongVerse method including defining the parameters.
10 green bottles, hanging on the wall,
10 green bottles, hanging on the wall,
And if one green bottle, should accidentally fall,
There'd be 9 green bottles, hanging on the wall,

9 green bottles, hanging on the wall,
9 green bottles, hanging on the wall,
And if one green bottle, should accidentally fall,
There'd be 8 green bottles, hanging on the wall,

...

2 green bottles, hanging on the wall,
2 green bottles, hanging on the wall,
And if one green bottle, should accidentally fall,
There'd be 1 green bottles, hanging on the wall,

  • SignalZeroFlag: it contains a drawSignalZeroFlag() method that draws the flag used by the US navy to signal the value "zero" (a white square with five small blue crosses, laid out like the dots on the "5" side of ordinary dice).
    You must complete the drawCross method including defining the parameters.
signal zero flag usnavy-num0.gif
instructions to
draw one cross
one cross

For each method (printNameTag, bottleSongVerse, drawCross)
  • Define the parameters of the method.
  • Define the body of the method, using the parameters appropriately.