1. There can be up to 100 names and gifts, not just 5.2. The loops to read in the names and gifts, should be sentinel-controlled loops, not count-controlled loops. That is, they should stop when the...

1 answer below »
1. There can be up to 100 names and gifts, not just 5.2. The loops to read in the names and gifts, should be sentinel-controlled loops, not count-controlled loops. That is, they should stop when the special value "*END*" is read in, and not always read in the entire array of names or gifts. See the slides for help on how to write a sentinel-controlled loop.3. To match the names with the gifts, the program should have one loop, not two. The loop should iterate through both the list of names and the list of gifts. It will be a single loop, but with two loop variables - one for the list of names and one for the list of gifts.


public static void main(String[] args) { Scanner sc = new Scanner(System.in); int totalNames =5; int totalGifts =5; String[] Names = new String [totalNames]; // Array for names String[] Gifts = new String [totalGifts]; // Array for gifts // while (sc.equals("END")) { for(int o = 0; o < names.length;o++)="" for="" loop="" for="" input="" the="" names="" {="" system.out.print("enter="" the="" names:");="" names[o]="sc.nextLine();" break;="" }="" }="" while(sc.equals("end"));{="" for(int="" p="0;" p="">< gifts.length;p++)="" for="" loop="" for="" input="" the="" gifts="" {="" system.out.print("enter="" the="" gifts:");="" gifts[p]="sc.nextLine();" break;="" }="" }="" int="" num="Names.length;" for(int="" j="1;"> 0 && Names[i-1].compareTo(t) > 0) { Names[i] = Names[i-1]; i--; } Names[i] = t; } System.out.println("*END*"); for(String s : Names) // print as sort when name first letter == gifts first letter { for (String g : Gifts) { if (s.charAt(0)==g.charAt(0)) { System.out.print(s +", " + g); System.out.println(); } else{ System.out.print(""); } } } System.out.println("*END*"); // end } COSC 215: Data Structures in Java COSC 215: Data Structures in Java Programming Assignment #1: Birthday Gifts Due: 1/26/22 Points: 100 Uses: Arrays, Sorting, I/O, Strings For your first assignment, you are write a program that reads in a list of people's name, followed by a list of birthday gifts, and matches the gifts to the people. The match is easy - the first letter of the birthday gift must be the same as the first letter of the person's name. For example, "Bobbie Saxon" could get a "Baseball bat" but not a "Football." "Xerxes Jones" could get a "Xerox machine." Note that not everyone may get a birthday gift - there may be no matching gift, and there may be birthday gifts that are not gifted. Details The program should read in a list of names from standard input, one name per line. Names always start with capital letters and may contain spaces. The special line "*END*" will indicate the end of the list of names. Then the program should read in a list of birthday gifts, also one per line. The birthday gifts always begin with capital letters and may have spaces in them as well. Again, the special line "*END*" indicates the end of the list of birthday gifts. There are at most 100 names and at most 100 birthday gifts, but there could be fewer than 100 of each. A match occurs when the first letter of the name is the same as the first letter of a birthday gift. If more than one gift matches a name, it doesn't matter which gift is matched. There is only one of each birthday gift, that is, the program can't match the same gift with multiple names. It may be that a person doesn't get a gift, and it may be that a gift is not given to any person. Input The input consists of a list of names, one name per line. Names may contain spaces. The list of names is ended by the special line "*END*". That list is followed by a list of birthday gifts, one per line. Birthday gifts may contain spaces. The list of birthday gift is ended by the special line "*END*". The list of names and the list of birthday gifts are not in any particular order, i.e., not sorted, and there may not be the same number of names as birthday gifts. There are at most 100 names and at most 100 gifts, but there could be less than 100 of each. Output The program will print out the names read in, one per line. The order of the names printed out does not need to be the same as the order in which they are read in. If the name is matched with a birthday gift, the birthday gift is printed out on the same line, separated from the name by a comma. If the person is not matched with a birthday gift, only the name is printed out on the line. After all the names are printed out, the program will print out the special line "*END*". Then program will print out the list of birthday gifts that were not given out. Additional Requirements The program should be efficient. It should sort the list of names and sort the list of birthday gifts and then match the names to the gifts. Proceed through the two sorted lists in lockstep. If a name matches a birthday gift (the first letters match), then print out both on a line, and advance to the next item of each list. If the first letter of the name precedes the first letter of the birthday gift, then there is no matching gift for the person, so the program should print out just the name, and advance to the next name (but does not advance to the next gift.) If the first letter of the name comes after the first letter of the birthday gift, then the birthday gift will not be given out. Add the birthday gift to the list of ungiven gifts, and advance to the next birthday gift (but do not advance to the next name.) If the program runs out of names first, then the remaining birthday gifts are not given out and should be added to the list of ungiven gifts. If the program runs out of birthday gifts firsts, then the remaining names in the list won't be given gifts. After coming to the end of both lists, the program should print out ""*END*" followed by the list of ungiven gifts, one per line. The program may use arrays, or ArrayLists (if you know about them.) The program should read from standard input and write to standard output. This is necessary so that the program can be tested by an automated program. Required Documentation The program should include a comment at the top with your name, the program name, date, a short program description, and the names of anyone who provided help with the program (tutors, classmates, other students, the web, etc.) Each section of code should be commented (in brief). Methods whose function is non-obvious from the name should have a comment line. Any tricky sections of code should be commented. Any code not your own should be commented (but, unless otherwise stated code that is not your own may only come from the book's code or code from class.) It is not necessary to document every line of code. For this program, you must write your own code - you are not to use code found on the net. Sample input Bobby Saxon Xerxes Jones Bob Smith Aaron Aardvark Professor Professorson Bruce Wayne Dick Grayson Alfred Pennyworth James Gordon John Smith *END* Doublemint gum Baseball bat Football Xerox machine Gumball Jonnycake Boomerang Trading card Christmas card Aardvark food Cash *END* Sample Output Aaron Aardvark, Aardvark food Alfred Pennyworth Bob Smith, Baseball bat Bobby Saxon, Boomerang Bruce Wayne Dick Grayson, Doublemint gum James Gordon, Jonnycake John Smith Professor Professorson Xerxes Jones, Xerox machine *END* Cash Christmas card Football Gumball Trading card Last modified: Jan. 11, 2022 Dr. Donald L. Simon, [email protected] http://www.mathcs.duq.edu/profs/simon.html COSC 215: Data Structures in Java Programming Assignment #1: Birthday Gifts Due: 1/26/22 Points: 100 Uses: Arrays, Sorting, I/O, Strings
Answered Same DayJan 28, 2022

Answer To: 1. There can be up to 100 names and gifts, not just 5.2. The loops to read in the names and gifts,...

Pawan answered on Jan 28 2022
104 Votes
To run the file
javac Main.java
java Main
Execution screenshot
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here