Help with Java !!! This Assigment me going crazy!!! There\'s another file that i
ID: 3826603 • Letter: H
Question
Help with Java !!! This Assigment me going crazy!!! There's another file that i have to insert in this code, its a .csv file (excel). PLEASE EXPLAIN AND SHOW OUTPUT, AND EXPLAIN WHERE I SHOULD PUT THE .CSV FILE. PLEASE
1. Document the Country class public interface. Use the javadoc utility to produce the class documentation. Use a web browser to view the documentation file generated by the javadoc utility.
2. Complete the TODOs in Lab9CountryDataTester.java and CountryDataLoader.java. Test your changes. You may want to make a change and test it rather that making all the changes and then test all the changes as once. Find a way to incrementally build and test. You may also want to work with a smaller data file during the build and test increments. When all the changes are made and the countryData.csv is the input file the program output looks like this:
Here's the Code, Country TODO's:
Here'sCountry Data Loader Code, TODO's:
And Last the Tester, TODO's:
Country Data Sorted by Natural Order [0] name Afghanistan population 32564342, area 652230.0 [1] name Akrotiri population 0, area: 123.0> [2] name Albania population: 3029278 area 28748.0 [3] name Algeria population: 39542166 area 2381741.0> [4] name American Samoa population: 54343 area 199.0> [5] name Andorra population. 85580 area 468.0 [6] name Angola. population 19625353 area 1246700.0> [7] [9] name Argentina population. 43431886 area: 2780400.0 252 Kname: Venezuela population 29275460, area 912050. 253 area [254]Explanation / Answer
//Country.java
public class Country implements Comparable<Country> {
private String name;
private long population;
private double area;
public Country() {
this("", 0, 0.0);
}
public Country(String name) {
this(name, 0, 0.0);
}
public Country(String name, long population, double area) {
this.name = name;
this.population = population;
this.area = area;
}
public String getName() {
return name;
}
public long getPopulation() {
return population;
}
public double getArea() {
return area;
}
public void setPopulation(long population) {
this.population = population;
}
public void setArea(double area) {
this.area = area;
}
public double getPopulationDensity() {
double pd = 0.0;
if(area > 0.0) {
pd = population / area;
}
return pd;
}
public String toString() {
String s = String.format( "<name: %s, population: %d, area: %.1f>", name, population, area);
return s;
}
public int compareTo(Country other) {
if(other == null) {
return 1;
}
int rc = name.compareTo(other.name);
if( rc != 0) {
return rc;
}
double popDiff = population - other.population;
if(popDiff > 0) {
return 1;
}
if(popDiff < 0) {
return -1;
}
double areaDiff = area - other.area;
if(areaDiff > 0) {
return 1;
}
if(areaDiff < 0) {
return -1;
}
return 0;
}
}
//CountryDataLoader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CountryDataLoader {
/**
* Reads country data from the country data file. Create a Country object
* for each line of country data. Stores the Country objects in a List.
*
* The country data file consists on lines of text. Each line contains
* three fields: the country's name, the country's population, and the
* country's area. A '|' is used to separate the fields.
*
* @param countryDataFile
* @return List of Country objects
*
* @throws FileNotFoundException
*/
public static List<Country> load(File countryDataFile ) throws
FileNotFoundException {
List<Country> countryList = new ArrayList<>();
Scanner inpFile = new Scanner(countryDataFile);
inpFile.useDelimiter("[| ]");
while(inpFile.hasNextLine()) {
String name = inpFile.next();
long population = inpFile.nextLong();
double area = inpFile.nextDouble();
Country country = new Country(name, population, area);
countryList.add(country);
inpFile.nextLine();
}
return countryList;
}
}
//Lab9CountryDataTester.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Lab 9. Interfaces. Complete the TODOs.
*/
public class Lab9CountryDataTester {
/**
* This program allows the user to select a country data file and sorts
* the country data.
*
* The country data file consists on lines of text. Each line contains
* three fields: the country's name, the country's population, and the
* country's area. A '|' is used to separate the fields. Here is a
* snippet of the data file:
Botswana|2182719|581730.0
Saint Vincent and the Grenadines|102627|389.0
Dominican Republic|10478756|48670.0
Denmark|5581503|43094.0
Mexico|121736809|1964375.0
*
*
* The country data is sorted in the following ordering:
* 1. Natural Order
* 2. Descending population (highest to lowest) and ascending name
* 3. Descending area and ascending name
* 4. Descending population density and ascending name
* 5. Reverse Natural Order
*
* After each sort the program prints the first 10 countries and the last
* 10 countries in the ordered list.
*/
public static void main(String[] args) throws FileNotFoundException {
final int PRINT_FIRST_N = 10;
final int PRINT_LAST_N = 10;
// TODO: Part 1.
File countryDataFile = new File("countryData.csv");
// TODO: Part 2.
List<Country> countryList = CountryDataLoader.load(countryDataFile);
// TODO: Part 3.
Collections.sort(countryList);
System.out.println();
System.out.println("Country Data Sorted by Natural Order");
printFirstN(countryList, PRINT_FIRST_N);
printLastN(countryList, PRINT_LAST_N);
// TODO: Part 4
Collections.sort(countryList, new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
if(o2 == null) {
return 1;
}
int rc = o1.getName().compareTo(o2.getName());
if( rc != 0) {
return rc;
}
double popDiff = o1.getPopulation() - o2.getPopulation();
if(popDiff > 0) {
return -1;
}
if(popDiff < 0) {
return 1;
}
return 0;
}
});
System.out.println();
System.out.println("Country Data Sorted by Descending population and " +
"Ascending name");
printFirstN(countryList, PRINT_FIRST_N);
printLastN(countryList, PRINT_LAST_N);
// TODO: Part 5
Collections.sort(countryList, new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
if(o2 == null) {
return 1;
}
int rc = o1.getName().compareTo(o2.getName());
if( rc != 0) {
return rc;
}
double popDiff = o1.getArea() - o2.getArea();
if(popDiff > 0) {
return -1;
}
if(popDiff < 0) {
return 1;
}
return 0;
}
});
System.out.println();
System.out.println("Country Data Sorted by Descending area and " +
"Ascending name");
printFirstN(countryList, PRINT_FIRST_N);
printLastN(countryList, PRINT_LAST_N);
// TODO: Part 6
Collections.sort(countryList, new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
if(o2 == null) {
return 1;
}
int rc = o1.getName().compareTo(o2.getName());
if( rc != 0) {
return rc;
}
double popDiff = o1.getPopulationDensity() - o2.getPopulationDensity();
if(popDiff > 0) {
return -1;
}
if(popDiff < 0) {
return 1;
}
return 0;
}
});
System.out.println();
System.out.println("Country Data Sorted by Descending population " +
"density and Ascending name");
printFirstN(countryList, PRINT_FIRST_N);
printLastN(countryList, PRINT_LAST_N);
// TODO: Part 7
Collections.reverseOrder(new Comparator<Country>() {
@Override
public int compare(Country o1, Country o2) {
if(o2 == null) {
return 1;
}
int rc = o1.getName().compareTo(o2.getName());
if( rc != 0) {
return rc;
}
return 0;
}
});
System.out.println();
System.out.println("Country Data Sorted by Reverse Natural Order");
printFirstN(countryList, PRINT_FIRST_N);
printLastN(countryList, PRINT_LAST_N);
}
/**
* Print the first N objects in a List
*
* @param list
* @param n
*/
private static void printFirstN(List list, int n) {
int endIdx = Math.min(n, list.size());
for(int k = 0; k < endIdx; ++k) {
System.out.printf("[%d] %s%n", k, list.get(k));
}
}
/**
* Print the last N objects in a list
*
* @param list
* @param n
*/
private static void printLastN(List list, int n) {
int startIdx = Math.min(list.size() - n, list.size());
for(int k = startIdx; k < list.size(); ++k) {
System.out.printf("[%d] %s%n", k, list.get(k));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.