Assignment 4

COMP112 2020 Tri 1: Assignment 4

  • Due 14 May 10am

Make sure you have BlueJ version 4.2.2 and Java version 11! This assignment won't work with the old BlueJ and Java 8

Goals

This assignment will give you experience in writing programs that get input from files and write output to files. It will also give you more practice using conditionals and loops.

Preparation

Download the zip file for assignment 4 and extract it to the COMP112-2020T1-Assig4 folder in your home folder. It should contain templates for the Java program you are to complete, along with data files. Read through the whole assignment to see what you need to do.

Look at the model answers to assignment 3, and make sure you understand all the components of the programs. Also, go over the code examples from the lectures that used files.

Summary

  • Dealing with files (90%)
    • Core:
      • GraphPlotter: right Write a program to plot a file of numbers
      • WordSearcher: right Write a program to search for words in a dictionary that match a given pattern.
    • Completion
      • SalesVisualiser:right Write a program to plot sales data from a file.
    • Challenge
      • AnimalStory: right Write a program to follow a script to make an animated story
  • Understanding Java. (10%)
    right Answer some short questions about Java.

To Submit

  • Your version of GraphPlotter.java
  • Your version of WordSearcher.java
  • Your version of SalesVisualiser.java
  • Your version of AnimalStory.java
  • Your UnderstandingJava.txt file

Overview

The assignment involves a series of short programs that process data from files. The programs build on the methods you wrote in the lab exercise. The core programs work on files of numbers and files of words. The completion program works on files with one item per line.


Core

Graph Plotter

In Assignment 3, you wrote a program to read a sequence of numbers from the user, and then plot them as a bar graph. For this assignment, you are to write a program to read a sequence of numbers from a file and plot the numbers using +'s for each point.

The program should ask the user for the name of a file, then draw the graph: two axes and +'s for each number.
Note: to draw a + at (x,y), draw a line from (x-2,y) to (x+2,y) and a line from (x,y-2) to (x,y+2)

The points should be spread out evenly to fit the graph (if there are lots of numbers in the file, the +'s will be very close; if there are only a few numbers in the file, they will be spread out.

Here is an example of the graph of the numbers in the file numbers1.txt.

plot of numbers1.txt


Word Searcher

The WordSearcher program finds words in a dictionary that contain a pattern specified by the user. This might be useful for crosswords or for playing Scrabble.

The dictionary is in the file dictionary.txt, which contains just over 200,000 English words.

Your program should ask the user for a pattern and then find and print out the words in the dictionary that contain that pattern. At the end, it will print a count of how many words contained the pattern. If there are more than 100 words, it will stop printing them out after the first 100, but it still counts and prints the total number of matches

Here is an example of running the program when the user enters the pattern bana:

Pattern to search for: bana
banados
banal
banales
banalities
banality
banally
banana
bananal
bananaland
bananas
bananna
bananza
cabana
cabanas
habana
la-habana
tabana
verbana

Total matching words = 18


Completion

Sales Visualiser

The SalesVisualiser program asks the user for a file with details of sales reported by dealers over the last three years, and then produces a bar graph of the data, showing the sales for each month, with different color bars for each year.

Each line of a sales data file contains
  • a year, (2017, 2018, or 2019)
  • a month, (01...12) and
  • a series of sales from the dealers

There may be a different number of sales on each line.
An example sales data file is shown below.
Note that the lines are not necessarily in date order.

Your program should draw a bar graph with 12 sets of bars, one set for each month. Each set should have
  • a red bar for the 2017 data,
  • a green bar for the 2018 data, and
  • a blue bar for the 2019 data
The height of the bar should be the total number of sales in that month in that year. The axis should be labelled.

Here is an example plot of the data in the file sales-data.txt shown at the end of the assignment:

plot of sales-data.txt


Challenge

The challenge question is an extension of the PetStory program from Assignment 2.

Complete the storyFromFile() method in AnimalStory.java so that it reads a script for an animated story from a file, and then follows the instructions in the script to animate a "story" involving at least two Animal characters.

See simple-script.txt (shown below) of a script file.

Each line of a script file is an instruction. These are the possible instructions and what the program should do in response to the instruction.
  • CHARACTER name type :
    Creates a new Animal with the given name and type, but holds them off-screen.
    type can be any of bird, dinosaur, dog, grasshopper, snake, tiger, turtle.
    This instruction must occur before any other instructions involving the animal
    It is OK to assume that all the CHARACTER instructions are at the beginning of the script.

  • ENTER name x y :
    Make the animal with that name appear on the screen at the position x, y

  • GO name direction distance :
    Turn the animal with that name to the direction (left or right) and move by distance

  • TELEPORT name x y :
    Teleport the animal with that name to the new position

  • EXIT name :
    Make the animal with that name disappear from the screen

  • JUMP name height :
    Make the animal with that name jump by the height

  • SPEAK name words :
    Make the animal with that name speak the words (can be multiple tokens)

  • THINK/SHOUT/INTRODUCE words :
    All three instructions work just like SPEAK.

  • CAPTION words :
    Display the words as a caption at the top of the window.

Extra instructions (for top marks)

  • ASK question words
    .....
    ELSE
    .....
    ENDASK :
    Ask the user a question and follow two different paths in the story depending on the answer:
    the instructions between ASK and ELSE if they said "yes"
    the instructions between ELSE and ENDASK if they said "no"
You do not have to handle ASK instructions inside other ASK instructions, but you can if you want! (you might need to label the ASK/ELSE/ENDASK instructions!

  • REPEAT number
    .....
    ENDREPEAT :
    Repeat the instructions between REPEAT and ENDREPEAT number times.


Understanding Java

It is important to understand how Java works, as well as writing programs in Java.

Write answers to the following questions about Java in the UnderstandingJava.txt file, and make sure you submit it.

These questions are worth 10% of the assignment (only 1.2% of your final grade)

  1. What is a variable? Give an example.
  2. What is a literal string? Give an example.
  3. Describe the differences between a variable and a literal string?
  4. Defining and calling methods: What is an argument? What is a parameter? What does it mean when we say "arguments and parameters must match"?


sales-data.txt

2017 01 21 15 32 12 2 7
2017 02 5 18 12
2017 04	  2  13   6   
2017 05	 12  16  15  11  15   2   9
2017 06	  2  19   4  19   7  19
2017 03  19   2  17  11   3
2017 07	 12   3  18   7  
2017 08	  5   7   4  
2017 12	 14  10   3   5   6   8  20   7  10  
2017 09	 17   3  16  18  19  10   6  10  
2017 10	  5   3  12   4  12  15  
2017 11	 18  19  16   2   
2018 01   5   5   4  11
2018 02  41   3
2018 03  16   3   2   4   1   8   4  13
2018 04  13   6  17  19    2  10  20
2018 05  10  13   9   16   3   8   7  12  15
2018 06   3   6  18 
2018 07  15  18  15  12   9  15 
2018 09   5  15  11  19   2   1  10  
2018 10  12  15   4   6  14  12   
2018 12   8  13  18   6   
2019 01  19  19   3  10  20  20  18  14   3  
2018 08  11  19   
2018 11   9   8   9   4  20  20  19   3
2019 02   7  17   9  10  14   6  14  13  
2019 03  17   3  12  14   5   7  20  17   1  13
2019 04  14  17   4  14  
2019 07   5  10  
2019 06  16  14  13   8  20  19   2
2019 05   6  15  16   3  12
2019 08   1  15   8  18  16  19
2019 09   5   1   5  11  16  13   7  11   6  20
2019 10  14  17  12  12   8   8   5   9  10
2019 12   8   6  13  16   4  17  10
2019 11  10   2  11   8  17   8   6  18  15   1

simple-script.txt

CHARACTER Berthie turtle
CHARACTER Charlie dog
ENTER Berthie 520 80
INTRODUCE Berthie Hello
SPEAK Berthie In this demo, I will
SPEAK Berthie show you what we can do
ENTER Charlie 200 200
GO Charlie right 0
JUMP Charlie 40
JUMP Charlie 40
THINK Berthie These dogs like jumping
JUMP Charlie 40
JUMP Charlie 40
SPEAK Berthie Please Charlie
SPEAK Berthie introduce yourself
INTRODUCE Charlie Rrowff!
EXIT Charlie
SPEAK Berthie That's all!
EXIT Berthie