Assignment 10

COMP112 2020 Tri 1: Assignment 10

  • Due 25 Jun 10am
  • Please note all extensions that you got from the course coordinator will finish on 26 June.
  • Any further extension beyond 26 June must be approved by the Associate Dean.

Goals

  • The goal of this assignment is to assess what you have learned in the course, so we will not provide any help for completing the questions. Please note
    • All Labs/Tutorials/HelpDesks/Workshop stop from this Friday 19 June.
    • Email online help is still open till 26 June, but we can only answer clarification questions.

Preparation

Download the zip file for assignment 10 and extract it to your home folder. It should contain templates for the Java programs you are to complete. Read through the whole assignment to see what you need to do. We do not have a demo video for this assignment. You may email xgao@ecs.vuw.ac.nz to ask for any clarification.

Overview

This assignment has five questions and covers the important topics we have learned in the course, including programming with conditionals, loops, files, arrays and ArrayLists.

To Submit

  • Your versions of Question1.java, Question2.java, Question3.java, Question4.java, and Question5.java

This assignment will be marked automatically. You must make sure that all files can compile. You are not allowed to change any method headers.

Question 1: Conditionals.

Complete the calculateFine method [7 marks]

If you are caught driving over the speed limit, the amount of your infringement fine will depend on how far over the speed limit you are going.

 How far over the speed limit   Infringement fine
 10km/h or less                   $30
 11-15km/h                        $80
 16 or more                       $150 

calculateFine(speedLimit, speed) should return the amount of the infringement fine for speeding. It has two parameters: the speed limit of a road and the speed of a car.

Complete the setAlarm method [8 marks]

The setAlarm(day, holiday, sick) method should return the hour that an alarm should be set. It has three parameters: an integer to specify the day of the week (1-5 for Mon-Fri; 6-7 for Sat-Sun), and two booleans for whether it is a holiday and whether you are sick.
  • Your alarm should normally be set to 7am during weekdays and 9am during weekends.
  • But if it is a holiday, you sleep in and the alarm should be set to 10am on weekdays and 11am on weekends.
  • However, if you are sick, you do not set the alarm at all, whatever day it is, and the method should return -1.

Question 2: Loops.

Complete the findMin method [7 marks]

findMin(numbers) should return the minimum number in an ArrayList of numbers specified in the parameter.

Complete the removeMin method [8 marks]

removeMin(numbers, min) should delete all occurrences of the number in min from the ArrayList in numbers, except it must not delete all the values from numbers - there should be at least one value left in the list.

For example,

if numbers holds {2.1, 3.0, 4.5, 2.1, 2.1, 2.1}, then removeMin(numbers, 2.1) should reduce numbers to {3.0, 4.5}

if numbers holds {3.2, 3.2, 3.2, 3.2}, then removeMin(numbers, 3.2) should reduce numbers to {3.2}

Question 3: Files [20 marks]

Suppose you have a text file with multiple lines where each line may have multiple words. You may assume that the file consists entirely of words, except for "." and "," characters, which are used as punctuation at the end of some words.

Complete the findLongestWord() method that will read all the words from the file specified by the parameter, and then find and return the longest word. If there are two or more equally longest words, it should return the last one. It should not count the punctuation.

For example, if the testfile.txt file contained the following,
 This course introduces programming fundamentals, using a high level 
 programming language called Java using an object oriented approach to program 
 design. Students develop their programming skills by constructing 
 computer programs for a variety of problems. The course provides a 
 foundation for all later courses in computer science, and develops 
 programming skills useful for students making applications in many other disciplines.
your method should return "applications" which has 12 letters. It should not return fundamentals or constructing, even though they also have 12 letters, because applications comes last.

Question 4: Arrays and ArrayLists

This question asks you to write four methods for a program that calculates ticket fares for a Wellington train line.

The following are the cash fares for Wellington trains based on the number of zones in the trip:
$2.50, $4.00, $5.00, $5.50, $6.50, $8.50, $9.50, $10.50, $12.00, $13.00, $15.00, $16.00, $17.50, $19.00
For example, passengers pay $2.50 to travel within 1 zone, $4.00 to travel between 2 adjacent zones, and $5.50 if the trip covers 4 zones, e.g. from zone 5 to zone 8. This data is stored in an array in the fares field.

Complete the findFare method [7 marks]

findFare(zone1, zone2) should return the correct fare for a trip from zone1 to zone2 (which might be the same zone).

For example:
  • findFare(10,10) should return 2.5 and
  • findFare(5, 8) should return 5.5.

Note that the trip can go from a larger zone number to a smaller zone number, e.g. from zone 8 to zone 3, and the fares can not be negative.

Complete the loadStations method [8 marks]

The file kpl-stations.txt contains the names and zone number of all the stations on the KPL line (the Kapiti line from Wellington to Waikanae). The file is listed below and shows, for example, that the "Wellington" station is in zone 1 and the "Porirua" station is in zone 5.
        Wellington 1
        TakapuRoad 4
        Redwood 4
        Tawa 4
        Linden 4
        Kenepuru 5
        Porirua 5
        Paremata 5
        Mana 6
        Plimmerton 6
        PukeruaBay 7
        Paekakariki 8
        Paraparaumu 9
        Waikanae 10

loadStations(filename) should read data from a data file of stations (such as "kpl-stations.txt") into the ArrayList in the stations field.
  • The stations field contains an ArrayList of Station objects.
  • A Station object (specified in the Station.java file, which is written for you) stores information about a station: its name and zone number. Both Station.java and kpl-stations.txt are included in the zip file.

Complete the findZone method [7 marks]

findZone(stationName) should look up the name of a station in the list of stations, and return the zone number of the station. If there is no station of that name in the list, findZone should return -1.

Complete the ticketFare method [8 marks]

ticketFare(start, dest) should calculate and return the fare to travel between the starting station and destination station, specified in the two parameters. For example, ticketFare("Porirua", "Waikanae") should return 8.5. If either of the station names is not in the list of stations, the method should return 0.0.

Question 5: A line-breaking algorithm [20 marks]

Complete the printText(text) method that will print the String in its parameter in a series of lines.
  • Each line of output should have at most 10 characters on it.
  • There should be as few lines as possible.
  • Where necessary, it should break words between lines with hyphens; the hyphen should always be the last character on a line.
  • No word should start or end with a hyphen
  • Do not put a space at the beginning of any line.

You may assume that the String contains only words, with no punctuation and exactly one space between words.

For example, if the parameter holds "We have ten assignments in this course Assig10 is the last one", it should print:
We have t-
en assign-
ments in 
this cour-
se Assig10
is the la-
st one 

*Hints: You may find some String methods helpful, for example, *
  • You can get a single character from a string using the charAt method: "SomeText".charAt(3) gives the character 'e'
  • You can get a substring from a string using the substring method: "SomeText".substring(2,6) gives the string "meTe"

Please note that the output of your method will be matched with the model solutions. Do not leave in your code any debugging statements that print out any extra information.