Assignment 3: New objects, random numbers, return values from methods, and while loops
Introduction to Computer Program Design: Assignment 3
Due 20 Oct 7pm This is a Pass/Fail (P/F) AssignmentGoals
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
Resources and links
- Online Part: Online questions
- Programming part:
- Download Zip file
- Submit your java files.
- Marks and Feedback.
- Java Documentation
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. Go to the Online questions now and answer all the Assignment 3 questions. There is no 24 hours grace period for these questions.
Programming part
To Submit:
-
PetShow.java
-
Animal.java
(if you do the challenge part)
setupGui
and main
methods.
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
You must complete this part even if you want to do the Challenge level. ThePetShow
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.
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 theAnimal
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.
- You must use the
EXIT_LEFT
,EXIT_RIGHT
,START_LEFT
, andSTART_RIGHT
constants. - The 3 animals must be initially randomly placed horizontally between
START_LEFT
andSTART_RIGHT
. - You may choose their vertical position.
- The
animate
method must call theroutine
,closestToLeft
, andclosestToRight
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 theAnimal
class. (OpenAnimal.java
in the BlueJ editor and select the "documentation" option.)
Animal
class has already been written for you and includes various methods such as goLeft
, goRight
, jump
, speak
etc.
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!