Computer science assignment

| May 15, 2019

Question 3 ( 27 marks)

 

The purpose of this question is to model a scenario of your choice with a small application that makes use of a map and provides the user with the ability to store, manipulate and extract data. 

You are encouraged to express yourself and to choose an area that interests you, but you must keep within the specification given below.

You will need to write at least one class of your own to model your scenario. You can add further classes of your own if you wish, but it is not essential, and you should avoid making your scenario too elaborate. We advise against adding further classes unless you are confident of what you are doing and are sure you have enough time to complete the coding.

When modelling your chosen scenario, it is important to test the methods you write. We recommend you write code in the OU Workspace to create an instance of your class and test each of your methods on it as you develop them. Remember that each time you make a change you will need to recompile the code before the change takes effect.

Your first task is to choose a scenario that interests you. Below are some examples to illustrate the sort of application maps can be used for.

Example scenarios using maps:

Activities 16-18 in Unit 10 illustrate a scenario to do with representing the interests of clients registered with a dating agency using an unsorted map, where each key of the map is a unique String, representing the name of a client, and the values are Sets of Strings, where each String in this Set represents an interest of that client.

Here are some further examples of how maps can be used:

Scenario: a council keeping a record of names of registered voters on a particular street using a sorted map, where each key of the map is an Integer, representing the number of a house in that street, and the values are Lists of Strings where each string in a List represents the name of a voter living at that house number.

Scenario: a league keeps a record of teams using a sorted map, where the keys of the map are Strings, each representing the name of a team, and the values are Sets of players. Players are represented by objects of a “bespoke” class – Player, designed especially for this application, with attributes name, position, goalAverage and salary.

Note that if you follow this sort of scenario, where you’re writing an extra class to be the component class, then, you will need to override the inherited equals()and hashCode() methods in that extra class.

You must not use any of the above scenarios, or any given in the module materials or elsewhere in this TMA. 

But otherwise it is up to you what scenario you invent, the type of map you choose (sorted or unsorted), and the types of the map keys and values, as long as the following conditions are met:

  • The keys must be of type Stringor Integer.
  • The value types must be either a Listor a Set from the Collections Framework, as is the case with the example scenarios given above. The values must not be references to individual objects.
  • So for example in the league example above, in the map, values are notindividual Player objects, but sets of Player objects.)

Note

  • You must discuss the scenario you have chosen with your tutor to check its suitability before proceeding further. Marks are awarded for this discussion. See part (a) below.
  • If you do not use a map whose values are of a collection type from the Collections Framework, then your maximum score on this question will be 15 (rather than 27).
  • Your answer should be a brief description of your chosen scenario, in terms similar to those in the example scenarios above.

You are required to write a short description, using the provided table, in ordinary English of your scenario.

 

Requirement Answer
General scenario  
Type of key (String or integer)  
Type of value (must be from Collections framework)  
Type of map (sorted or unsorted)  
Description of any additional classes  

 

5 marks

  • Create a project.. If your chosen scenario involves bespoke classes for map values (such as Playerin the last example scenario), write them in this project.

Next, write a suitably named class that models your chosen scenario, which for the moment only has to include the following features:

  1. An instance variable used to reference the map object you described in (a)
  2. A constructor that creates an empty map of a suitable type and assigns it to your map instance variable
  3. A public instance method to populate the map with some test data suitable to your chosen scenario. This method should take no arguments and should need no user intervention, in particular it should not involve the use of dialogues. All the data must be written into the code of the method.

 

 

 

4 marks

  • In this part we want you to add three instance methods to your scenario class.

Do not make use of dialogues for input or output.

The names of the methods should be kept exactly as given below. Where there are arguments you will need to use types appropriate to your scenario.

Each method should have a header comment which explains the purpose of the method and what arguments it takes.

Method name Description Marks
printMap() Takes no arguments and iterates over all the map’s keys to print out a meaningful textual representation of each key in the map and the associated value. Returns no value.

Note the method must iterate through the map keys: simply using System.out.println() with the map as argument is not an acceptable solution.

3
printMapValue() Takes a single argument. If the argument is a key in the map, prints a meaningful textual representation of its associated value, otherwise prints an output line announcing that the key is not present. Returns no value. 3
addMapEntry() Takes two arguments representing a key and a value. Adds the key and value to the map. If the key is already present the existing value is overwritten. Returns no value.

Note that the map values are collections, so the second argument must be of an appropriate collection type.

3

9 marks

  • In this part we want you to add further instance methods to your scenario class.

You must implement three of the six methods specified below.

Do not make use of dialogues for input or output.

The names of the methods must be kept exactly as given below. In some cases you will need to decide appropriate argument or return types.

Each method must have a header comment which explains the purpose of the method, what arguments it takes, and its return value if any.

Method name Description Marks
deleteEntry() Takes a single argument representing a key in the map. If the key given as the argument exists, deletes the key-value pair from the map, and returns true, otherwise returns false. 3
selectKeys() Returns a collection of all the keys that satisfy some particular selection criterion specified by one or more arguments. 3
selectValues() Returns a collection of all the map values that satisfy some particular selection criterion specified by one or more arguments. Note that since the map value for each key is itself a collection, this method will return a collection of collections. 3
createReducedMap() Returns a new map of the same type but including only the entries that satisfy some particular selection criterion specified by one or more arguments. 3
addToValue() Adds a new element to the collection of elements for a particular key. You can assume that the key exists in the map. Returns no value. 3
deleteFromValue() Deletes a particular element from the collection of elements for a particular key. You can assume that the key exists in the map and the corresponding collection contains the specified element. Returns no value. 3
  • Note – there are no extra marks for implementing morethan three of these methods, but if you would prefer to give a more complete solution, then please do so. You must indicate clearly which three of your methods you are submitting for assessment. If you submit more than three but do not indicate which of them you wish to be assessed it will be assumed that you intend the first three to be chosen.

9 marks

Don’t forget to:

include your TMA03Q3_Sol in the TMA03Download folder

paste the code you wrote for each part of the question into the corresponding part of your Solution Document.

Question 4 (28 marks)

You should be able to answer this question once you have completed Unit 12. The question also draws on what you have learnt in Units 10 and 11 and on earlier parts of the module.

The folder TMA03Download folder you unzipped earlier contains a skeleton project TMA03Q4.

The question concerns the Walton Classic Car Rally (WCCR for short), a rally for cars built between 1st January 1960 & 31st December 1967.

Software is required to set up a list of entrants, organised by car category, and produce lists of results by category. The categories are:

  • E1: Cars with engines 1200 cc or less
  • E2: Cars with engines between 1201 cc and 2000 cc inclusive
  • E3: Cars with engines of 2001 cc and over

Entrants register for the event by completing an online form on a secret website. Once registration closes, the data for each entrant, i.e., their name and the category of their car is written to a disk file. This file, which is in CSV format, has been provided for you as entrants.txt.

Launch BlueJ and open the project TMA03Q4 in the TMA03Download folder you unzipped earlier, then immediately save the project as TMA03Q4_Sol in the same folder. You should see that the project contains the class Entrant.

  • Open the Entrantclass, which represents entrants registered for the rally. The class contains declarations for a number of instance variables, a partially complete constructor, and a number of getter and setter methods. The purpose of each instance variable is indicated in the comments accompanying the declarations.

Code to initialise the instance variables name, category and time is provided in the Entrant class’s constructor.

Entrant numbers are to be allocated on a sequential basis as each Entrant object is created: the first entrant whose details are read from the file is to be given the number 1, the second 2, the third 3 and so on.

  • Declare a private, class(i.e. static) variable of type int for the Entrant class called nextNumber and initialise it to 1.

1 mark

  • Add code to the constructor to set the numberinstance variable of a newly created Entrant object to the value of the class variable nextNumber and then increment the value of nextNumber by 1, ready for the next Entrant object to be created.

Test your code by creating several instances of Entrant in the OUWorkspace and inspecting them. You should find that your Entrant objects all have the same state, except for their number instance variables which should have the values 1, 2, 3 and so on.

2 marks

  • In BlueJ, and using your project TMA03Q4_Sol, create a class called WCCR. In the class begin by deleting the example instance variable, the line of code in the constructor that references the variable, and the example method, adding suitable class comments and the following import statements:

import java.util.*;

import java.io.*;

import ou.*;

  • The WCCRclass needs a private instance variable, called entrants, to reference a list of Entrant objects in a rally. Add a line of code to the WCCR class to declare this instance variable. 

Next add a line of code to the constructor that was automatically created by BlueJ to assign a new instance of ArrayList to entrants.

2 marks

  • As mentioned in the preamble to this question, details about rally entrants are recorded in a file called entrants.txt. This file, which can be found in the project folder for this question, records for each entrant their name and car category in CSV format, like this:

John Smith,E1

Mary Jones,E2

Jane Smith,E1

MeiLing Wang,E3

Carlos Gonzalez,E3

Darren Edwards,E2

Melanie Roberts,E1

Note the comma delimiters. If you want to look at the file, use a text editor such as Notepad, but make sure that you close the file without saving it.

In the WCCR class write a public instance method called readInEntrants() that takes no arguments and returns no value. The method will need to read from the file entrants.txt.

The code for the method should do the following.

  • Prompt the user to select an appropriate text file.
  • Make use of BufferedReaderand Scanner objects to read the file line by line.
  • As each line (which contains the name of an entrant and their category of car) is read, a new Entrantobject should be created and its instance variables set using the values found in the line. Your code may assume that all input provided is valid. Finally, that instance of Entrant should be added to the list referenced by the instance variable entrants.
  • You should ensure that the input file is properly closed by your method and that it handles possible exceptions correctly.

Testing your code: You can test your code by executing

WCCR rally = new WCCR();

rally.readInEntrants();

in the OUWorkspace. When prompted for a file you should select entrants.txt. Having executed the method, you can use the inspector to inspect rally and check that its instance variable entrants references a list of the entrants created from the data in the text file, with correct names and car categories.

10 marks

    • To test the software, the organisers need a way to simulate the time each entrant takes to run the rally. After 20 years, experience has shown that no entrant completes the Walton Classic Car Rally in under 30.00 minutes or takes over 60.00 minutes. 

Write a private instance method for the WCCR class called generateTime() that takes no arguments and returns a random double value between 30.00(inclusive) and 60.00 (exclusive).

In the Java class libraries, look up the Javadoc for the Random class in the package java.util

You can find the Java class library documentation via the Help menu in BlueJ or online at

http://docs.oracle.com/javase/7/docs/api/

Read the detail for the instance method with the header public double nextDouble(). When an instance of the Random class is sent a message of the form nextDouble() it will return a random double between 0.0 (inclusive) and 1.0 (exclusive).

For your method to return a value between 30.00(inclusive) and 60.00(exclusive) you will need to make this calculation:

30.00 + (60.00 – 30.00) * the value returned by nextDouble().

2 marks

  • Write a public method for the WCCRclass called runRally() that takes no arguments and returns no value. The method should iterate over entrants, and for each entrant generate a random number between 30.00 (inclusive) and 60.00(exclusive) which should be used to set the time (in minutes) for that entrant.

Testing your code: You can test your code by executing

WCCR rally = new WCCR();

rally.readInEntrants();

rally.runRally();

Then use the inspector to inspect rally and check that the entrants held in the instance variable entrants have all had time set with values between 30.00(inclusive) and 60.00 (exclusive) (in reality you will have more decimal places – don’t worry about this).

1 mark

  • For convenience, the organisers want the list of entrants organised in order of the times taken to complete the rally, with the fastest (i.e. smallest) time first. This will be done by a method called sortEntrantList(). However, before writing this method, you will need to make some changes to the Entrantclass.
    • Modify the Entrantclass so that it implements the Comparable interface. To do this you will need to modify the class header and write a compareTo() method. 

Your compareTo() method will need to compare the time instance variables of the receiver and the method’s argument and return an int. To do this you should make use of the Double class’s static method compare() which takes two double primitive values as its arguments and returns an int value. The method returns 0 if the arguments are numerically equal, a negative value if the first argument is less than the second, and a positive value if the first argument is greater than the second.

Include your modified class header and your compareTo() method in your Solution Document.

3 marks

  • Now write the sortEntrantList()method for WCCR. The method should be public, take no arguments and return no value. The method should sort the receiver’s entrants list by the time each entrant has taken to complete the rally so that the fastest entrant is first in the list, the next fastest is second and so on.

1 mark

    • The organisers of the rally want to create three tables of results, one for each of the car categories. Declare three private instance variables for the WCCRclass called e1Results, e2Results and e3Results, each of which should be suitable for referencing a map whose keys are names of entrants and whose values are their times. The keys should be sorted at all times.

1 mark

  • Next add three lines line of code to the WCCRconstructor to assign suitable map objects to the instance variables you declared in (d)(i).

1 mark

  • Write a public instance method called categorise()for the WCCR class that takes no arguments and returns no value. This method should iterate over entrants, populating the maps e1Results, e2Results and e3Results, with the correct names and times of entrants.

Testing your code: You can test your code by executing

rally = new WCCR();

rally.readInEntrants();

rally.runRally();

rally.categorise();

Then use the inspector to inspect rally and check that e1Results, e2Resultsand e3Results, have been populated correctly.

4 marks

 

 

Question 4 resources;

 

Get a 5 % discount on an order above $ 150
Use the following coupon code :
2018DISC
Assignment 4: Special Problems and Challenges and the Future of Policing In America
Gynecologic Health

Category: Completed Assignments

Our Services:
Order a customized paper today!