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

2ip Code 40218: 1 resident Requirements Zip Code: 55101 Fontanella, Alan Luck, A

ID: 3713046 • Letter: 2

Question

2ip Code 40218: 1 resident Requirements Zip Code: 55101 Fontanella, Alan Luck, Andrew The purpose of this project is to gain an understanding of using a hash table, sorting and basic report generation techniques. 2ip Code 55101: 2 residents Zip Code: 55102 2ip Code 55102: 1 resident Report Summary: Smith, Joe You will be provided an input file with each line containing a person's first name, last name, and zip code separated by 1. Create a hash map 2. Read each line from the input file. 3. Parse the data into a Person class instance 2 Records in error 3 Zip Codes encountered 4 Records Processed containing firstName, lastName zipCode. If the parsing is not correctlist 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 wili need to make a copy of all entries in the HashMap into another data structure. Grading Criteria . Methods must be of appropriate length usually less than 1 page in length Appropriate private and public access modifiers must be used. Almost always data is private and methods are public although you may certainly have private or protected methods if appropriate. 4. . 5. . Meets program specifications as described above . The program is robust with no runtime errors or Program is readable 6. 7. 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 Comments o Use lavadocs conventions [Appendix H) o Include comments on the class as a whole 8. including A few sample lines in the input file might be: Description of program Your name (use @author) -Date (due or date you started or - Source of any "borrowed" code .Concise description of what ,55101 last modified) BryanHls, 40218 Andreck, 55101 Toe smich, 55102 o For each method include andreK.Luck, 55102 method does Arguments for each method (use @param) For this input file, the generated report should look like: -Returned value (use @returns) Exceptions which are thrown (@throws) Format error on line 5: ?nawaosa Entry on line already encountered: Andres k, 55102 .Consistent and correct indenting Meaningful identifiers Normal capitalization conventions are used The source of any "borrowed" code is clearly identified . Company 2ip Code Distribution April 6, 2018

Explanation / Answer

SourceCode:

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

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.

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