Make-up Assignment 1

COMP102/112 2022 Tri 1: Make-up Assignment 1

days Due 3 Jun 10am

ALERT! This assignment is for students who have not submitted more than one of assignments 1 - 5, and are therefore likely to fail the mandatory requirement to submit reasonable attempts at 8 out of the 10 assignments (assignments 1 to 10).

ALERT! The assignment will not be marked, and only has a Core requirement. But submitting a reasonable attempt will help you meet the mandatory requirements. Solutions are not provided.

Goals

The assignment addresses
  • using objects,
  • loops,
  • and reading from files.

Preparation

Download the zip file and extract it to a new folder in your home folder. It should contain templates for the Java program you are to complete. Read through the whole assignment to see what you need to do.

Look at the model answers to PetShow in assignment 3 and make sure you understand all the components of the program.

To Submit

  • Your CartoonAnimator.java program and any script files you created.

Overview

The assignment involves writing a java program that creates an animation using CartoonCharacter objects (similar to PetShow from assignment 3), but reads the script from a file.

CartoonAnimator

The PetShow program in assignment 3 animated a short story, using the Animal class. The PetShow program created some Animal objects and made a sequence of method calls on the objects to make them go right, go left, jump, speak, shout, etc. The problem with the PetShow program is that it only showed one fixed story. If you wanted a different story, you would need to rewrite the program.

A nicer design would allow the script of a story to be stored in a file, and then the program would read the data from the file and animate the story on the screen. That way, the program could display lots of different stories if it were given the scripts of the stories in different files.

  • If you are using a computer in a lab, you can run the CartoonAnimator demo to see what your program should do (look in the assigMakeup1-demos folder).
  • Or, you can watch the demo video.

You are to complete the CartoonAnimator program that reads a script from a file and animates the story. The CartoonAnimator program uses the CartoonCharacter class: it creates CartoonCharacter objects and calls methods on them.

The first two lines of a script file should contain the names (imagePrefix) and initial positions of the two characters. For example, the first two lines of simple-script.txt are
Casey 100 150
Bob 400 150
This specifies that the name of the first character is "Casey", that the character should initially be placed at (100, 150), and that the image files for it have "casey-" has a prefix. Similarly, the second object is named "Bob", initial placement is (400, 150) and uses the image files prefixed with "bob-".

The remaining lines of a script file consist of instructions. Each instruction has
  • the name of a character (Casey or Bob in simple-script.txt; the names may be different in other script files, as long as they match the names on the first two lines of the script file and that there are the four image files prefixed with that name. For example, if the name is "Pat" then the folder must contain four files called pat-left-frown, pat-left-smile, pat-right-frown, and pat-right-smile (they can be jpg files or png files),
  • a command (lookLeft, lookRight, smile, frown, walk, or speak)
  • any arguments the command needs, if any (an integer for walk; text for speak)
For example, the first six instructions in simple-script.txt are
Casey frown
Casey speak hello there,
Bob lookLeft
Bob speak hello
Casey smile
Bob walk 10

The template contains an animateFromFile method. The method should ask the user for a script file (using UIFileChooser ). (Note, for security reasons, the demo always opens the simple-script.txt file.). The method must then read the first two lines of the script, remembering the names of the two characters, and creating two CartoonCharacter objects (using the image file, and the initial positions). The program must then loop through the rest of the script file, reading one instruction at a time and performing each instruction by calling the appropriate method (with any specified arguments) on the appropriate object. For example, given the first instruction of simple-script.txt, the program should call the frown() method on the first CartoonCharacter object (the one that was named "Casey").

You will need a loop to step through all the instructions in the script, and several if statements to select the appropriate object and the appropriate method to call on the object.

We have also provided a second script file called script2.txt with a script for two characters with the names Alice and Bob.

Core
Complete the animateFromFile method so that it plays the script in the file the user selects.

The make-up assignment only requires the core.

If you wanted to do more, you could consider the following:
  • The scripts specified above always introduce both cartoon figures at the beginning. Extend your program in some appropriate way so that the second cartoon figure can be introduced part way through the script. You will need to work out how the script will specify this; one way would be to have an "enter" command that represents a new character.
  • There are good reasons why the Core only had two characters. Extend the program to allow any number of characters who can enter and exit at any time in the script.

The script files:

simple-script.txt :

Casey 100 150
Bob 400 150
Casey frown
Casey speak hello there,
Bob lookLeft
Bob speak hello
Casey smile
Bob walk 10
Bob walk 20
Casey lookLeft
Bob walk 10
Casey frown
Bob walk 20
Bob walk 20
Bob lookRight
Casey walk 40
Bob walk 20

script2.txt:

Alice 100 150
Bob 400 150
Alice lookRight
Bob lookLeft
Alice speak What's your name?
Bob frown
Bob speak What?
Alice frown
Alice walk 10
Alice speak WHAT'S YOUR NAME?
Bob smile
Bob speak Oh, I'm Bob
Alice speak What did you say?
Bob frown
Bob speak What?
Alice walk 10
Alice speak I didn't hear you.
Bob walk 10
Bob speak I said I'M BOB
Alice smile
Alice speak OK, Hello Bob
Bob smile
Bob walk 10
Bob speak What's your name?
Alice frown
Alice speak What did you say?
Bob frown
Bob speak This is too hard
Bob lookRight
Bob walk 20
Alice speak I agree!! way too hard
Alice think I want to go now
Alice lookLeft
Alice walk 20
Bob walk 20
Alice walk 20
Bob walk 20
Alice walk 20
Bob walk 20
Alice walk 20
Bob walk 20
Alice walk 20
Bob walk 20