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

Db.text file People.java File People.java import java.io.File; import java.io.Fi

ID: 3707389 • Letter: D

Question

Db.text file

People.java File

People.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


class People {

private final ArrayList<Person> db;
private static Scanner in = null;
static final int RADIX = 16; // hexadecimal (base 16)

  
public People {

db = new ArrayList<Person>();
//
final Scanner in = new Scanner(new File(filename));
//
while(in.hasNextLine()){
db.add(new Person(in.nextLine()));
}
//
in.close();
}

public Person get(int personID){
Person p_ID = null;
// for each loop
for(Person p: db){
if (p.id() == personID){
p_ID = p;
//
break;
}
}
//
return p_ID;
}


public String toString() {
String returnString = "";
//
for(Person p: db){
//
returnString += p.toString() + " ";
}
return returnString;
} // toString()

private static String promptForInput {
System.out.print(prompt);
if (in == null)
in = new Scanner(System.in);
final String result = in.nextLine();
if (result.equals("q"))
return null;
else return result;
} // ()

/

public static void main(String[] args) {

String filename = "db.txt";

People people = new People;

  
String personIDString = promptForInput("Desired id: ");

  
int personID =Integer.parseInt(personIDString, RADIX);
  
Person p = people.get(personID);

if (p == null)
System.out.println("ID '" + personIDString + "' not found");
else
System.out.println(p.toString());
}
}

Write a Java class that extends People which has a
method getFullName() which receives an I.D. and returns the
corresponding person's name in the form last name, comma, rest of
name. Example:

> java Hw05
ID: 3bfb8
3bfb8's name is Lonagin, Moses

> java Hw05
ID: 1193f
1193f's name is Jones, James URL

> java Hw05
ID: 95c39
95c39's name is Stein, Frank N.

The main() function in your new class must create an object of your
class, and then ask the user for an ID (in hexadecimal), and finally
display the name by displaying the String returned by getFullName().


There is one name in db.txt which is a little more challenging than the
rest, Tim Buck, II. Do not spend time trying to make this name display
correctly. We'll let Juniors, IIs, IIIs, etc., wait for some later date
to be treated correctly.

Hint: if call your class for this homework assign 5. To create an instance, assign 5 must have a constructor, also named assign 5(). The first thing the constructor does is create the parent class constructor . The parent class constructor needs the name of the file, db.txt, so "db.txt" must be passed into your new constructor, by main(), as a string, and then passed to the People constructor.

749e 1995-11-82 1-475-942-1615 Eva Destruction Leb25 1980-02-13 1-651-260-8773 Bob Loblaw aled6 1982-10-10 1-732-572-2182 Penumbra Lua 6190d 1965-11-18 1-425-215-7673 Sara Soda 2062c 1969-11-22 1-209-691-627 Sy Borg f734f 1973-01-19 1-213-944-8230 Jerry Hattrick 78075 1958-12-25 1-671-589-5690 Phil Anthropic 50889 1983-97-15 1-727-668-6289 Casper Bhoot ea061 1971-03-19 1-604-380-2489 Squarebob Sponge Pants 52079 1957-88-28 1-226-515-1667 Helen Wheels bdd10 1987-85-21 1-217-373-8783 Al Debaron 1c8ff 1973-01-98 1-443-616-3160 Jay Walker 4b7aa 1956-02-24 1-330-875-3842 Tin Buck, 11 762ce 1991-84-10 1-626-403-5794 Anne Ominous 051d0 1978-11-15 1-678-869-8794 Art Rococo 4bd11 1981-85-25 1-714-786-4352 Janes Rivers 708a0 1967-85-17 1-223-969-3944 Pat Pending 2cc01 1967-10-10 1-670-457-5427 Sarah Bellum 8614d 1971-89-21 1-518-479-0309 Lucinda D. Boltz c16fc 1977-02-10 1-343-390-8956 Mickey Angels e7e2a 1984-12-23 1-443-794-6553 C. Erol Madre 0221c 1980-85-28 1-246-584-4760 Mora Leigh Flexible 92233 1987-10-21 1-246-958-0440 Mischa Ternoff 67ale 1959-84-87 1-417-632-7855 Les Active cb4c2 1959-01-16 1-289-592-8337 Thomas Katz 2d886 1980-09-82 1-531-824-7428 Marge N. 0 Hara db3b5 1979-12-09 1-669-274-5143 Warren Pease 6404a 1971-83-14 1-441-966-3391 Kim Chee 5acee 1993-85-87 1-206-409-4897 Chester Peak 7047f 1998-82-16 1-345-686-3902 Janes Bonn c47be 1981-12-23 1-256-456-1013 Janes URL Jones 250b1 1971-11-16 1-242-234-0485 Thomas Foolery 715de 1997-10-27 1-541-983-1179 Kaye Serah fe976 1990-02-25 1-253-986-1746 Jan Derr 53069 1967-83-26 1-406-899-6518 Austin Tatious 145a4 1965-01-82 1-509-984-5444 Zbigniew Payne 764e 1963-06-15 1-603-429-757B Anne Hinga c9ddd 1972-87-23 1-680-556-5252 Al Kaline B6e84 1997-07-12 1-628-985-0242 Champ Payne 38c9a 1960-88-25 1-332-442-0612 Tom Ollay 3c927 1955-11-01 1-284-914-8142 Erol Flintstone c9ebd 1967-01-26 1-304-515-5362 Bea Faroni 082e8 1989-09-29 1-212-991-7893 Mary Anna Trench 5edd6 1985-12-26 1-608-262-6154 Phil 0'Sophie 12d01 1966-03-12 1-717-525-9844 Dinah Might 30329 1977-04-81 1-612-308-4694 Chip Monk 45681 1963-02-11 1-503-645-1827 Al Feedersain 2b1d4 1984-94-18 1-215-408-4630 Sally Forth ffd97 1979-82-89 1-763-784-0325 Noah Pinion 0f4ae 1978-11-14 1-242-915-1604 Sarah N. Dippity dec39 1955-84-88 1-226-597-3033 Al Italia 2fef7 1955-11-12 1-408-465-9176 Frank N. Stein 1923d 1963-10-29 1-281-258-7205 Yagotta B. Kidding C333c 1978-83-04 1-508-B65-5942 Catnip Evergreen e8a08 1971-03-92 1-562-867-1518 Joe King 17d47 1961-82-81 1-613-300-5295 Karen Sympathy d3d0c 1996-07-21 1-254-427-1723 Cassie 0. Pia d4daa 1998-12-93 1-607-883-3522 Moses Lonagin

Explanation / Answer

/*
* Save this code for class Hw05 in HW05.java
* This Class extends People class
*/
import java.io.FileNotFoundException;

public class Hw05 extends People{

// Constructor with file name as argument and super() invikes super class Constructor i.e People
public Hw05(String filename) throws FileNotFoundException {
super(filename);
// TODO Auto-generated constructor stub
}

//Main entry point
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String filename = "db.txt";
      
       // Create People object
        People people = new People(filename);
        //Read ID from user for search
        String personIDString = People.promptForInput("Desired id: ");
       //Conver to integer with base 16 since id is base 16 in input file
        int personID =Integer.parseInt(personIDString, RADIX);
       // invoke getFullName methos
        Person p = people.getFullName(personID);
        //Check if match found
        if (p == null)
            System.out.println("ID '" + personIDString + "' not found");
        else
            System.out.println(p.toString());
}

}

======================

/*
* Save this People class in People.java file

*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class People {
    // Define array list to store Person object
    private final ArrayList<Person> db;
    //Scanner to read input
    private static Scanner in = null;
    static final int RADIX = 16; // hexadecimal (base 16)

    public People(String filename) throws FileNotFoundException {
        db = new ArrayList<Person>();
        //Read input file
        final Scanner in = new Scanner(new File(filename));
        //Loop thru all the lines
        while(in.hasNextLine()){
        // Read a line from input file
        String text = in.nextLine();
        //Create person object
        Person p = new Person();
        //To split the columns in file i.e. ID,DOB,Phonenumber
        StringTokenizer st = new StringTokenizer(text, " ");
        //st.nextToken() returns first column and subsequent call return next columns and so on
      int id = Integer.parseInt(st.nextToken(), 16);
      String dob = st.nextToken();
      String phonenumber = st.nextToken();
      // Get name
      String lastname = st.nextToken();
      String restname = "";
      while(st.hasMoreTokens()) {
          restname+=st.nextToken()+" ";
         }
      //set values to Person object
      p.setId(id);
      p.setDob(dob);
      p.setLastname(lastname);
      p.setPhonenumber(phonenumber);
      p.setRestName(restname);
      //add to arraylist
           db.add(p);
        }
        //
        in.close();
    }
    public Person getFullName(int personID){
        String fullName = "";
        // for each loop to loop thru all the Person object in Array
        for(Person p: db){
            //if match found return Person object
            if (p.getId() == personID){
               return p;
             
            }
        }
     
        return null;
    }

    public String toString() {
        String returnString = "";
        //
        for(Person p: db){
           //
            returnString += p.toString() + " ";
        }
        return returnString;
    } // toString()
    protected static String promptForInput(String prompt) {
        System.out.print(prompt);
        if (in == null)               
            in = new Scanner(System.in);
        final String result = in.nextLine();
        if (result.equals("q"))
            return null;
        else return result;
    }
  
}

=========================

/*
* Save this class in Person.java
*/

import java.util.StringTokenizer;

public class Person {

int id;
String dob;
String phonenumber;
String lastname;
String restName;
public Person() {

}

//Just getter and Setter methods.
public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getRestName() {
return restName;
}

public void setRestName(String restName) {
this.restName = restName;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}

//To String to return firstname and restname combined with id
public String toString() {
return Integer.toHexString(this.id)+"'s name is "+this.lastname+", " +this.restName;
}

}

//===========================
//Output
//Desired id: ea061
//ea061's name is SquareBob, SpongePants

//Desired id: 1eb25
//1eb25's name is Bob, Loblaw

?==========NEW CODE ===========

/*
* Save this code for class Hw05 in HW05.java
* This Class extends People class
*/
import java.io.FileNotFoundException;

public class Hw05 extends People{

// Constructor with file name as argument and super() invikes super class Constructor i.e People
public Hw05(String filename) throws FileNotFoundException {
super(filename);
// TODO Auto-generated constructor stub
}

//Main entry point
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String filename = "db.txt";

// Create People object
People people = new People(filename);
// Read ID from user for search
String personIDString = People.promptForInput("Desired id: ");
// Conver to integer with base 16 since id is base 16 in input file
int personID = Integer.parseInt(personIDString, RADIX);
// invoke getFullName methos
Person p = people.getFullName(personID);
// Check if match found
if (p == null)
System.out.println("ID '" + personIDString + "' not found");
else
System.out.println(p.toString());
}

}

======================

/*
* Save this People class in People.java file

*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class People {
    // Define array list to store Person object
    private final ArrayList<Person> db;
    //Scanner to read input
    private static Scanner in = null;
    static final int RADIX = 16; // hexadecimal (base 16)

    public People(String filename) throws FileNotFoundException {
       db = new ArrayList<Person>();
// Read input file
final Scanner in = new Scanner(new File(filename));
// Loop thru all the lines
while (in.hasNextLine()) {
// Read a line from input file
String text = in.nextLine();
// Create person object
Person p = new Person(text);
// add to arraylist
db.add(p);
}
//
in.close();
    }
    public Person getFullName(int personID){
        String fullName = "";
        // for each loop to loop thru all the Person object in Array
        for(Person p: db){
            //if match found return Person object
            if (p.getId() == personID){
               return p;
             
            }
        }
     
        return null;
    }

    public String toString() {
        String returnString = "";
        //
        for(Person p: db){
           //
            returnString += p.toString() + " ";
        }
        return returnString;
    } // toString()
    protected static String promptForInput(String prompt) {
        System.out.print(prompt);
        if (in == null)               
            in = new Scanner(System.in);
        final String result = in.nextLine();
        if (result.equals("q"))
            return null;
        else return result;
    }
  
}

=========================

/*
* Save this class in Person.java
*/

import java.util.StringTokenizer;

public class Person {

int id;
String dob;
String phonenumber;
String lastname;
String restName;
public Person() {

}

public Person(String line) {

StringTokenizer st = new StringTokenizer(line, " ");
// st.nextToken() returns first column and subsequent call return next
// columns and so on
int id = Integer.parseInt(st.nextToken(), 16);
String dob = st.nextToken();
String phonenumber = st.nextToken();
// Get name
String lastname = st.nextToken();
String restname = "";
while (st.hasMoreTokens()) {
restname += st.nextToken() + " ";
}
this.id = id;
this.dob = dob;
this.lastname = lastname;
this.restName = restname;

}

//Just getter and Setter methods.
public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getRestName() {
return restName;
}

public void setRestName(String restName) {
this.restName = restName;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}

//To String to return firstname and restname combined with id
public String toString() {
return Integer.toHexString(this.id)+"'s name is "+this.lastname+", " +this.restName;
}

}

//===========================
//Output
Desired id: ea061
ea061's name is SquareBob, SpongePants

Desired id: 1eb25
1eb25's name is Bob, Loblaw

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