Assignment 3: New objects, random numbers, return values from methods, and while loops

Introduction to Computer Program Design: Assignment 3

days Due 20 Oct 7pm

ALERT! This is a Pass/Fail (P/F) Assignment

Goals

The goal of this assignment is for you to be able to construct programs in Java that
  • create new objects and call methods on them
  • use random numbers
  • return values from methods
  • have while loops

Online part

Start with the online part as it will help you consolidate your knowledge for the programming part.

This part is compulsory and you must pass all the online questions related to this assignment by the deadline to pass this assignment.
The Challenge questions are optional but would help your learning.

right Go to the Online questions now and answer all the Assignment 3 questions.

right There is no 24 hours grace period for these questions.

Programming part

To Submit:

  • PetShow.java
  • Animal.java (if you do the challenge part)

right Do not rename this file. You must use the provided template file and not remove the setupGui and main methods.
right When you have submitted this file, check that you can read the file listed on the submission page, and complete the submission process.

Zip file

PetShow: Pass level

ALERT! You must complete this part even if you want to do the Challenge level.

The PetShow program should tell a short animated story using Animal objects. The story consists of 3 animals (randomly situated), that performs the same "welcome" routine. Then the left and right most animals will compete towards their respective exit (the closest to them). The animation stops when one of them has crossed their respective exit line. The animal that is horizontally in the middle remains still.

pet-show-actions.png

tip You do not have to draw the vertical lines.

You have 4 methods to write
  • closestToLeft which returns the animal closest to the left of the Graphic Pane
  • closestToRight which returns the animal closest to the right of the Graphic Pane
  • routine which makes the animal perform some routine. The routine can be as simple as "introducing itself, going left, jumping, going right, speaking". Try to use a mix of methods from the Animal class. You can be as creative as you want. You will need to think about what parameter(s) the routine method needs to have.
  • animate that animate the whole story.

Instructions and Tips
  • You must use the EXIT_LEFT, EXIT_RIGHT, START_LEFT, and START_RIGHT constants.
  • The 3 animals must be initially randomly placed horizontally between START_LEFT and START_RIGHT.
  • You may choose their vertical position.
  • The animate method must call the routine, closestToLeft, and closestToRight methods with the right parameters.
  • Use a while loop to move the animals towards their goal line.
  • You should also look at the documentation version of Animal.java to find out the parameters of the constructor and the names and parameters of the methods in the Animal class. (Open Animal.java in the BlueJ editor and select the "documentation" option.)

The Animal class has already been written for you and includes various methods such as goLeft, goRight, jump, speak etc.

tip 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", "snake", "tiger", and "turtle" in the download folder, but you can make your own if you wish. They must be gif files. Remember to submit them!

PetShow: Challenge level

Create your own Animal class. It must work like the provided Animal class, except instead of using drawImage, you need to use the UI draw and fill methods (e.g. drawOval, fillRect), similar to the Blinky example in the COMP102 lectures (You can find the code in the COMP102 lecture schedule for week 7).

When the characters move around the screen they should be animated to make it a more interesting show.