Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***For beginners learning java****code simple as possible DB.TXT 8749e 1995-11-02 1-475-942-1615 Eva Destruction 1eb25 1980-02-13 1-651-260-8773 Bob Loblaw a1ed6 1982-10-10 1-732-572-2182 Penumbra Lua 6f90d 1965-11-18

***For beginners learning java****code simple as possible

DB.TXT

8749e 1995-11-02 1-475-942-1615 Eva Destruction

1eb25 1980-02-13 1-651-260-8773 Bob Loblaw

a1ed6 1982-10-10 1-732-572-2182 Penumbra Lua

6f90d 1965-11-18 1-425-215-7673 Sara Soda

2062c 1969-11-22 1-209-601-6270 Sy Borg

f734f 1973-01-19 1-213-944-8230 Jerry Hattrick

78075 1958-12-25 1-671-589-5690 Phil Anthropic

50889 1983-07-15 1-727-668-6289 Casper Bhoot

ea061 1971-03-19 1-604-380-2489 Squarebob Sponge Pants

52079 1957-08-28 1-226-515-1667 Helen Wheels

bdd10 1987-05-21 1-217-373-8703 Al Debaron

1c8ff 1973-01-08 1-443-616-3160 Jay Walker

4b7aa 1956-02-24 1-330-875-3842 Tim Buck, II

762ce 1991-04-10 1-626-403-5794 Anne Ominous

051d0 1978-11-15 1-678-869-8794 Art Rococo

4bd11 1981-05-25 1-714-786-4352 James Rivers

708a0 1967-05-17 1-223-969-3944 Pat Pending

2cc01 1967-10-10 1-670-457-5427 Sarah Bellum

86f4d 1971-09-21 1-510-479-0309 Lucinda D. Boltz

c16fc 1977-02-10 1-343-390-8956 Mickey Angels

e7c2a 1984-12-23 1-443-794-6553 C. Erol Madre

0221c 1980-05-28 1-246-584-4760 Mora Leigh Flexible

92233 1987-10-21 1-246-958-0440 Mischa Ternoff

67a1e 1959-04-07 1-417-632-7055 Les Active

cb4c2 1959-01-16 1-289-502-8337 Thomas Katz

2d886 1980-09-02 1-531-824-7428 Marge N. O'Hara

db3b5 1979-12-09 1-669-274-5143 Warren Pease

6404a 1971-03-14 1-441-966-3391 Kim Chee

5acee 1993-05-07 1-206-409-4897 Chester Peak

7a47f 1998-02-16 1-345-686-3902 James Bonn

c47be 1981-12-23 1-256-456-1013 James URL Jones

250b1 1971-11-16 1-242-234-0405 Thomas Foolery

715de 1997-10-27 1-541-983-1179 Kaye Serah

fe976 1990-02-25 1-253-986-1746 Jan Derr

53a69 1967-03-26 1-406-809-6518 Austin Tatious

145a4 1965-01-02 1-509-984-5444 Zbigniew Payne

f764e 1963-06-15 1-603-429-7578 Anne Hinga

e9ddd 1972-07-23 1-680-556-5252 Al Kaline

86e04 1997-07-12 1-620-985-0242 Champ Payne

38c9a 1960-08-25 1-332-442-0612 Tom Ollay

3c927 1955-11-01 1-204-914-8142 Erol Flintstone

c9ebd 1967-01-26 1-304-515-5362 Bea Faroni

002e0 1989-09-29 1-212-991-7893 Mary Anna Trench

50dd6 1985-12-26 1-608-262-6154 Phil O'Sophie

12d01 1966-03-12 1-717-525-9844 Dinah Might

30329 1977-04-01 1-612-308-4694 Chip Monk

45681 1963-02-11 1-503-645-1827 Al Feedersain

2b1d4 1984-04-18 1-215-408-4630 Sally Forth

ffd97 1979-02-09 1-763-704-0325 Noah Pinion

0f4ae 1978-11-14 1-242-915-1604 Sarah N. Dippity

d0c39 1955-04-08 1-226-597-3033 Al Italia

2fef7 1955-11-12 1-408-465-9176 Frank N. Stein

f923d 1963-10-29 1-281-258-7205 Yagotta B. Kidding

c333c 1970-03-04 1-500-865-5942 Catnip Evergreen

e0a08 1971-03-02 1-562-867-1518 Joe King

17d47 1961-02-01 1-613-300-5295 Karen Sympathy

d3d0c 1996-07-21 1-254-427-1723 Cassie O. Pia

d4daa 1998-12-03 1-607-883-3522 Moses Lonagin

Consider the following Person class. Its fields correspond to the fields

on each line in the db.txt file from homework 3. In db.txt, the ID is 5

hexadecimal digits. This fits in a 32-bit int. I could have used decimal

instead, but decimal would have required an extra column (and so an

extra byte per line).

The other fields are strings.

The constructor assumes the data is in the expected format. It takes, as

a string, a line from the db.txt file, and scans through the line,

picking out and saving the ID, the date of birth, the phone number, and

the person's name. Note the special handling to convert the ID from a

hexadecimal string to an int. Also note that Scanner's nextLine() method

is used for the name, but just next() for the other fields. This is

because the name could be any number of tokens in length. We just grab

all that is left on the line as the name.

toString() overrides toString() from the Object class. Notice how the ID

is converted back to hexadecimal for display.

class Person {

private final int id; // 5 hex digits fits in one 4B int, or a

// 10B UTF-16 String. Pick the smaller.

private final String dob; // date of birth

private final String phone;

private final String name;

static final int RADIX = 16; // hexadecimal (base 16)

public Person(final String line) {

// Could throw if there are too few tokens or if the first token

// isn't a hex value. Ignores anything past the fourth token.

final Scanner s = new Scanner(line);

id = s.nextInt(RADIX);

dob = s.next();

phone = s.next();

name = s.nextLine();

} // constructor

public int id() { // accessor/getter

return id;

} // id()

@Override

public String toString() {

return

Integer.toHexString(id) + " "

+ dob + " "

+ phone + " "

+ name;

} // toString()

} // class Person

Make a Java program that creates a Person object for each line in

db.txt. These Person objects must be stored in an ArrayList. Do

*not* modify Person.

Your program will consist of Person and new class called People. People

must have:

(1) A private final field of type ArrayList. ArrayList is in

java.util.

(2) A constructor that receives the name of the input file, reads the

file, and places each line of the file in the ArrayList.

(2a) Here's how to open the input file:

final Scanner in = new Scanner(new File(filename));

where filename is the name of the input file. File is in

java.io.

(2b) The constructor will loop while in.hasNextLine() returns true.

(2c) A person can be added to the ArrayList like this:

db.add(new Person(in.nextLine());

Here db is the ArrayList. You may use db, or some other

name.

(2d) Be sure to close the input file.

(2e) The constructor must be declared to throw

FileNotFoundException. Do *not* catch the exception in the

constructor, since that may result in a poorly-initialized People

instance. FileNotFoundException may be imported from java.io.

(3) A public function get() which receives a Person ID as an int,

searches the ArrayList for the matching Person, and returns the matching

Person. If no match is found, return null.

(4) A toString() method that overrides toString() in Object. This must

be tagged @Override. This method takes no arguments, and returns a

String. The returned String will be all the Person objects concatenated,

one per line (separated by newline characters).

(5) A main() function that creates a new People instance, passing it

"db.txt" as an argument. main() then prompts the user for an ID number,

reads the ID number, and displays the matching Person. If no matching

Person is found, display a message indicating that the ID is unknown.

Feel free to use the following code to prompt for and read an ID:

private static Scanner in = null;

private static String promptForInput(final String prompt) {

System.out.print(prompt);

if (in == null) // only need one Scanner

in = new Scanner(System.in);

final String result = in.nextLine();

if (result.equals("q"))

return null;

else return result;

} // promptForInput()

Note that get() and toString() perform *NO* I/O.

For full credit, do NOT index into the ArrayList. Use ForEach loops

(section 7.2.7 of the 10th edition of the text).

What to submit:

(1) Your Java source code (this is the file ending in .java).

(2) A screen shot showing two runs of your program, one where the

requested ID is found, and one where the ID is not found.

At the top of the file must be a comment identifying the programmer or

programmers.

image text in transcribedimage text in transcribedimage text in transcribed

8749e 1995-11-82 1-475-942-1615 Eva Destruction 1eb25 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design A Practical Approach

Authors: Marilyn Campbell

1st Edition

1587193175, 978-1587193170

Students also viewed these Databases questions