Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this task, you need to implement a JAVA project that calculates the sum / average of population numbers by parsing them from text files

In this task, you need to implement a JAVA project that calculates the sum/average of population numbers
by parsing them from text files (using various delimiters, such as spaces, or an equal sign). For implementation, this project consists of the following classes/files:
1). PopulationHandler (Interface)
In this interface class, you need to define two method prototypes.
void fileRead()// open file, handle FileNotFoundException, and
populaation space :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PopulationSpace implements PopulationHandler {
private String filePath;
public PopulationSpace(String filePath){
this.filePath = filePath;
}
@Override
public void fileRead(){
try (Scanner scanner = new Scanner(new File(filePath))){
double sum =0; // Placeholder for the sum
int count =0; // Placeholder for the count of valid lines
while (scanner.hasNextLine()){
String line = scanner.nextLine();
if (line.trim().isEmpty()){
System.out.println("NULL line detected!");
continue;
}
// Process each line using space as the delimiter
String[] parts = line.split("");
if (parts.length ==2){
// Parse the population number and perform necessary operations
try {
int population = Integer.parseInt(parts[1].trim());
sum += population;
count++;
} catch (NumberFormatException e){
// Handle the exception if the population number is not a valid integer
}
}
}
// Calculate the average
double average =(count >0)? sum / count : 0;
// Output the result to the terminal
System.out.printf("The average population for the space delimiter is %.2f%n", average);
} catch (FileNotFoundException e){
System.out.println("File not found exception!");
}
}
@Override
public void getNumber(){
// No need to implement this method for PopulationSpace
}
}
population equal:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class PopulationEqual implements PopulationHandler {
private String filePath;
public PopulationEqual(String filePath){
this.filePath = filePath;
}
@Override
public void fileRead(){
try {
Scanner scanner = new Scanner(new File(filePath));
int sum =0;
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] parts = line.split("=");
if (parts.length ==2){
int population = Integer.parseInt(parts[1].trim());
sum += population;
}
}
scanner.close();
System.out.println("Sum for equal sign delimiter: "+ sum);
try (PrintWriter writer = new PrintWriter("data_equal.txt")){
writer.println("Sum for equal sign delimiter: "+ sum);
} catch (FileNotFoundException e){
e.printStackTrace();
}
} catch (FileNotFoundException e){
System.out.println("FileNotFoundException: "+ e.getMessage());
}
}
@Override
public void getNumber(){
// No specific implementation needed for getNumber() in this class
}
}
population handler:
// PopulationHandler interface
public interface PopulationHandler {
void fileRead();
void getNumber();
}
main:
public class Main {
public static void main(String[] args){
// Test case for FileNotFoundException
PopulationHandler popSpace = new PopulationSpace("./FileDoesNotExist.txt");
popSpace.fileRead();
System.out.println("");
// Test case for the Space delimiter
PopulationHandler popSpace2= new PopulationSpace("./worldpop_Space.txt");
popSpace2.fileRead();
popSpace2.getNumber();
System.out.println("");
// Test case for the Equal Sign delimiter
PopulationHandler popEqual = new PopulationEqual("./worldpop_EqualSign.txt");
popEqual.fileRead();
popEqual.getNumber();
}
}
worldpop_space.txt:
India 1147995898
United States 303824646
worldpop_EqualSign.txt:
China =1330044605
United Kingdom =60943912In this task, you need to implement a JAVA project that calculates the sum/average of population numbers
by parsing them from text files (using various delimiters, such as spaces, or an equal sign). For implemen-
tation, this project consists of the following classes/files:
. PopulationHandler (Interface)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions