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.
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...
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...
2 part question. Thumbs up promised. A) A block with mass m1 = 9.3 kg is...
2 part question. Thumbs up promised. A) A block with mass m1 = 9.3 kg is on an incline with an angle θ = 23° with respect to the horizontal. For the first question there is no friction, but for the rest of this problem the coefficients of friction are: μk = 0.2 and μs = 0.22. When there is no friction, what is the magnitude of the acceleration of the block? Now with friction, what is the magnitude of...
THUMBS UP IF YOU CITE! :D Purpose and Quality Statement: In this section, you will define...
THUMBS UP IF YOU CITE! :D Purpose and Quality Statement: In this section, you will define patient safety and the purpose of a quality plan. - Explain the purpose of implementing a quality plan. In your explanation, consider how accreditation standards drive an organization’s patient safety and quality initiatives. - Determine the healthcare organization’s commitment to patient safety and quality. Consider the mission statement and policies of the organization to guide your answer. - Describe the various stakeholder groups that...
Please show all work and calculations for a thumbs up! Suppose a company has a forklift...
Please show all work and calculations for a thumbs up! Suppose a company has a forklift but is considering purchasing a new electric lift truck that would cost $230000 and has operating cost of $25000 in the first year. For the remaining years, operating costs increase each year by 5% over the previous year’s operating costs. It loses 15% of its value every year from the previous year’s salvage value. The lift truck has a maximum life of 4 years....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT