Reads an input record and prints the name if the current


The file named babynames80s.txt contains the data for names in the 80s, pasted directly from the Social Security Administration web site. The numbers in the files have comma separators:

1 Michael 663,087 3.4512 Jessica 469,271 2.5448

2 Christopher 555,660 2.8920 Jennifer 440,697 2.3899

3 Matthew 458,596 2.3869 Amanda 369,589 2.0042

4 Joshua 396,328 2.0628 Ashley 352,033 1.9090

5 David 383,258 1.9947 Sarah 272,376 1.4771
...

MODIFY the BabyNames.java program to handle them. Also modify the program so that it prompts the user for a file name.

Here is a sample program run:

Filename: babynames80s.txt

1  Michael Jessica

2  Christopher Jennifer

3  Matthew Amanda

4  Joshua Ashley

5  David Sarah

6  James Stephanie

7  Daniel Melissa

8  Robert Nicole

9  John Elizabeth

10  Joseph Heather

11  Jason Tiffany

12  Justin Michelle

13  Andrew Amber

14  Ryan Megan

15  William Amy

16  Brian Rachel

17  Brandon Kimberly

18  Jonathan Christina

19  Nicholas Lauren

20  Anthony Crystal

21  Eric Brittany

22  Adam Rebecca

23  Kevin Laura

24  Thomas Danielle

25  Steven Emily

26  Timothy Samantha

27  Richard Angela

28  Jeremy Erin

29  Jeffrey Kelly

30  Kyle Sara

31  Benjamin Lisa

32  Aaron Katherine

33  Charles Andrea

34  Mark Jamie

35  Jacob Mary

36  Stephen Erica

37  Patrick Courtney

38  Scott Kristen

39  Nathan Shannon

40  Paul April

41  Sean Katie

42  Lindsey

43  Kristin

44  Lindsay

45  Christine

46  Alicia

47  Vanessa

48  Maria

49  Kathryn

50  Allison

51  Julie

52  Anna

53  Tara

54  Kayla

55  Natalie

56  Victoria

57  Monica

58  Jacqueline

59  Holly

60  Kristina

61  Patricia

62  Cassandra

63  Brandy

64  Whitney

65  Chelsea

66  Brandi

67  Catherine

68  Cynthia

69  Kathleen

70  Veronica

71  Leslie

72  Natasha

73  Krystal

74  Stacy

75  Diana

76  Erika

77  Dana

78  Jenna

79  Meghan

80  Carrie

81  Leah

82  Melanie

83  Brooke

84  Karen

85  Alexandra

86  Valerie

87  Caitlin

88  Julia

89  Alyssa

90  Jasmine

91  Hannah

92  Stacey

93  Brittney

94  Susan

95  Margaret

96  Sandra

97  Candice

98  Latoya

99  Bethany

100  Misty

BELOW is the BabyNames.java code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BabyNames
{
public static final double LIMIT = 50;

public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new File("babynames.txt"));

RecordReader boys = new RecordReader(LIMIT);
RecordReader girls = new RecordReader(LIMIT);

while (boys.hasMore() || girls.hasMore())
{
int rank = in.nextInt();
System.out.print(rank + " ");
boys.process(in);
girls.process(in);
System.out.println();
}

in.close();
}
}

BELOW is the BabyNames.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BabyNames
{
public static final double LIMIT = 50;

public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new File("babynames.txt"));

RecordReader boys = new RecordReader(LIMIT);
RecordReader girls = new RecordReader(LIMIT);

while (boys.hasMore() || girls.hasMore())
{
int rank = in.nextInt();
System.out.print(rank + " ");
boys.process(in);
girls.process(in);
System.out.println();
}

in.close();
}
}

BELOW IS THE RecordReader.java
import java.util.Scanner;

/**
This class processes baby name records.
*/
public class RecordReader
{
private double total;
private double limit;

/**
Constructs a RecordReader with a zero total.
*/
public RecordReader(double aLimit)
{
total = 0;
limit = aLimit;
}

/**
Reads an input record and prints the name if the current total is less
than the limit.
@param in the input stream
*/
public void process(Scanner in)
{
String name = in.next();
int count = in.nextInt();
double percent = in.nextDouble();

if (total < limit) { System.out.print(name + " "); }
total = total + percent;
}

/**
Checks whether there are more inputs to process
@return true if the limit has not yet been reached
*/
public boolean hasMore()
{
return total < limit;
}
}

YOUR main class should be called BabyNames.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Reads an input record and prints the name if the current
Reference No:- TGS01249308

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)