辅导案例-CS1027B
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


This assignment is due on Friday, July 3, 2020 by 11:55pm. See the bottom for submission
details.

Purpose

To gain experience with
 Creating classes with simple methods
 Inheritance and sub-classes
 Reading from a text file
 Algorithm design

Introduction

In honour of Canada Day (July 1), this first assignment has a theme of Canadian geography.
You are constructing a program that displays a map of Canada and some of the notable cities
across the country. There is a panel in the upper-left corner overlaid on the map, and this panel
contains information about the country. By default, it displays statistics about the various cities'
populations. This panel changes when a selection is made – which is done by right-clicking
down and dragging the mouse and then letting go to complete the selection – to display the list
of cities located within the bounds of that rectangular selection.
Notes: City locations are approximate and indicated by the bottom point of the marker icons.
Populations are approximate, rounded, and may not be up to date.

CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II



Figure 1. A screenshot of the finished product, the map of Canada displayed with various city
markers indicating capitals and other popular cities. A region of the map has been selected,
shown as a blue translucent rectangle, and the panel in the upper-left corner displays the cities
contained in the selected region.



Provided files

The following is a list of files provided to you for this assignment. Please do not alter these files
in any way.
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


 MyFileReader.java – helps with reading a text file
 Map.java – provides the visual GUI for the program
 TestClasses.java – provides several tests to check that your code is working correctly
 cities.txt – contains the list of Canadian cities and their info to be read into the program
 canada.jpg – map of Canada
 marker_city.png – marker icon for a non-capital city
 marker_prov.png – marker icon for a provincial capital city
 marker_nat.png – marker icon for the national capital city

Classes to implement

For this assignment, you must implement 4 Java classes: City, ProvCapital, NatCapital, and
Program. Follow the guidelines for each one below.
In all of these classes, you can implement more private (helper) methods, if you want to, but you
may not implement more public methods. You may not add instance variables other than the
ones specified below. Penalties will be applied if you implement additional instance variables.

City.java

This class represents one city in the program. It contains information about the city and is used
by the provided Map class to display the city's marker. Note that you will have to import
javax.swing.ImageIcon in this City class and its sub-classes in order to use the marker icons.
The class must have the following private variables:
 name (String)
 population (int)
 x (int)
 y (int)
 marker (ImageIcon)
The class must have the following public methods:
 City (constructor) – takes in parameters for name, population, x, and y and assigns their
values into the corresponding instance variables. The marker instance variable is
initialized as a new ImageIcon object with the "marker_city.png" image file.
 getName – returns the city's name
 getPopulation – returns the population
 getX – returns the x coordinate
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


 getY – returns the y coordinate
 getMarker – returns the marker icon
 setName – takes in a new name parameter and assigns it to the corresponding variable
 setPopulation – takes in a new population parameter and assigns it to the corresponding
variable
 setMarker – takes in a new marker parameter and assigns it to the corresponding
variable
 equals – takes in a parameter of an "other" City object and checks if "this" and "other"
are equal, by checking if both the x distance and y distance are less than 5 pixels. For
example if CityA is located at (100, 200) and CityB is located at (102, 196), they are
considered equal because in both dimensions they are within 5 pixels of one another.
 toString – returns the city's name to be printed out when the city object is printed

ProvCapital.java

This class represents a city that is a capital of a province or territory in Canada. Since it is a
specific category of some cities, this class must be created as a sub-class of City.
In addition to the inherited variables from City, this class must have one private instance
variable:
 province (String)
The class must have the following public methods:
 ProvCapital (constructor) – takes in parameters for name, population, x, y, and province.
The first four of which must be sent to the super-class constructor first. The province
parameter value must then be assigned to the corresponding instance variable. The
marker instance variable is initialized as a new ImageIcon object with the
"marker_prov.png" image file. (Hint: how can you set this marker icon even though it's
private and declared in a different class?)
 toString – return the city's name followed by a space and then the province in which this
city is the capital, surrounded by parentheses (i.e. Toronto (Ontario)).

NatCapital.java

This class represents the city that is the capital of Canada (Ottawa). Since it is a specific
category of a city, this class must be created as a sub-class of City.
This class does not require any new instance variables, it only needs those that are inherited
from City.
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


The class must have the following public methods:
 NatCapital (constructor) – takes in parameters for name, population, x, and y, which
must all be sent to the super-class constructor first. The province parameter value must
then be assigned to the corresponding instance variable. The marker instance variable is
initialized as a new ImageIcon object with the "marker_nat.png" image file.
 toString – return the city's name followed by a space and then "Canada's capital"
surrounded by parentheses (i.e. Ottawa (Canada's capital)). Even though there is only
one capital, don't hard-code the first part of this string (Ottawa) but rather retrieve the
variable from the object.

Program.java

This class, as its name suggests, will be the main heart of the program. It will be the entry point
of the program, read in the file of cities and create objects for each of them, contain the array of
those cities, as well as perform tasks that interact with the GUI.
This class must have only one variable:
 cityArray (City array) – it must be private and static
The class must have the following methods:
 Program (public constructor) – take in a boolean parameter "showMap" which will
indicate whether we want to run the program with the visual component or not. Initialize
the cityArray with 5 slots to begin. Create a MyFileReader object to open and read the
cities.txt file. The first line is header info and must be read in but can be immediately
discarded. After that, the file follows a specific format in which each line contains one
piece of information in the following order: city name, population, x, y, type, [prov]. The
type will either be "nat_cap" (national capital), "prov_cap" (provincial capital), or "city" for
a non-capital city. The [prov] line is only provided for provincial capital cities. All other
cities will not have the province mentioned. As you read in each set of the city's
properties, create a City object containing the info and add it to cityArray. When needed,
call expandCapacity to create more space. After all the cities have been read and
created, check the value of the parameter, showMap. If it is true, create a Map object
and invoke its addCity() method with each of the city objects in cityArray to add their
markers to the map. Call the map's defaultText method. If showMap is false, do nothing.
 getCityList (public) – return the global array of cities
 expandCapacity (private) – create a new array that has 5 more spots than cityArray has,
and then transfer the contents from cityArray to this new array and point the cityArray
 findCitiesInRect (public static) – take in four int parameters: sx, sy, ex, and ey which
represent the (x, y) coordinate of one corner of a selection and the (x, y) coordinate of
the diagonally opposite corner of the selection. Find all the cities that are located within
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


the bounds of the rectangle defined by (sx,sy) and (ex,ey) and add them into a City array
in the same order they are stored in cityArray but without gaps in the array. They must
all be in consecutive array slots. You may not use any different data structures or
collections for this process. Note that the regions can go in any direction, i.e. sx isn't
necessarily less than ex, and same with sy and ey. You must account for all cases. Note
also that cityArray may have null slots so check for this to avoid exceptions. Return the
City array containing the cities within the selection bounds at the end of the method. The
Map class is already set up to call this method and use the results in the returned array
to be displayed in the upper-left panel. The following figures demonstrate selections in
opposite directions and the corresponding sx, sy, ex, and ey placements. In the left
figure, the selection begins at the upper left corner (sx, sy) and ends in the lower right
corner (ex, ey). In the right diagram, the selection begins in the lower right corner (sx, sy)
and ends in the upper left corner (ex, ey).

 defaultTextboxInfo (public static) – calculate the average, minimum, and maximum
populations of all capital cities (including the provincial capitals and the national capital)
and the City objects which have the minimum and maximum populations. Then do the
same thing for the non-capital cities. In total you will compute 6 values and for 4 of them
you will also need the corresponding City object. Return an Object array with each of
these results in the order shown below. The average stats must be doubles, the min and
max stats must be integers, and the remaining results must be City objects that
correspond to the min or max stats. The Map class is set up to call this method and use
the results as the default text in the upper-left panel when there is not an active selection
on the map. The results in the array must be ordered as:
array[0] = avg value (caps)
array[1] = min value (caps)
array[2] = min City (caps)
array[3] = max value (caps)
array[4] = max City (caps)
array[5] = avg value (non caps)
array[6] = min value (non caps)
array[7] = min City (non caps)
array[8] = max value (non caps)
array[9] = max City (non caps)
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II



Marking notes

Marking categories
 Functional specifications
o Does the program behave according to specifications?
o Does it produce the correct output and pass all tests?
o Are the class implemented properly?
o Are you using appropriate data structures?
 Non-functional specifications
o Are there Javadocs comments and other comments throughout the code?
o Are the variables and methods given appropriate, meaningful names?
o Is the code clean and readable with proper indenting and white-space?
o Is the code consistent regarding formatting and naming conventions?
 Penalties
o Lateness: 10% per day
o Submission error (i.e. missing files, too many files, ZIP, etc.): 5%
o "package" line at the top of a file: 5%

Remember you must do all the work on your own. Do not copy or even look at the work of
another student. All submitted code will be run through cheating-detection software.

Submission (due Friday, July 3, 2020 at 11:55pm EDT)

To submit the assignment, navigate to Week 2 (or Week 1) and click on Assignment 1. In this
assignment page, attach the files listed below to the assignment page and click Save and
Submit. Check that your submission went through on there and look for an automatic OWL
email to verify that it was processed. Incomplete or incorrect submissions will be subject to
penalties, so verify the submission before closing OWL.

Rules
 Please only submit the files specified below. Do not attach other files even if they were
part of the assignment.
 Do not ZIP or use any other form of compressed file for your files. Attach them
individually.
CS1027B
ASSIGNMENT 1 Computer Science Fundamentals II


 Submit the assignment on time. Late submissions will receive a penalty of 10% per day.
 Forgetting to hit "Submit" is not a valid excuse for submitting late.
 Submitting the files in an incorrect submission page will receive a penalty.
 You may re-submit code if your previous submission was not complete or correct,
however, re-submissions after the regular assignment deadline will receive a penalty.

Files to submit
 City.java
 ProvCapital.java
 NatCapital.java
 Program.java





Canada map obtained from: https://freevectormaps.com/canada/CA-EPS-01-0002?ref=atr.
51作业君 51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: ITCSdaixie