Assignment 4: Input, Variables and Expressions, Drawing.

Introduction to Computer Program Design: Assignment 4

days Due 3 Nov 7pm

ALERT! This is a Pass/Fail (P/F) Assignment

Goals

This assignment will give you practice at writing programs that:
  • use conditionals
  • use for loop (foreach loops and for loops)
  • have methods that return values

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.

right Go to the Online questions now and answer all the Assignment 4 questions.

right There is no 24 hours grace period for these questions.

Programming part

Note that for the programming part, you have to complete 2 programs. Each program has a Pass/Fail level that everyone must complete, and an optional Challenge level.

To Submit:

  • RoadProfiler.java
  • PatternsDrawer.java

right Do not rename these files. You must use the provided template files and not remove the setupGui and main methods.
right Remember to submit both of these files. When you have submitted them, check that you can read the files listed on the submission page, and complete the submission process.

Zip file

Program 1: RoadProfiler

RoadProfiler: Pass level

You must complete this part even if you want to do the Challenge level.

You are to complete 3 methods that all participate in analysing and displaying the profile of a section of a road, based on a GPS measurements every 10 meters along the section of road.
  • printAverageHeight method so that it computes and prints out the average height of the road.
  • maximumHeight method so that it finds and returns the maximum height in the list.
  • displayProfile method so that it plots the profile by drawing lines from each measurement point to the next.

Output with input
from heights.txt:
road-profiler-screenshot.png

Instructions and Tips:
  • The average of a set of numbers is the sum of the numbers, divided how many numbers there are.
  • To find the maximum, your loop will need to keep track of the largest height seen so far. You may want to use the value Double.NEGATIVE_INFINITY which is the smallest number that Java recognises.
  • You must use the LEFT, SEA_LEVEL, and STEP constants.
  • The analyseProfile() method (completed for you) reads a sequence of heights from the user, using the UI.askNumbers() method, then calls the 3 methods you need to complete.
  • For testing, you can avoid having to type all the numbers each time by selecting "Set Input" on the UI frame's MENU then choose the file heights.txt. The UI will then automatically input the input from that file instead of waiting for you to type the numbers.

RoadProfiler: Challenge level

  • Extend the displayProfile method so that it
    • draws a y-axis with labels showing the heights.
    • scales the data vertically to fit the window (so that heights above 400 don't go off the graphics pane),
    • scales the display horizontally so that however many numbers there are, they will be spread evenly across the graphics pane.
    • displays in red the portion of the road that has the highest gradient.
  • Add a new method that classifies the road profile:
    • "uphill" if the road height is increasing and never goes down
    • "downhill" if the road height is decreasing and never goes up
    • "flat" if all the heights are within a 3m range.
    • "Dip" if the road starts high, goes low, then goes high again by the far end.
    • "Hill" if the road starts low, goes high, then goes low again by the far end.
    • "Bumpy" if the road goes up and down in a more complicated way (ie, none of the above).

Program 2: PatternsDrawer

PatternsDrawer: Pass level

You must complete this part even if you want to do the Challenge level.

You are to complete 2 methods:
  • drawDraughtsBoard that draws a checkers board - the standard board for chess and checkers/draughts
    checkers.png
    right This needs two nested loops but must sometimes draw a black square and sometimes an white square.
    tip If the row and column are both even, or both odd, then it is white. (Remember %, the 'remainder' operator!)

  • drawTriGrid that draws a triangular grid of squares that makes dark circles appear in the intersections when you look at it.
    illusion.png
    tip This also requires two nested loops, but each row must have a different number of squares.

Instructions:

  • The position and size of the pattern are specified by the three constants PATTERN_LEFT, PATTERN_TOP, and PATTERN_SIZE. You must use these constants.
  • Each method starts by asking the user for a number: the size. The loops should use this number!

PatternsDrawer: Challenge level

  • Hexagonal grid - drawn with lines, to look like a wall of cubes
    hexagonal.png

  • Spiral - a spiral of squares (as in some board games that require movement along a path towards a goal
    spiral.png