Answered step by step
Verified Expert Solution
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 sumaverage 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 classesfiles:
PopulationHandler Interface
In this interface class, you need to define two method prototypes.
void fileRead open file, handle FileNotFoundException, and
populaation space :
import java.ioFile;
import java.ioFileNotFoundException;
import java.util.Scanner;
public class PopulationSpace implements PopulationHandler
private String filePath;
public PopulationSpaceString filePath
this.filePath filePath;
@Override
public void fileRead
try Scanner scanner new Scannernew FilefilePath
double sum ; Placeholder for the sum
int count ; Placeholder for the count of valid lines
while scannerhasNextLine
String line scanner.nextLine;
if linetrimisEmpty
System.out.printlnNULL line detected!";
continue;
Process each line using space as the delimiter
String parts line.split;
if partslength
Parse the population number and perform necessary operations
try
int population Integer.parseIntpartstrim;
sum population;
count;
catch NumberFormatException e
Handle the exception if the population number is not a valid integer
Calculate the average
double average count sum count : ;
Output the result to the terminal
System.out.printfThe average population for the space delimiter is fn average;
catch FileNotFoundException e
System.out.printlnFile not found exception!";
@Override
public void getNumber
No need to implement this method for PopulationSpace
population equal:
import java.ioFile;
import java.ioFileNotFoundException;
import java.ioPrintWriter;
import java.util.Scanner;
public class PopulationEqual implements PopulationHandler
private String filePath;
public PopulationEqualString filePath
this.filePath filePath;
@Override
public void fileRead
try
Scanner scanner new Scannernew FilefilePath;
int sum ;
while scannerhasNextLine
String line scanner.nextLine;
String parts line.split;
if partslength
int population Integer.parseIntpartstrim;
sum population;
scanner.close;
System.out.printlnSum for equal sign delimiter: sum;
try PrintWriter writer new PrintWriterdataequal.txt
writer.printlnSum for equal sign delimiter: sum;
catch FileNotFoundException e
eprintStackTrace;
catch FileNotFoundException e
System.out.printlnFileNotFoundException: egetMessage;
@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 mainString args
Test case for FileNotFoundException
PopulationHandler popSpace new PopulationSpaceFileDoesNotExisttxt;
popSpace.fileRead;
System.out.println;
Test case for the Space delimiter
PopulationHandler popSpace new PopulationSpaceworldpopSpace.txt;
popSpacefileRead;
popSpacegetNumber;
System.out.println;
Test case for the Equal Sign delimiter
PopulationHandler popEqual new PopulationEqualworldpopEqualSign.txt;
popEqual.fileRead;
popEqual.getNumber;
worldpopspace.txt:
India
United States
worldpopEqualSign.txt:
China
United Kingdom In this task, you need to implement a JAVA project that calculates the sumaverage 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 classesfiles:
PopulationHandler Interface
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started