Assignment 3

COMP112 2020 Tri 1: Assignment 3

  • Due Fri 8 May 11:59pm

Goals

This assignment will give you practice at writing programs with:
  • methods that call methods in the same class
  • methods that return values
  • for loops

Preparation

Download the zip file for assignment 3 and extract it to the COMP112-2020T1-Assig3 folder in your home folder. It should contain templates for the programs you are to complete.

Read through the whole assignment, and run the demos so that you know what you need to do.

Look at the model answers to assignment 2, and make sure you understand all the components of the programs.

Summary

Overview

The assignment involves two programs to submit:
  • The first program involves writing methods with loops to analyses lists of numbers and methods that return values.
  • The second program involves designing a set of three methods with parameters to draw complex shapes.

This assignment will contribute 12% of your final grade. Each part of the assignment will be marked out of 100, as for assignment 2. Your mark will be returned electronically, along with feedback comments from the markers.

To Submit

  • Your version of PollutionAnalyser.java
  • Your version of StreetDrawer.java
  • Your Reflection.txt file

Program 1: Pollution Analyser

Construct a PollutionAnalyser program with several methods for analysing a list of readings of air pollution levels from an experiment over the course of a day.

View the PollutionAnalyser demo video (on the AssignmentVideos page) to see what the methods should do.

There are several things about the pollution levels that a user may be interested in:
  • The average pollution level.
  • How the pollution rose and fell.
  • The maximum and the minimum pollution levels during the day.
  • How much the pollution level went over the danger threshold.

The analyse() method reads a sequence of levels from the user, using the UI.askNumbers() method. The sequence of levels is terminated by the word "done". analyse() then calls 6 methods which each do one part of the analysis.

Core:
  • Complete the printAverageLevel method so that it computes and prints out the average of the pollution levels.
  • Complete the printDangerousLevels method so that it prints out all the values where the pollution level is above the danger threshold (120 ppb).
  • Complete the plotLevels method so that it plots the bars in the graphics pane.
    All the pollution levels should be positive. For the core, it doesn't matter if a bar goes over the top of the window.

Testing:
  • There is a simpleTest method which will test your methods on the example data below. It will help you get your code working, but is not a complete test.
  • You need to also use analyse to enter a variety of different sets of numbers to check that your program works correctly in lots of different cases.
  • If you select "Set Input" on the UI frame's MENU then choose the file pollutionlevels.txt, then analyse will automatically input the data from that file instead of the user. This has three sequences of levels to help with testing your program. You can write your own test files if you like.

Completion:

  • Extend the plotLevels method to draw pollution levels over the danger threshold in red, and lower levels in blue, and also draws a horizontal line at the danger level.
  • Extend the plotLevels method so that it handles pollution levels over the maximum value (400) by plotting a bar of the maximum size and putting an asterisk ("*") above it to show that it has been cut off.
  • Complete the maximumLevel method so that it finds and returns the maximum levels in the list.
  • Complete the minimumLevel method so that it finds and returns the minimum levels in the list.
  • Complete the reportDangerPeriod method so that it calculates if the pollution level was above the danger threshold continuously for at least half (>=50%) of the readings, and reports what fraction of the readings were in that period.

Challenge:

  • Extend the plotLevels method to draw both an x-axis and a y-axis, and put labels on them, roughly every 50 pixels.
  • Extend the plotLevels method to use the maximum pollution level to scale the y-axis so that the maximum level would reach just to the top of the graph.
  • Extend the plotLevels method to use the number of pollution levels to scale the x-axis so that all the bars fit in the window.
  • Add a medianLevel method that will compute and return the median level in the list, and extend analyse() to report the median.
  • Extend the plotLevels method to show the mean, maximum, minimum and median on the plot

Here is an example result of the analyse() method
Enter levels, end with 'done': 91 149 167 84 158 185 196 182 125 90  done
 -------Analysis-------
Average level was 142.7 
149 is dangerous!
167 is dangerous!
158 is dangerous!
185 is dangerous!
196 is dangerous!
182 is dangerous!
125 is dangerous!
70% of the readings were dangerous levels

Maximum level was:  196 ppb
Minimum level was:  84 ppb
Longest period continuously dangerous was 5 readings (50%)

Hints:
The average of a set of numbers is the sum of the numbers, divided by the number of levels.

To find the minimum and the maximum, your loop will need to keep track of the smallest and largest levels seen so far.

You may want to use the values Integer.MAX_VALUE and Integer.MIN_VALUE which are the largest and smallest integer numbers that Java recognises.

When designing and constructing a program, it is usually a good idea to get the basic part of the program working before adding the extra features. It would be a good idea to first make the analyse method just read the levels and print out the average, and let the other methods print a message. Once you have that working, you can extend the program to also deal with the plotting, the minimum, and the maximum.

Program 2: Street Drawer

Some computer games include the ability to create their gameworld programmatically so that rather than just using predefined images constructed by an artist, the game uses images constructed by code.

World construction code can be very complex; for this question, you are to make a very simplistic world construction program for a game set in an urban environment.

Construct a StreetDrawer program that will draw a street of houses, where each house has several windows. The houses can be as simple as the following diagram.

StreetDrawer-output.png

You can view the StreetDrawer demo video (on the AssignmentVideos page) to see what the program should do.

You can choose what your houses look like, and how they are placed, as long as:
  • The street has at least four houses (there are eight in the diagram)
  • All the houses on the street are identical (or at least similar).
  • Every house has at least three windows (they have four in the diagram)
  • All the windows are the same (or at least similar).

Unlike most assignments where we give you all the method headers, for this program we have given you an almost empty template. It has a StreetDrawer class with a setupGUI method, but nothing else.

You need to write at least three methods:
  • a method to draw a window at a given position and size.
  • a method to draw a house at a given position, which calls the window drawing method at least three times to draw the windows.
  • a drawStreet(  ) method which should call the house drawing method at least four times to draw the houses.

You have to work out names and parameters of the methods, and the arguments of the method calls.

Note:
  • You will get few marks if your program has a large amount of repeated code.
  • You should use named constants and variables where appropriate
  • Each method must have a documentation comment, even if it is brief.

Core:
  • Define the three methods to draw a basic street of houses.

Completion:

  • Give the methods more parameters so that the houses and windows can be different sizes.
  • Make the houses more interesting (eg, put curtains on the windows, or have more than one window on each level....)

Challenge:

  • Enable the user to control the output, eg,
    • to change the number of houses on the street,
    • to change the number of floors in each house,
    • to change the number of windows on each floor. Note, this should involve additional parameters on the methods.
  • Add some randomness to the construction, so that there are random variations in each house, and each time the drawStreet method is called, the program will create a different looking street. (This kind of variation is critical for good automated world-construction).

Final Part: Reflection

Answer the following question in the Reflection.txt file, and make sure you submit it. Please note that you only need to write answers as plain text and you do not need to write any java files.

Note: Your answer can use UI.sleep(100); to make the program wait for 100 mS (milliseconds).

Suppose the Taxi class represents taxis for a game set in an urban world. Taxis can drive along roads, change their direction, and can pickup and drop off passengers. The class has the following constructor and methods:
  • public Taxi(int maxPassengers)
    Constructor for creating a new Taxi with the given number of seats.

  • public void drive()
    Start driving in the current direction.

  • public void changeDir(double dir)
    Changes the direction of the SpaceShip by the specified amount (in degrees, positive to the left)

  • public void pickup(int passengers)
    Stop the Taxi and pickup the specified number of passengers.

  • public void dropoff()
    Stop the Taxi and drop off all the current passengers.

Write a method called testTaxi that will first create two new Taxi's, one that can hold 3 passengers and one that can hold 8 passengers. It should then
  • make both taxis start driving,
  • after 500 mS, make the smaller taxi turn 90 degrees to the left
  • after a further 200 mS, make the larger taxi turn 45 degrees to the right
  • after a further 2000 mS, make the smaller taxi pickup 2 passengers, then reverse direction and start driving again
  • after a further 1000 mS, make the larger taxi pickup 5 passengers, and start driving again
  • after a further 10000 mS, make the smaller taxi pickup 1 more passenger, turn right, and start driving again,
  • after a further 5000 mS, make the larger taxi drop off all its passengers.