Introduction
,java,,tore, search, sort, remove
Country class CountryCatalogue class
Requirement
CS1027: Assignment 1
Due: Tuesday, July 5 th , 11:55pm.
Weight: 10%
Purpose:
To gain experience with
• Java
• Fixed length arrays
Task:
You will recreate your assignment 3 from 1026 in Java.
In this assignment you will create a complete program that uses classes to store, search, sort, remove,
and filter country data. The two major tasks are outlined below.
Provided With:
You are given a file called Assignment1.java that contains a main method. Use this class to run
the code you write. Your code must work with this file; you cannot alter this file as a means of making
your code work. The marker will use a file similar to this to test the functionality of your code.
You are also provided two files called ThingToWriteFile.java and ThingToReadFile.java. These
classes are provided to make fileIO easier. Have a look at the code to understand how to use these
objects.
Two text files are included. One contains the country information (data.txt) and the other
contains the country/continent information (continent.txt). NOTE: Both of these files have a header
line!
On the last page of this assignment you are provided with an example output. Reference this for
text formatting.
You may assume all data is correct and that there are no errors in the data files (don’t worry
about exceptions, or validating inputs, etc.).
Stuff To Do:
1. Implement a Country class:
• The purpose of this object is to represent a country. This object will store some
important information about the country and will have a small number of simple
methods.
• This class will contain variables for the country name, population, area, and continent in
which it exists.
• You will need to write a constructor for the object. This constructor should take a name,
population, area, and continent as parameters.
• Write getter methods for the four instance variables (getName, etc.).
• Write a method called getPopDensity to return the country’s population density
(population/area).
• Write a single setter method for the population instance variable (setPopulation).
• Write a method called writeToFile which takes a ThingToWriteFile object as a
parameter. This method must write some country details to the provided
ThingToWriteFile object provided. Below is an example of how to print the details to
file (commas are important):
i. NAME, CONTINENT, POPULATION, POPULATION DENSITY
ii. Ex: Canada, North America, 34207000, 3.428881310807587
• Write a method printCountryDetails that prints some country details to the user:
i. NAME is located in CONTINENT has a population of POPULATION an area
of AREA and has a population density of GETPOPDENSITY
ii. Ex: Canada is located in North America has a population of 34207000, an area
of 9976140.0, and has a population density of 3.428881310807587
• Write a toString method which simply returns a string in this format (include a new line
character at the end):
i. NAME in CONTINENT
ii. Canada in North America
Test all your classes and methods before moving to the next section. Feel free to create other
helper methods if necessary.
2. Implement a CountryCatalogue class:
• The class should have a few important instance variables:
i. A final int called DEFAULT_SIZE which is equal to 5. This variable will be
used when the catalogue array is instantiated.
ii. A final int called NOT_FOUND which is equal to -1. This will be used as a flag
for some of our methods.
iii. A country array called catalogue which will store a bunch of countries.
iv. An int which stores the number of countries currently in the catalogue
v. A set of called continents. This set will store the continents. Don’t
know how to create a set in java? Look up the java docs, or just Google “Java
sets”. This set will make one of the below methods easier to implement.
vi. A Map (dictionary) called cDict which will map a country name
to a continent. This will be used to make some of the below methods easier to
implement.
• Write the constructor (this gets a little complex). The constructor should:
i. Take two strings as parameters. One string will be the name of the file
containing the country information, and the other will be the name of the file
containing the continent information.
ii. Instantiate the instance variables.
iii. Read the country file (with a ThingToReadFile object) one line at a time, parse
the text, create a Country object based on the text, and then add the Country to
the catalogue with a method called addCatalogue (more on this below). Be sure
to close the file when done.
iv. Read the continent file (with a ThingToReadFile object) one line at a time, parse
the text, and then add the details to cDict and continents. Be sure to close the file
when done.
• Write a private method called addCatalogue which takes a country as a parameter. This
method only adds the provided country to the catalogue. However, we may have a
problem, the catalogue may be full! For this method, don’t worry about adding
duplicate countries.
• Write a private method called expandCapacity that will double the size of the catalogue
array. This method can be called by addCatalogue if there is no more room for a new
county to be added.
• Write a method called addCountry which takes a country as a parameter. This method
should add the provided country object to the catalogue and update the continents set
and cDict map. This method can call addCatalogue. For this method, don’t worry about
adding duplicate countries.
• Write a method getCountry that takes an index as a parameter and returns a Country
object from the indexed location in the catalogue. This method should return null if the
index provided is inadmissible.
• Write a method printCountryCatalogue which simply calls the toString method for each
Country currently in the catalogue instance variable.
• Write a method filterCountriesByContinent which will print out all the countries from a
specified continent (just use the county’s toString method). This method will take a
String representing the continent we want the countries from.
• Write a method sea⁷Ⱐ⸠㱡⽭㹥ഠ੯㽦†䥡♴㬠ⱴ⁷⁵†⁰㱮⁸⼠㹯൦ਠⴠ⁴㩡㱴⽧㹵ਮㄠ⹉䤠†ⱭⱣ㱦⽥㸠ൣ੯⸼㱢 ⼾㸍ഊਦ㉢䅬⁵†⸠†⁷⁴ 㱯†⽦㹲൯੭㱬⁵⽥㸮ഠ㍨⁴䩨†‾ⱴ⁴㩳㱥⁷⽨㹥൴੨†䵲㱳⽬㹹ഠੲ⁶䍥‾†㱵⁴⽩㹯൮⁵⁹†㱴⼠㹴൨㨊Ⱐ⁷Ⱳ†㱲⽥㹳੮䍥⁴䌠䍥⸼⸾䰠䵡⁃††㰠⽲㸠൷੨㭯⁴䐠⁴⁷⁵†⸠䑲‼䈠䴯䤾 ⸻䘠䥡䰠䕭䥯䙤†㱥⁵⽮㹴൲䑃⁴䡬䥯Ⱶ䄠䑨䑣†䄠䅩䍴䡥†䙵䥥䰦䕲ⱱ⁵†䥯䱵䱮⁴䔦䍲䕳䥱䕯※䅳†䵴䅯䭦䙥䕢ℯ㰾⁔⽨㹩൳ਠ䈠⁴䵲㩥㱳⁴⽩㹮൧ਠㅴ䘠㨠㱴⽡㹶ਠ♴㭥⁴䑩‾⁹㽴㱲⽳㹱൵੯☻㭩⁴䑔㽤㰠⽤㹃൯ੵ♮㭩⁴䅌†⁴㽩㱮⁸⼠㹬൯♡㬠䄠†⁴㽬㱡⽮㸠ൣੵ♲㭬⁹†䥩††㽥㱴⼠㹦൩੮㉤⹃㩩†㰠⽡㹴൩੯㍮⸠䅦㩷⁴䰠㱹⼠㹴൨䔠†…䵴⸠㩯㱤⽩㹮൴䍯⁹䄠†††⁴㍡㑩㉬び㜠どて〠ⱴ㥵㥮㝴㙲ㅹ㐠ぬづⱳ†㱤⽰㹵൬⁴㍹⸠㑲㉡㡮㡧㡥ㄮ㌠ㅔと㡩び㜠㕭㡥㝴㱨†⽴㹡൫†㰠⼠㹲ൡ੮䕧⁵⁶⁷䕤
㕡㑶㍥ㄠ㙳㙯ねづⰼ ⁴ㅩ㍯の㉡㝬㤠はⱯ⁰⁓⁰㰠⽶㹩㑤ㄠ㙢㥬㉯㕷㈠ㅦ㑯㍲㐠㝡㘠㙤㥥㉭㱯⁴⽲㹡൴੩䍯☠㭯䕷♯㬠㱵※⼠㹗൲੩㱩⽴㸠൷੨䍩䍴㩥㰠⽴㹩൮䍮⁴䅨㱯⼼㹢൲ਠ⁰䄠
䅢㱮†⽯㹮ഠੴ䉨†䅴㱡⽵㹥ഩਠ䩯䄠㰊⽴㹩൮䍮䅥㱵⁴⽩㹯൮ਮ䤠䅰㱯⁰⽵㹴ഠੰ†䅯㰠⽲㸠യਾ䴍†䅯㱴⼠㹯൵ੴ䕰䅤㰮⼠㸯ാ䘊†䕹㱬⽥㹳ഠ䥮⁴䕳㱬⽥㹥ഠੴ䅴䅥㱭⽯㹤൳ਠ䭥⁹䅲㰍⁁⽑㸺഼䍲⁵※䅷㱣⁰⽡㹲ਠ⁃䅡㱵†⽵㹳ਠ䜽 䄾㰺⁏⼡㸠ൄ䌠⁅‡‡㩲䍮䅰⁷⁹ㄠ㌯㌾㤍ㄊ㤨ちのつ〠ⱡ⁴㰠⽪㹥ൣੴ㤠㕯㥦㘠㥳㙯ね〠Ɽ⁕ⁱㅳ㌠㥩㕳㑴㍥ㅡ㑤㘮㤠㥆㙯㕲㐼㡢㥲㰠⼊㹥൸䍭 㩬⁏䭴⁴††䅴†⁴⁴㕨づ㔠び㍡㥭㍥㌮ⰼ 㤻㠠け㜺㘠⹗㥨㉹Ⱐ㱣…⽲㹳൱ੵ††⁁⁉㕴ㄦ㑲㥱㑵㉯〻㝳㠠ㅣㅯ㕭㡰㉬㥩㥣㱡⼮㸠ਦ䍲䅩㩲㰠†⽴㹨ਠ䅵㰠⽯㹮ദੲ䍳㰠⽷㸠൨੯䵷㱯‿⼼㹢൲ਠ䌯⁔†〠⁴㉧㔠㩘㰠⁴⽯㸠൴੨䉥⁴⁴‼䅢㰦⽬㸻ഠੑ†⁍㉮㉡㝩ㅥ㙳㜩㈠㡩㝮㐠㕊㥡㑶㝡㘿ㄼ㙢㰠⼊㹯ഠੁ䌺䅭㱯⁹⽴㹨൯੮⁵†㍭㐠㉨㡥㡲㡥ㄮ㌠ㅆど㡲び㝴㔬㠠㝣㱥†⽯㹵൴ਠ䝴‾䄍㰠⽳㸠൦੯ⁱ※ぬ㈠ㅮ㡥㡥㥤㜠ぴ㕯㜠㥩㥭㕰ㅯ㉲㑴㐠㙴㱥⽢㹲ൡੲ䍹†㩩䅧Ⱐㅥ㝳㜠㝯㙦㔼㕢ひ㌠㌯ⰾ㰍⼊㹥ople using them. Instantiating them is a little weird (more details on this later in the
course, for now you’ll have to just type the magic code) (HINT for instantiation:
hashmap). Iterating through them is different too; try Googling how to iterate through a
map in java.
• Q: How do we use Sets in Java?
o A: Basically exactly the same answer above, but the java docs are different
()
• Q: How do I parse a String in Java?
o A: Try looking up “splitting a string in java” with Google.
• Q: I swear I did everything right, but for some reason my files won’t open!
o A: This isn’t uncommon.
? If you’re using eclipse, try putting the .txt files in the project directory (the same
folder c