Assignment 2

COMP112 2020 Tri 1: Assignment 2

  • Due 30 Apr 10am


After completing this assignment, you will be able to write Java programs that:
  • use conditionals (if statements)
  • create new objects and call methods on them


Download the zip file for assignment 2 and extract it to the COMP112-2020T1-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 COMP112 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.

Look at the model answers to assignment 1 and make sure you understand all the components of the programs. If you wrote your programs 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 program
  • Your program
  • Your Reflection.txt file

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 three parts to the assignment:
  • Key Validator:
    right Write a program to check whether a given key is valid or invalid.
  • Pet Show:
    right Write a program to create animals figures and animate them.
  • Reflection.
    right Write up your reflections on this assignment.

Program 1: 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(), startsWith(...), 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 7 characters long, AND at most 20 characters long, AND
  2. The key must not start with the special characters '#' or '_', AND
  3. The key must not have a space character anywhere.

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

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 7 characters long, AND at most 20 characters long, AND
  2. The key must not start with the special characters '#' or '_', AND
  3. The key must not have a space 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 contain the user's name, AND
  6. The key must contain either a '#' or a '_', but not both.

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 any "prefix" of the user's name of 2 characters or more in any case.
    Eg, if name is "Peter", the key does not contain "Pe", or "pE" or "Pet", or "pete" or "PEtE", or...

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

Program 2: Pet Show

The PetShow program should tell short animated stories using Animal objects. The program is like the Butterflies program in the lectures, and will create new Animal objects and call methods on them to make them "talk" and move on the screen. See PetShow demo (or the video for an example of how the animal characters may interact; you should make your own story!

You should also look at the documentation version of to find out the parameters of the constructor and the names and parameters of the methods in the Animal class. (Open in the BlueJ editor and select the "documentation" option, as in the Exercise section.)

For the core version, you will need to complete the animate method in the PetShow class:

  • animate() should create two or more Animal characters and animate them according with a fixed sequence of actions by calling a series of methods on the animals.

The Animal class has already been written for you and includes various methods such as goLeft, goRight, jump, speak etc. Try to make use of most of these methods to make the animals interact. Entertain us!

Note that there are several possible images you can use for your animals. The first argument to the Animal constructor must be the name of an image in the animals folder. There are several images called "bird", "dinosaur", "dog", "grasshopper", "tiger", and "turtle" in the download folder, but you can make your own if you wish. They must be gif files.


For the completion version, you will need to complete the threeAnimalsRoutine() method in the PetShow class:

  • threeAnimalsRoutine should create three Animal objects then make each of them perform the same little "routine" in turn. The routine can be as simple as "introducing itself, going left, jumping, going right, speaking".
  • You should define a method called routine in the PetShow class and your threeAnimalsRoutine method should call the routine method for each of the three animals in turn. You will need to think about what parameter(s) the routine method needs to have.

Note: the routine method has been commented out in the template because it would mess up the compiler. You will need to uncomment it when you fix it.

Make a more complicated routine for the three animals which involves each of the animals doing several different small routines. You should define several methods for each of the different small routine, and you might even have methods that are combinations of simple routines, allowing you to have a quite complicated show altogether. You may even choose to modify the Animal class to make the animals able to do more basic movements.

Final Part: Reflection

Answer the following questions in the Reflection.txt file, and make sure you submit it.

  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. Why did the routine method have to have a parameter (the Animal character parameter)?
  3. 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?