Must be done in Java** please help I am struggling on what to do. the task is cr
ID: 3909230 • Letter: M
Question
Must be done in Java** please help I am struggling on what to do. the task is create a program that does the following(pls make the code as simply as posssible as i am just learning java):
read the following .txt files
continenet.txt
Country,Continent
China,Asia
United States of America,North America
Brazil,South America
Japan,Asia
Canada,North America
Indonesia,Asia
Nigeria,Africa
Mexico,North America
Egypt,Africa
France,Europe
Italy,Europe
South Africa,Africa
South Korea,Asia
Colombia,South America
data.txt
Country|Population|Area
China|1,339,190,000|9,596,960.00
United States of America|309,975,000|9,629,091.00
Brazil|193,364,000|8,511,965.00
Japan|127,380,000|377,835.00
Canada|34,207,000|9,976,140.00
Indonesia|260,581,100|1,809,590.97
Nigeria|186,987,563|912,134.45
Mexico|128,632,004|1,969,230.76
Egypt|93,383,574|1,000,000.00
France|64,668,129|541,656.76
Italy|59,801,004|300,000.00
South Africa|54,978,907|1,222,222.22
South Korea|50,503,933|98,076.92
Colombia|48,654,392|1,090,909.09
saveCountryCatalogue: given a parameter filename (a string), write the country data of the catalogue as specified below to file filename. Each entry should be formatted as below, one country per line. Return the number of lines that were written in the file filename. Format: Name|Continent|Population|PopulationDensity Ex: China|Asia|1200000000|14.56
findCountryWithLargestPop :
find the country with the largest population, return the name of this country. This method takes no additional parameters
findCountryWithSmallestArea:
find the country with the smallest area, return the name of this country. This method takes no additional parameters.
setPopulationOfACountry :
Given two parameters countryName (a string) and countryPopulation (an integer), find the country named countryName in the countryCatalogue (if it exists), and set the population of that country to the value countryPopulation . o Return true if the population is updated, and return false otherwise.
Explanation / Answer
Given below is the code for the question.In order to see output fiel, you need to refresh project after code finishes running.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
Country.java
-----
public class Country {
private String name;
private String continent;
private long population;
private double area;
public Country(String name, String continent){
this.name = name;
this.continent = continent;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public String getName() {
return name;
}
public String getContinent() {
return continent;
}
public String toString(){
return String.format("Country: %s, Continent: %s, Population: %d, Area: %.2f", name, continent, population, area);
}
}
ProcessCountry.java
--------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class ProcessCountry {
static ArrayList<Country> countryCatalogue;
public static int saveCountryCatalogue(String fileName) throws FileNotFoundException{
PrintWriter pw = new PrintWriter(fileName);
DecimalFormat formatter = new DecimalFormat("###,###,###.##");
for(Country c: countryCatalogue)
pw.printf("%s|%s|%s|%.2f ", c.getName() , c.getContinent(), formatter.format(c.getPopulation()), c.getPopulation()/c.getArea() );
pw.close();
System.out.println("Country catalogue saved to " + fileName +". Please refresh folder to see output file");
return countryCatalogue.size();
}
public static String findCountryWithLargestPop(){
int maxIdx = 0;
for(int i = 1; i < countryCatalogue.size(); i++)
{
if(countryCatalogue.get(i).getPopulation() > countryCatalogue.get(maxIdx).getPopulation())
maxIdx = i;
}
return countryCatalogue.get(maxIdx).getName();
}
public static String findCountryWithSmallestArea(){
int minIdx = 0;
for(int i = 1; i < countryCatalogue.size(); i++)
{
if(countryCatalogue.get(i).getArea() <countryCatalogue.get(minIdx).getArea())
minIdx = i;
}
return countryCatalogue.get(minIdx).getName();
}
public static void setPopulationOfACountry(String countryName, long countryPopulation){
for(int i = 0; i < countryCatalogue.size(); i++)
{
if(countryCatalogue.get(i).getName().equalsIgnoreCase(countryName)){
countryCatalogue.get(i).setPopulation(countryPopulation);
return;
}
}
}
public static void setAreaOfACountry(String countryName, double countryArea){
for(int i = 0; i < countryCatalogue.size(); i++)
{
if(countryCatalogue.get(i).getName().equalsIgnoreCase(countryName)){
countryCatalogue.get(i).setArea(countryArea);
return;
}
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String continentFilename, popAreaFilename;
System.out.print("Enter filename containing country/continent: ");
continentFilename = keyboard.next();
System.out.print("Enter filename containing population/area: ");
popAreaFilename = keyboard.next();
//process continent data
Scanner infile = null;
try {
infile = new Scanner(new File(continentFilename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
infile.nextLine(); //skip the column names line
countryCatalogue = new ArrayList<Country>();
while(infile.hasNextLine())
{
String line = infile.nextLine();
String[] values = line.split(",");
countryCatalogue.add(new Country(values[0], values[1]));
}
infile.close();
//process population area
try {
infile = new Scanner(new File(popAreaFilename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
System.exit(1);
}
infile.nextLine();
while(infile.hasNextLine())
{
String line = infile.nextLine();
String[] values = line.split("\|");
setPopulationOfACountry(values[0], Long.parseLong(values[1].replaceAll(",", "").trim()));
setAreaOfACountry(values[0], Double.parseDouble(values[2].replaceAll(",", "").trim()));
}
infile.close();
System.out.print("Enter output filename to save data: ");
String outFilename = keyboard.next();
System.out.println("Country with largest population: " + findCountryWithLargestPop());
System.out.println("Country with smallest area: " + findCountryWithSmallestArea());
try {
saveCountryCatalogue(outFilename);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
output
=====
Enter filename containing country/continent: continent.txt
Enter filename containing population/area: data.txt
Enter output filename to save data: output.txt
Country with largest population: China
Country with smallest area: South Korea
Country catalogue saved to output.txt. Please refresh folder to see output file
output file generated : output.txt
=======
China|Asia|1,339,190,000|139.54
United States of America|North America|309,975,000|32.19
Brazil|South America|193,364,000|22.72
Japan|Asia|127,380,000|337.13
Canada|North America|34,207,000|3.43
Indonesia|Asia|260,581,100|144.00
Nigeria|Africa|186,987,563|205.00
Mexico|North America|128,632,004|65.32
Egypt|Africa|93,383,574|93.38
France|Europe|64,668,129|119.39
Italy|Europe|59,801,004|199.34
South Africa|Africa|54,978,907|44.98
South Korea|Asia|50,503,933|514.94
Colombia|South America|48,654,392|44.60
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.