首页 > > 详细

Java辅导: CS1027 Assignment 1辅导Java设计

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

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!