Assignment 5

COMP102/112 2022 Tri 1: Assignment 5

days Due 28 Apr 10am


This assignment will give you more experience in writing programs that read data from a file and do something with the data. It will also give you more practice using conditionals and loops.


Download the zip file for assignment 5 and extract it to the COMP-102-112-2022T1-Assig5 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 and run or view the demo so that you know what you need to do.

Later when the model answer to assignment 4 becomes available, make sure you understand all the components of the programs.

Also, go over the code examples from the lectures that read files.


  • Core:
  • Completion and Challenge
    • Weather Data: right Write a program to analyse weather data from files of weather-station measurements.

To Submit

Your version of
  • (Core question)
  • (Completion and Challenge questions)


Graph Plotter

In Assignment 4, 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 (one number per line) and plot the numbers using +'s for each point.

You can run the GraphPlotter demo program in the demonstrations folder to see what the program should do. You can also view the demo video.

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

Completion and Challenge

Weather Reporter

Television and internet weather reports provide graphical visualisations and reports of weather data, using data about temperatures, pressures, windspeed, etc from a weather database. For this assignment, you are to write a program that produces a graphical visualisation and a simple report of some New Zealand weather data stored in files.

You can run the WeatherReporter demo program in the demonstrations folder to see what the program should do. You can also view the demo video.

The weather data (extracted from online MetOffice weather data from 24-26 January 2021) consists of measurements from weather stations around NZ at every three hours over the three day period. Each weather station reports measurements of temperature, dew-point, surface pressure, and sea-level pressure.

The format of the data files consists of a series of "snapshots" of the data over the country. Each snapshot has
  • a header line with the date and time of the snapshot (four integers for day, month, year, and time)
  • a second header line with the number of weather-stations with reported measurements in the snapshot (which varies with the snapshot)
  • a series of lines of data for each weather station. Each line has
    • station name: one token, eg "Cape-Reinga"
    • (x, y) coordinates on the map: two numbers, eg 186 38
    • four numbers for temperature, dew-point, suface-pressure, and sea-level-pressure

Some of the data files (eg hot-weather.txt, and cold-weather.txt) have data for just one snapshot; the weather-all.txt file has data for 15 snapshots (in date/time order).

You should look at the data files before trying to write your program.


plotTemperatures() should plot the temperatures for the first snapshot in a file on a map of New Zealand. For each weather station in the snapshot, it should draw a small circle on the map at the location of the weather station. The colour of the circle should show the temperature (colder temperatures should be more blue and hotter temperatures should be more red).

It is good design to break this into two methods: plotTemperatures() and plotSnapshot(...).

  • plotTemperatures() should
    • Ask for the name of the weather data file and open a Scanner on the file
    • Call plotSnapshot, passing the Scanner as an argument, to plot one snapshot from the file.

  • plotSnapshot(...) should:
    • Draw the map (in the file "map-new-zealand.gif")
    • Read the date and time from the first header line and draw the date/time at the top-left of the map.
    • Read the number of stations from the second header line, then
    • For each station,
      • Read the name, coordinates, and data, and
      • Plot the temperature for that station at the coordinates

Hint: You will find the getTemperatureColor(...) method useful to work out the right color for a given temperature.

Hint: You should use the constants LEFT_TEXT, TOP_TEXT, for drawing the date/time and the constant DIAM for drawing the circles.


  • Extend plotShapshot(...) so that it also identifies the highest and lowest temperatures from the weather stations in the snapshot and plots those temperatures with a larger circle (double the size of the other temperatures).

  • Complete the animateTemperatures() method so that it displays an animated view of all the snapshots in a file, displaying each snapshot in turn, pausing briefly (half a second) between each snapshot.
    If you reuse plotSnapshot(...), then animateTemperatures() should be very simple.
    Note: since there is only one data file with multiple snapshots, your method can read from weather-all.txt without asking the user for the name of the file.

  • Complete the reportStation() method to print a table of all the weather data from a single station, one line for each snapshot in the file. The method should:
    • Ask for the name of the station.
    • Print a header for the table
    • Then for each line of data for that station in the weather-all.txt file, it prints a line in the table with the date/time, temperature, dew-point, surface-pressure, and sea-level-pressure
    • If there are no entries for that station, it will print a message saying "Station not found".

To go further

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

  1. When do we call a method on this, and when do we call a method on something else, such as UI or a String?
  2. Dealing with files that have header lines is different from dealing with files without header lines. What approaches do you use to design methods that work on the different kinds of files?