Question

In: Computer Science

*******FOR A BIG THUMBS UP******** ------INSTRUCTIONS--------- Coding standards note: The coding standards are in a document...

*******FOR A BIG THUMBS UP********

------INSTRUCTIONS---------

Coding standards note: The coding standards are in a document in a D2L module titled Coding Standards. See Grading Criteria for points lost if not followed.

Competencies being graded:

  1. Ability to implement a HashMap
  2. Ability to populate and print from a HashMap
  3. Ability to write items from a HashMap
  4. Read and write a binary file using ObjectInputStream and ObjectOutputStream
  5. Ability to read an English language problem description and design a multi class solution in Java
  6. Ability to follow given coding standards- in D2L content under Coding Standards.

Problem Statement:

Create a program that reads in the file people.dat from Homework 2. (Yes, you can use the code from Homework 1). Please make sure to include Person.java in your submission.

Implement your own HashMap implementation. You may use the HashMap example provided in the notes, but you MUST modify it to handle Person objects only and bring it completely up to coding standards to receive all points. You MAY NOT use java.util.HashMap or java.util.TreeMap in this problem.

After reading in the Person objects, store the Personobjects into your HashMap implementation using the unique ID as the key to the map rather than the ArrayList used in Homework 2. Then print the Person objects from the HashMap in a user friendly fashion. You may use iterator or for-each loop-> DO NOT JUST System.out.println THE HASHMAP- ITERATE THROUGH IT.   Hint: Modify your toString in Person to get a nice looking output.

------people.dat---------

¬í sr PersonM”fò(Ÿ# I idNumL cityt Ljava/lang/String;L    firstNameq ~ L lastNameq ~ xp t Bufordt Nanat Agyemansq ~ t Dulutht Josepht Andersonsq ~ t Lawrencevillet Kylet Brookssq ~ t Daculat Joshuat    Broughtonsq ~ t Lilburnt Demetrit Clarksq ~ t
Snellvillet Davidt Edwardssq ~ t Atlantat Jonit Elshanisq ~ t Decaturt Jacobt Fagan

------peopleFile.txt-------

Nana
Agyeman
Buford
1
Joseph
Anderson
Duluth
2
Kyle
Brooks
Lawrenceville
3
Joshua
Broughton
Dacula
4
Demetri
Clark
Lilburn
5
David
Edwards
Snellville
6
Joni
Elshani
Atlanta
7
Jacob
Fagan
Decatur
8

--------Person.java-----------

import java.io.Serializable;
public class Person implements Serializable
{
   public String firstName;
   public String lastName;
   public int idNum;
   public String city;

   /**
   * Default constructor used to create empty attributes
   */
   public Person()
   {
       firstName = "";
       lastName = "";
       idNum = 0;
       city = "";
   }

   /**
   * @param firstName
   * @param lastName
   * @param idNum
   * @param city
   */
   public Person(String firstName, String lastName, int idNum, String city)
   {
       this.firstName = firstName;
       this.lastName = lastName;
       this.idNum = idNum;
       this.city = city;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString()
   {
       return firstName + " " + lastName ;
   }

   /**
   * @return the firstName
   */
   public String getFirstName()
   {
       return firstName;
   }

   /**
   * @param firstName the firstName to set
   */
   public void setFirstName(String firstName)
   {
       this.firstName = firstName;
   }

   /**
   * @return the lastName
   */
   public String getLastName()
   {
       return lastName;
   }

   /**
   * @param lastName the lastName to set
   */
   public void setLastName(String lastName)
   {
       this.lastName = lastName;
   }

   /**
   * @return the idNum
   */
   public int getIdNum()
   {
       return idNum;
   }

   /**
   * @param idNum the idNum to set
   */
   public void setIdNum(int idNum)
   {
       this.idNum = idNum;
   }

   /**
   * @return the city
   */
   public String getCity()
   {
       return city;
   }

   /**
   * @param city the city to set
   */
   public void setCity(String city)
   {
       this.city = city;
   }

}

----------GeneratePeopleFile.java-----------

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class GeneratePeopleFile
{

   public static void main(String[] args)
   {
       // Starting point of program
       // open a file of Person class and read them into an ArrayList of Persons
       ArrayList<Person> people = new ArrayList<Person>();
       File peopleFile = new File("peopleFile.txt");
       // open a Scanner to read data from File
       Scanner peopleReader = null;
       try
       {
           peopleReader = new Scanner(peopleFile);
       } catch (FileNotFoundException e)
       {
           // TODO Auto-generated catch block
           System.out.println("File not found - terminating program");
           System.exit(0);
           e.printStackTrace();

       }

       // read one person at a time
       while (peopleReader.hasNext())
       {
           // read first name
           String firstName = peopleReader.next();
           String lastName = peopleReader.next();
           String city = peopleReader.next();
           int id = peopleReader.nextInt();
           // create new Person instance and add to ArrayList
           Person temp = new Person(firstName, lastName, id, city);
           people.add(temp);

       }

       // print info to user
       System.out.println("The people from the file are:");
       System.out.println(people);

       // write people to another file
       File secondPeopleFile = new File("people.dat");
       ObjectOutputStream peopleWrite = null;
       try
       {
           peopleWrite = new ObjectOutputStream(new FileOutputStream(secondPeopleFile));

           for (Person temp : people)
           {
               peopleWrite.writeObject(temp);
           }
           peopleWrite.close();
       } catch (IOException e)
       {
           System.out.println("Problems writing to file");

           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}

Solutions

Expert Solution

Short Summary:

  1. Implemented the program as per requirement
  2. Created hashmap with unique id as key and Person as value
  3. Used for loop iterations for hashmap and printed using toString method of Person class
  4. Attached source code and sample output


**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class GeneratePeopleFile
{

public static void main(String[] args)
{
// Starting point of program
// open a file of Person class and read them into an ArrayList of Persons
// ArrayList<Person> people = new ArrayList<Person>();

   //Declare hashmap with unique id as key and Person object as Value
HashMap<Integer, Person> personMap=new HashMap<>();
File peopleFile = new File("C:\\Users\\asus\\peopleFile.txt");
// open a Scanner to read data from File
Scanner peopleReader = null;
try
{
peopleReader = new Scanner(peopleFile);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("File not found - terminating program");
System.exit(0);
e.printStackTrace();

}

// read one person at a time
while (peopleReader.hasNext())
{
// read first name
String firstName = peopleReader.next();
String lastName = peopleReader.next();
String city = peopleReader.next();
int id = peopleReader.nextInt();
// create new Person instance and add to ArrayList
Person temp = new Person(firstName, lastName, id, city);

personMap.put(id, temp);
// people.add(temp);

}

// print info to user
System.out.println("The people from the file are:");

// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<Integer,Person> entry : personMap.entrySet())
System.out.println( entry.getValue().toString());

// personMap.forEach((k,v) -> System.out.println(v.toString()));

// write people to another file
File secondPeopleFile = new File("C:\\Users\\asus\\people.dat");
ObjectOutputStream peopleWrite = null;
try
{
peopleWrite = new ObjectOutputStream(new FileOutputStream(secondPeopleFile));
// using for-each loop for iteration over Map.entrySet()

for (Map.Entry<Integer,Person> entry : personMap.entrySet())
peopleWrite.writeObject(entry.getValue());

/* for (Person temp : people)
{
peopleWrite.writeObject(temp);
}*/
peopleWrite.close();
} catch (IOException e)
{
System.out.println("Problems writing to file");

// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Person.java

import java.io.Serializable;
public class Person implements Serializable
{
   public String firstName;
   public String lastName;
   public int idNum;
   public String city;

   /**
   * Default constructor used to create empty attributes
   */
   public Person()
   {
       firstName = "";
       lastName = "";
       idNum = 0;
       city = "";
   }

   /**
   * @param firstName
   * @param lastName
   * @param idNum
   * @param city
   */
   public Person(String firstName, String lastName, int idNum, String city)
   {
       this.firstName = firstName;
       this.lastName = lastName;
       this.idNum = idNum;
       this.city = city;
   }

   @Override
   public String toString() {
       return "[firstName=" + firstName + ", lastName=" + lastName + ", idNum=" + idNum + ", city=" + city + "]";
   }

   /**
   * @return the firstName
   */
   public String getFirstName()
   {
       return firstName;
   }

   /**
   * @param firstName the firstName to set
   */
   public void setFirstName(String firstName)
   {
       this.firstName = firstName;
   }

   /**
   * @return the lastName
   */
   public String getLastName()
   {
       return lastName;
   }

   /**
   * @param lastName the lastName to set
   */
   public void setLastName(String lastName)
   {
       this.lastName = lastName;
   }

   /**
   * @return the idNum
   */
   public int getIdNum()
   {
       return idNum;
   }

   /**
   * @param idNum the idNum to set
   */
   public void setIdNum(int idNum)
   {
       this.idNum = idNum;
   }

   /**
   * @return the city
   */
   public String getCity()
   {
       return city;
   }

   /**
   * @param city the city to set
   */
   public void setCity(String city)
   {
       this.city = city;
   }

}

Code Screenshot:

Person.java

Output:


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

Document The nursing process definition and all standards of practice.
Document The nursing process definition and all standards of practice.
Document The nursing process definition and all standards of practice.
Document The nursing process definition and all standards of practice.
Thumbs Up Will Be Given For Answer. A) Discuss some of the actions that managers may...
Thumbs Up Will Be Given For Answer. A) Discuss some of the actions that managers may engage in to erode shareholder value. B) What are some of the advantages and disadvantages associated with a firm’s expansion into international markets?
Document the process big marker uses to purchase new servers?
Document the process big marker uses to purchase new servers?
What is better, BMW or Mercedes in your opinion? Thumbs up for every answer!
What is better, BMW or Mercedes in your opinion? Thumbs up for every answer!
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article...
Please answer this question correctly and quickly for a thumbs up. In the Molly Anderson article assigned for this class, Professor Anderson describes her food systems vision. Thinking about your own food systems vision, what are three things you would like to change or affect in the food system? Please respond by listing one thing you would like to do individually, one thing you would like society to do collectively, and one thing you think the UW could do to...
FORECASTING , THE COMPLETE ANSWERS TO THESE QUESTIONS WILL RECEIVE A THUMBS UP! Weekly demand figures...
FORECASTING , THE COMPLETE ANSWERS TO THESE QUESTIONS WILL RECEIVE A THUMBS UP! Weekly demand figures at Hot Pizza are as follows: Week Demand 1 108 2 116 3 118 4 124 5 96 6 119 7 96 8 102 9 112 10 102 11 92 12 91 Estimate demand for the next 4 weeks using a 4-week moving average as well as simple exponential smoothing with α = 0.1. Evaluate the MAD, MAPE, MSE, bias, and TS in each...
Thumbs Up Will Be Given For Answer. Chapter Topic: The Organization of IB Please find an...
Thumbs Up Will Be Given For Answer. Chapter Topic: The Organization of IB Please find an INTERNATIONAL BUSINESS article to use. By doing so, YOU NEED to please visit websites such as  Reuters, Bloomberg, Wall Street Journal, etc, and put in the keywords "International Business" and the chapter topic "The Organization of IB​​​​​​​" on the website. You NEED to choose a relevant and interesting article that was made within the PAST COUPLE MONTHS that has to do with "International Business" and...
Thumbs Up Will Be Given For Answer. Please ELABORATE and give examples on the 2 courses...
Thumbs Up Will Be Given For Answer. Please ELABORATE and give examples on the 2 courses of action/strategies Delta Airlines will take that are listed below: 1st course of action: Emphasize policies and protocols aligned with the CDC and WHO to ensure customer safety to retain brand loyalty (for aftermath of pandemic). Do this through advertising/marketing/consistent communication from CEO himself/offer benefits and reliable customer service. 2nd course of action: Shift their focus to other sources of revenue by undertaking a...
Write up a document explaining the Markowitz model and Black-Litterman model. The document should be in...
Write up a document explaining the Markowitz model and Black-Litterman model. The document should be in PDF format. Note that Word 2010 and 2013 can save in / print to PDF format. About 500-1000 words (2-4 pages double-spaced) is fine. You can use the example in the Excel template if you feel that helps you explain more clearly. Note that detailed mathematical explanations or derivations are not required. The document should explain i) what the Markowitz model would suggest that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT