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
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
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(); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
