Design and implement an application name the driver class Pa

Design and implement an application (name the driver class Part3) that reads a sequence of up to 25 pairs of names and postal codes for individuals from a file named activity 1. inp (E.g. each line of the data file should contain a person\'s name and a standard 5 digit zip code). Read in the data from the input file and store the data in an object designed to store a first name (string), last name (string) and postal code (integer). Assume each line of input will contain two strings followed by an integer value, each separated by white space. Your data should be stored in a Person class that you design that contains a default (no argument) constructor, accessors and mutators for all data, and a toString method used in the printing. After the input has been read in, use a for loop to iterate through the 25 objects and print the string representation of each data object by calling your toString method for the class that stores the data. You are required to create your own input file and to include it in the jar file you will use to deliver your lab. Your program should work though with any input file following the described format. Use an array of Person objects to store the data objects.

Solution


/**
*
* The java class Person that represetnts
* the object of person*/
//Person.java
public class Person {

   private String firstName;
   private String lastName;
   private int zipcode;
  
   public Person() {
       firstName=\"\";
       lastName=\"\";
       zipcode=0;
   }
  
   //Mutator/setter methods
   public void setFirstName(String firstName){
       this.firstName=firstName;
   }
   public void setLastName(String lastName){
       this.lastName=lastName;
   }
   public void setZipCode(int zipcode){
       this.zipcode=zipcode;
   }
  
   //Accessor/getter methods
   public String getFirstName(){
       return firstName;
   }
   public String getLastName(){
       return lastName;
   }
   public int getZipCode(){
       return zipcode;
   }
  
   //Returns string description of the Person object
   @Override
   public String toString() {      
       return String.format(\"%-15s%-15s%-10d\", firstName,lastName,zipcode);
   }
      
}

----------------------------------------------------------------------------------------------------------------------------

/**
* the java program that reads a text file
* called activity1.txt and stores the values in
* Person class and print the person object
* data to console.
* */
//Part3.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Part3
{
   public static void main(String[] args)
   {  
       String fileName=\"activity1.txt\";      
       Scanner filescanner=null;
      
       //Create an array of type Person class
       Person[] persons=new Person[25];
       int index=0;
      
       try
       {
           //Create a Scanner class object
           filescanner=new Scanner(new File(fileName));      
           String line=\"\";      
           //read text data from file
           while(filescanner.hasNextLine())
           {
               //read line from file
               line=filescanner.nextLine();
              
               //split the line using space delimiter
               String data[]=line.split(\" \");
               String firstName=data[0];
               String lastName=data[1];
               int zipcode=Integer.parseInt(data[2]);
              
               //create a person object
               persons[index]=new Person();
               //Set first name, last name and zip code
               persons[index].setFirstName(firstName);
               persons[index].setLastName(lastName);              
               persons[index].setZipCode(zipcode);
              
               index=index+1;
           }          
           System.out.printf(\"%-15s%-15s%-15s\ \",\"FirstName\",\"LastName\",\"Zipcode\");
           System.out.printf(\"-------------------------------------\ \");
      
           //print persons objects to console
           for (int i = 0; i < persons.length; i++)
           {
               System.out.println(persons[i]);
           }
       }
       catch (FileNotFoundException e)
       {
           System.out.println(e.getMessage());
       }  
   }  
}//end of the Part3 class

----------------------------------------------------------------------------------------------------------------------------

activity1.txt

Hilton Barile 12345
Bernardine Ewart 12346
Shellie Przybyla 12347
Odell Perlmutter 12348
Bob Ewell 12335
Gillian Ledford 12367
Derick Ramsburg 16745
Monet Caudell 18945
Amalia Bakken 23456
Bernardo Macgillivray 45676
Erick Froehlich 45454
Lakia Velasques 34654
Mathew Zahn 66767
Jutta Levey 68676
Brent Fallis 67856
Carri Laflamme 53435
Millard Teegarden 34543
Diane Hardisty 78678
Vi Weidemann 78645
Iona Kitchin 32424
Sheri Yuan 23245
Franchesca Goettl 45354
Audry Hanes 54353
Evangelina Dansby 54354
Leatha Minear 87868

----------------------------------------------------------------------------------------------------------------------------

sample output:

FirstName      LastName       Zipcode      
-------------------------------------------------
Hilton         Barile         12345   
Bernardine     Ewart          12346   
Shellie        Przybyla       12347   
Odell          Perlmutter     12348   
Bob            Ewell          12335   
Gillian        Ledford        12367   
Derick         Ramsburg       16745   
Monet          Caudell        18945   
Amalia         Bakken         23456   
Bernardo       Macgillivray   45676   
Erick          Froehlich      45454   
Lakia          Velasques      34654   
Mathew         Zahn           66767   
Jutta          Levey          68676   
Brent          Fallis         67856   
Carri          Laflamme       53435   
Millard        Teegarden      34543   
Diane          Hardisty       78678   
Vi             Weidemann      78645   
Iona           Kitchin        32424   
Sheri          Yuan           23245   
Franchesca     Goettl         45354   
Audry          Hanes          54353   
Evangelina     Dansby         54354   
Leatha         Minear         87868   

 Design and implement an application (name the driver class Part3) that reads a sequence of up to 25 pairs of names and postal codes for individuals from a file
 Design and implement an application (name the driver class Part3) that reads a sequence of up to 25 pairs of names and postal codes for individuals from a file
 Design and implement an application (name the driver class Part3) that reads a sequence of up to 25 pairs of names and postal codes for individuals from a file
 Design and implement an application (name the driver class Part3) that reads a sequence of up to 25 pairs of names and postal codes for individuals from a file

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site