Lab Exercise for Assignment 2

COMP102/112 2022 Tri 1: Lab Exercise 2

Goals

After completing this assignment, you will be able to write Java programs that:
  • use methods for drawing
  • use conditionals (if statements)

Preparation

Download the zip file for lab exercise 2 and extract it to the COMP-102-112-2022T1-Lab2 folder in your home folder. It should contain templates for the exercises program. Read through the exercises to see what you need to do. 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 1 becomes available, make sure you understand all the components of the program. If you wrote your program a little differently from the model solution, that's OK as long as your program works but still make sure you understand how the model solution code works.

Summary

There are two parts to the exercises:

Exercise Series 1: Flags.

For the first series of exercises, you must complete methods that draw various simple national flags.

  • If you are using a computer in a lab, the FlagExercise demo (in the assig2-demos folder) shows what they should do.
  • Or, your can watch the video demonstrating what your FlagExercise program should do.

The file FlagExercise.java contains two constants, LEFT and TOP, which specify the position of the flags.

The first method is called drawJapanFlag. It should clear the graphics pane and ask the user for the width of the flag, then calculate the height of the flag, the circle diameter, and the left and top of the circle. Finally, it should draw the flag. It should use the constants LEFT and TOP to work out where the flag is placed.

Japan japan.gif
instructions The Japanese flag

Test your method with at least two different widths.

Write drawIndonesiaFlag to draw the flag of Indonesia.
Indonesia indonesia.gif

Write similar methods (drawAustriaFlag , etc) to draw the flags of the following countries. You may stop once you have written one of the flag methods by yourself.

Austria austria.gif
instructions The Austrian flag

Bangladesh bangladesh.gif Niger niger.gif

Exercise Series 2: Conditionals exercise

The ConditionalsExercise class contains four methods that each involves asking for some information from the user and then performing different actions depending on the answers.

  • If you are using a computer in a lab, the ConditionalsExercise demo (in the assig2-demos folder) shows what they should do.
  • Or, your can watch the video demonstrating what your ConditionalsExercise program should do.

Complete the following methods in the ConditionalsExercise.java file. You should complete the methods, until you have written at least two by yourself. (It may be useful practice to do all of them!)

  • validHour() should ask the user for a number. If the number is out of range (< 0 or > 12), then it reports "that number is not a valid time". Otherwise, it prints the number in the for "The time is 6 o'clock" (if they entered 6).

  • wordGame() should ask the user to enter a word. It then prints "Yes, that fits" if the word starts with "p" and is 7 characters long, otherwise it prints "Sorry, that word won't work".
    Hint: You can call the length() method on a String to find out how many characters long it is, and you can call the startsWith("xyz") method on a String to determine whether the start of the String matches "xyz". (Look up the documentation of those methods in the online java documentation for the String class).

  • drawAShape() should ask the user to enter the name of a shape, and then draws the appropriate shape by calling one of the drawCup, drawMan, drawCat, drawCar, and drawTV methods which are provided. If the shape is not one of cup, man, cat, car, or TV, the program prints "sorry, I don't know about that shape".
    Note: all the shape drawing methods are included in the file for you - you don't have to write them.

  • longestWord() should ask the user to enter three words and print out the longest one. If two of the words are equally long, it doesn't matter which it prints. You can call the length() method on a string to find out how long it is.