Assignment 2

COMP102/112 2022 Tri 1: Assignment 2

days Due 24 Mar 10am


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


Download the zip file for assignment 2 and extract it to the COMP-102-112-2022T1-Assig2 folder in your home folder. It should contain templates for the two Java programs you are to complete. Read through the whole assignment 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.

To Submit

  • Your flag drawer program:
  • Your program

Do not rename these files! (If you do, the submission system will complain.)

Remember to submit all of these files. When you have submitted them, check that you can read the files listed on the submission page, and complete the submission process.


There are two parts to the assignment:
  • Flag Drawer:
    right Write a program to draw some more complex flags.
  • Key Validator:
    right Write a program to check whether a given key is valid or invalid.

Program 1: Flag Drawer

  • You can run the FlagDrawer demo to see what your program should do (look in the assig2-demos folder).
  • Or, you can watch the video demonstrating what your FlagDrawer program should do. (Please note that the buttons for the Challenge on the video are not the flags we asked.)


Write methods to draw:
  • The flag of Belgium
  • The flag of the Red Cross

Details for each flag can be found in the comments above the relevant methods in the code, as well as in the table below. For each flag there is a small image showing how it should look (this what your program must draw), and a larger image with instructions showing how big each part should be to assist you in writing the code. Make sure to test your methods with at least two different sizes.

Belgium: The Belgian flag
instructions The Belgium flag

Red Cross: Red Cross
instructions red-cross-instructions

Hint: For any Java color, say, will give a darker shade of red and will give a brighter shade of red.


Write methods for the Pacman flag and the flag of Greenland.

Pacman: pacman.png
Greenland: The flag of Greenland

Hint: You can use drawArc to draw parts of circles.


Write methods for the flag of Jamaica and the three stars flag.

You may have to think outside the box to work out how to draw the flag of Jamaica with the methods provided in the UI class.

Jamaica: jamaica.gif

Getting the stars right on the three stars flag is tricky! You may need to use the documentation to find out more of the drawing actions of the UI in order to do it.

To get full marks, you need to define a method to draw a star at some position and with a specified size. You can then reuse this method for each of the three stars.

Three stars: three-stars.png

Program 2: Key Validator

Many computer-based systems require the user to have some kind of key (a user code, a password, an ID, etc) for access. This is usually some string of characters (letters and/or digits and/or punctuation). It is often important for the key to have some particular format, and many systems will check that a user's key has a valid format. Run the demo in the lab or watch the demo video.

This part of the assignment requires you to write a program that will test whether a key is valid or invalid, according to a set of rules. (Note, these rules are simple, and are made up for this assignment; they are NOT a good set of rules for a password!)

Hint Look carefully at the documentation in the String class. You will find quite a few of the methods to be helpful, especially the length(), endsWith(...), and contains(...) methods.


For the core, your program must check if a key is valid according to these rules:
  1. The key be at least 8 characters long, AND at most 16 characters long, AND
  2. The key must not end with the special characters '#' or '$', AND
  3. The key must not have a hyphen ('-') character anywhere.

Complete the validateKeyCore method so that it checks the key against the rules, then prints out a message:
  • if the key is valid, reports that the key is valid
  • if the key is invalid, reports saying the key is invalid and gives one reason (one rule that it failed),

Note that the validateKeyCore method has one parameter - the key to check. The method should check the value in that parameter against the rules. The method should NOT ask the user to enter the key - that is already done for you by the doCore method.


For the Completion, your program must check if the key is valid according to a larger set of rules, any provide a more complete report. The rules include the core rules above plus some more:
  1. The key be at least 8 characters long, AND at most 16 characters long, AND
  2. The key must not end with the special characters '#' or '$', AND
  3. The key must not have a hyphen ('-') character anywhere, AND
  4. The key must have at least one Upper case character and at least one Lower case character, AND
  5. The key must not start with the same character as the first character of the user's name, AND
  6. The key must contain either a '#' or a '$', but not both. (Note: it is OK to have multiple #'s if there are no $'s, and vice versa.)

Complete the validateKeyCompletion method so that it checks the key against the rules and either
  • reports that the key is valid
  • reports ALL the rules that the key failed.

Note that the validateKeyCompletion method has two parameters - the key to check and the user's name. The method should NOT ask the user to enter the key or the name - that is already done for you by the doCompletion method.


For the challenge, your program must do the same as the completion, except that it must also check these additional rules:
  1. The key must have a mix of numbers and letters (at least one of each)
  2. The key must not contain the user's name spelled backwards, case insensitive.
    Eg, if name is "Peter", the key does not contain "ReTEp", or "RETEP" or "retep", or...

You should define your own doChallenge and validateKeyChallenge methods and add a button to the setupGUI method.

To go further

  1. What is one thing that you did in this assignment that is similar to the previous assignment? Was it easier this time? How did you approach doing it?
  2. When using several "if" statements, you can use a sequence of separate "if" statements or you can combine them into a longer "if... else if .... else if ...." statement. What's the difference? Is one better than the other?