Hi, can someone please provide notes throughout to code to explain each step ple
ID: 3890157 • Letter: H
Question
Hi, can someone please provide notes throughout to code to explain each step please
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class UNpopulation {
private int rank;
private long popJuly2016;
private long popJuly2017;
private String countryDetails;
private long growth;
private float growthPerCent;
public UNpopulation(int rank, long popJuly2016, long popJuly2017, String countryDetails) {
this.rank = rank;
this.popJuly2016 = popJuly2016;
this.popJuly2017 = popJuly2017;
this.countryDetails = countryDetails;
}
public void calculateGrowth() {
growth = popJuly2017 - popJuly2016;
}
public void calculateGrowthPercent() {
growthPerCent = ((float) growth / popJuly2016) * 100;
}
public String toString() {
StringBuilder result = new StringBuilder();
result.append(rank + " " + String.format("%,d", popJuly2016) + " ");
result.append(String.format("%,d", popJuly2017) + " " + growth + " ");
result.append(String.format("%.3f", growthPerCent) + " ");
result.append(countryDetails);
return result.toString();
}
}
public class UNSurveyour {
public static ArrayList<UNpopulation> list;
private static void populateListFromFile(String string) throws FileNotFoundException {
Scanner in = new Scanner(new File(string));
in.nextLine();
while(in.hasNextLine()){
String data[] = in.nextLine().split(" ");
int rank=Integer.parseInt(data[0]);
long pop2016=Long.parseLong(data[1]);
long pop2017=Long.parseLong(data[2]);
String countryDetail = data[3].trim();
UNpopulation newEntry = new UNpopulation(rank,pop2016,pop2017,countryDetail);
list.add(newEntry);
}
in.close();
}
private static void processData() {
for(UNpopulation entry:list){
entry.calculateGrowth();
entry.calculateGrowthPercent();
}
}
private static void displayData() {
System.out.println("Rank Pop_July_2016 Pop_July_2017 Growth %-Growth Country_Location");
for(UNpopulation entry:list){
System.out.println(entry);
}
}
private static void writeEntriesToFile(String string) throws IOException {
FileWriter writer = new FileWriter(string);
writer.write("Rank Pop_July_2016 Pop_July_2017 Growth %-Growth Country_Location ");
for(UNpopulation entry:list){
writer.append(entry+" ");
}
writer.close();
}
public static void main(String[] args) throws IOException {
list = new ArrayList<>();
populateListFromFile("PopulationTable-UN.txt");
processData();
displayData();
}
}
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class UNpopulation {
private int rank;
private long popJuly2016;
private long popJuly2017;
private String countryDetails;
private long growth;
private float growthPerCent;
public UNpopulation(int rank, long popJuly2016, long popJuly2017, String countryDetails) { //Constructor for UNPopulation
this.rank = rank;
this.popJuly2016 = popJuly2016;
this.popJuly2017 = popJuly2017;
this.countryDetails = countryDetails;
}
public void calculateGrowth() {
growth = popJuly2017 - popJuly2016; //Calculating growth
}
public void calculateGrowthPercent() { //Calculating growth percentage
growthPerCent = ((float) growth / popJuly2016) * 100;
}
public String toString() { //string representation of the object.
StringBuilder result = new StringBuilder(); // Building the string
result.append(rank + " " + String.format("%,d", popJuly2016) + " "); //Adding rank and population information 2016
result.append(String.format("%,d", popJuly2017) + " " + growth + " ");
result.append(String.format("%.3f", growthPerCent) + " "); //Adding growth percent
result.append(countryDetails); //Adding country details
return result.toString(); //Converting to String
}
}
public class UNSurveyour {
public static ArrayList<UNpopulation> list;
private static void populateListFromFile(String string) throws FileNotFoundException {
Scanner in = new Scanner(new File(string));
in.nextLine();
while(in.hasNextLine()){ // Accessing lines in the file
String data[] = in.nextLine().split(" "); //Splitting the line with space as delimiter
int rank=Integer.parseInt(data[0]); //Accessing each word obtained after splitting of line into words.
long pop2016=Long.parseLong(data[1]);
long pop2017=Long.parseLong(data[2]);
String countryDetail = data[3].trim();
UNpopulation newEntry = new UNpopulation(rank,pop2016,pop2017,countryDetail); //Creating a new UNpopulation object
list.add(newEntry); //Adding to the list
}
in.close();
}
private static void processData() { //Calaculating the growth percentage
for(UNpopulation entry:list){
entry.calculateGrowth();
entry.calculateGrowthPercent();
}
}
private static void displayData() { //Displaying the data on console
System.out.println("Rank Pop_July_2016 Pop_July_2017 Growth %-Growth Country_Location");
for(UNpopulation entry:list){ //Accessing every entry in the list and displaying it
System.out.println(entry);
}
}
private static void writeEntriesToFile(String string) throws IOException {
FileWriter writer = new FileWriter(string); //Creating a file
writer.write("Rank Pop_July_2016 Pop_July_2017 Growth %-Growth Country_Location "); //Writing the header information
for(UNpopulation entry:list){ // Writing the list into the file.
writer.append(entry+" ");
}
writer.close();
}
public static void main(String[] args) throws IOException {
list = new ArrayList<>(); // Declaring ArrayList object
populateListFromFile("PopulationTable-UN.txt"); // Populating the list from the file PopulationTable-UN.txt.
processData(); // to calculate growth percentage in every country
displayData(); //displaying the list
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.