Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming The purpose of this project is to gain an understanding of usin

ID: 3702375 • Letter: J

Question

Java Programming

The purpose of this project is to gain an understanding of using a hash table, sorting and basic report generation techniques.

You will be provided an input file with each line containing a person’s first name, last name, and zip code separated by commas.

Instruction:

Create a hash map.

Read each line from the input file.

Parse the data into a Person class instance containing firstName, lastName, zipCode. If the parsing is not correct list that line as in error. Hint: there are standard Java library methods to help with the parsing.

Add the Person to the hash map. If a person by the same first and last name already exists, display an error and ignore this entry.

Sort the entries in the hash map by zip code, last name, and then first name. Hint: a HashMap does not support sorting. You will need to make a copy of all entries in the HashMap into another data structure.

Print a report header.

Print out the results sorted by zip code with a count of the numbers in each zip code.

Print a report summary.

Example of output:

A few sample lines in the input file might be:

Alan,Fontanella,55101

Bryan,Hills,40218

Andrew,Luck,55101

Joe,Smith,55102

Bill,Gates

Andrew,Luck,55102

For this input file, the generated report should look like:

Format error on line 5: Bill,Gates

Entry on line 6 already encountered: Andrew,Luck,55102

Company Zip Code Distribution – April 6, 2018

Zip Code: 40218

Hills, Bryan

Zip Code 40218: 1 resident

Zip Code: 55101

Fontanella, Alan

Luck, Andrew

Zip Code 55101: 2 residents

Zip Code: 55102

Smith, Joe

Zip Code 55102: 1 resident

Report Summary:

2 Records in error

3 Zip Codes encountered

4 Records Processed

Input file:

Alan,Fontanella,55101
Bryan,Hills,40218
Andrew,Luck,55101
Rob,Ehrlich,55118
Joe,Smith,55102
Ada,Friedman,55404
Lucia,Friedman,55404
Owen,Friedman,55404
Kyra,Axner,55408
Bill,Gates,55417
Emil,Kraepelin,55408
Abraham,Lincoln,55467
Isaac,Newton,55404
Albert,Einstein,55455
Neils,Bohr,55414
Charles,Darwin,55455
Louis,Pasteur,55404
Sigmund,Freud,55408
Galileo,Galilei,55478
Antoine,Laurent,55478
Johannes,Kepler,55488
Nicolaus,Copernicus,55404
Michael,Faraday,55414
James,Clerk,Maxwell,55488
Claude,Bernard,55488
Franz,Boas,55455
Werner,Heisenberg,55478
Linus,Pauling,55404
Rudolf,Virchow,55408
Erwin,Schrodinger,55478
Ernest,Rutherford,55488
Paul,Dirac,55414
Robe,Hrlich,55118
Andreas,Vesalius,90002
Ycho,Brahe,55404
Comte,De-Buffon,55478
Ludwig,Boltzmann,55408
Max,Planck,55455
Marie,Curie,55478
William,Herschel,55404
Charles,Lyell,55488
Pierre,De-Laplace,90002
Edwin,Hubble,55455
Joseph,Thomson,55488
Max,Born,55408
Francis,Crick,55478
Enrico,Fermi,90002
Leonard,Euler,55488
Justus,Liebig,55455
Arthur,Eddington,55478
William,Harvey,90002
Marcello,Malpighi,55455
Christiaan,Huygens,55478
Karl,Gauss,55414
Albrecht,Haller,55408
August,Kekule,55478
Robert,Koch,55408
Murray,Gell-Mann,90029
Emil,Fischer,55414
Dmitri,Mendeleev,90029
Sheldon,Glashow,60617
James,Watson,60617
John,Bardeen,55414
John,Von-Neumann,90029
Richard,Feynman,55478
Alfred,Wegener,55408
Stephen,Hawking,55455
Anton,Van-Leeuwenhoek,60629
Max,Von-Laue,90029
Euclid,,55417
Melinda,Gates,55417
Gregor,Mendel,55414
Heike,Kamerlingh-Onnesek
Max,Von,Laue,90029
Gustav,Kirchhoff
Hans,Bethe,60617
Homas,Hunt,Morgan,55414
Hermann,Von-Helmholtz,60629
Paul,Ehrlich,55455
Ernst,Mayr,90029
Charles,Sherrington,55467
Heodosius,Dobzhansky,60629
Max,Delbruck,55455
Jean,Lamarck,60617
William,Bayliss,60622
Noam,Chomsky,55417
Frederick,Sanger,60622
Louis,Broglie,55414
Carl,Linnaeus,55467
Jean,Piaget,55417
George,Simpson,60622
Claude,Levi-Strauss,60629
Lynn,Margulis,55467
Karl,Landsteiner,60622
Konrad,Lorenz,60608
Edward,Wilson,60630
Gertrude,Elion,55467
Hans,Selye,55417
Robert,Oppenheimer,60630
Edward,Teller,60630
Willard,Libby,55414
Bill,Gates,55467
Jonas,Salk,55417
bill,gates,55417
Melinda,Gates,55417

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.

Person.java
==========


public class Person {
private String firstName;
private String lastName;
private int zipcode;

public Person(String fname, String lname, int zip)
{
firstName = fname;
lastName = lname;
zipcode = zip;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getZipcode() {
return zipcode;
}

public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}


}

SortPersons.java
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class SortPersons {
public static void main(String[] args) {
String filename;
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter input filename: ");
filename = keyboard.next();

try {
//load the file
int lineNo = 0;
int errors = 0;
String line;
Scanner file = new Scanner(new File(filename));
HashMap<String, Person> map = new HashMap<String, Person>();


while(file.hasNextLine())
{
line = file.nextLine();
lineNo++;

String[] tokens = line.split(",");
if(tokens.length != 3)
{
System.out.println("Format error on line " + lineNo + ": " + line);
errors++;
}
else
{
String key = tokens[0] + " " + tokens[1];
key = key.toUpperCase();
if(map.containsKey(key))
{
System.out.println("Entry on line " + lineNo + " already encountered: " + line);
errors++;
}

else
{
Person p = new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2]));
map.put(key, p);
}
}
}
file.close();

ArrayList<Person> list = sortOnZip(map); //sort entries in map
int previousZip = 0;
int numZips = 0;
int count = 0;

//print report
System.out.println();
for(Person p : list){
if(p.getZipcode() != previousZip)
{
if(previousZip != 0)
System.out.println("Zip Code " + previousZip + ": " + count + " residents ");

numZips++;
count = 0;
previousZip = p.getZipcode();
System.out.println("Zip Code: " + previousZip);
}

System.out.println(p.getLastName()+", " + p.getFirstName());
count++;
}
System.out.println("Zip Code " + previousZip + ": " + count + " residents ");

System.out.println("Report Summary:");
System.out.println(errors + " Records in error");
System.out.println(numZips + " Zip Codes encountered");
System.out.println(list.size() + " Records Processed");


} catch (FileNotFoundException e) {

System.out.println(e.getMessage());
}

}

private static ArrayList<Person> sortOnZip(HashMap<String, Person> map)
{
ArrayList<Person> list = new ArrayList<Person>();
for(Person p : map.values())
list.add(p);

//selection sort on zipcode
for(int i = 0; i < list.size(); i++)
{
int minIdx = i;
for(int j = i + 1; j < list.size(); j++)
{
if(list.get(j).getZipcode() < list.get(minIdx).getZipcode())
minIdx = j;
}

if(minIdx != i)
{
Person temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}

return list;
}
}

output
=====
Enter input filename: sample1.txt
Format error on line 5: Bill,Gates
Entry on line 6 already encountered: Andrew,Luck,55102

Zip Code: 40218
Hills, Bryan
Zip Code 40218: 1 residents

Zip Code: 55101
Luck, Andrew
Fontanella, Alan
Zip Code 55101: 2 residents

Zip Code: 55102
Smith, Joe
Zip Code 55102: 1 residents

Report Summary:
2 Records in error
3 Zip Codes encountered
4 Records Processed

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote