Lab Exercise for Assignment 3

COMP112 2020 Tri 1: Lab Exercise 3

Goals

This assignment will give you practice at writing methods with:
  • methods that call methods in the same class
  • methods that return values
  • for loops

Preparation

Download the zip file for lab exercise 3 and extract it to the COMP112-2020T1-Lab3 folder in your home folder. It should contain templates for the programs you are to complete for the lab exercise part only. Read through the exercises.

Look at the model answers to assignment 2, and make sure you understand all the components of the programs. Also, go over the code examples from the lectures that used for loops.

Summary

Exercise Series 1: Programming with for loops

For the second series of exercises, you must complete methods that use for loops to repeat actions.

Complete the following methods in the ForeachLoopsExercise.java file.

  • squareNumbers() should read a sequence of numbers, using UI.askNumbers(), then print out the squares of each number.
    For example, if they enter
    4 21 20 9 12 done
    it should print
    16 441 400 81 144
    (either on one line or on separate lines). If you select "Set Input" on the UI frame's MENU then choose the file numbers.txt, the UI will automatically input the data from that file. This may help with testing your program.

  • addBigNumbers() should read a sequence of numbers, using UI.askNumbers(), then print the sum of all the numbers that are bigger than 9.
    For example, if they enter
    4 21 20 9 12 done
    it should add up 21+20+12 and print
    53.
    You can use the file numbers.txt to input data automatically.

  • printTHWords() should read a sequence of words, using UI.askStrings(), then print out all the words containing "th".
    For example, if they enter
    Mathilde is the happiest person other than Matthew
    it should print out
    Mathilde the other than Matthew.
    You can use the file words.txt to input data automatically.

  • drawDots() should read a sequence of x coordinates, using UI.askNumbers(), then draw dots (10 pixels diameter) centered at each x coordinate, as long as it is between 0 and 300. You can use the file for-drawing-dots.txt to input data automatically.

Exercise Series 2: Defining and Using Methods with parameters

The first series of exercises is given in the MethodParametersExercise class. It contains three methods that each perform some repetitive task. The repetitive component is done by calling a separate method several times (with different arguments each time).

  • doNameTags() 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.

  • printBottleSong() 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,

  • drawSignalZeroFlag() draws the flag used by the US navy to signal the value "zero", which is 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.