The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Splitting Gifts Between Twins
You are a parent of young twins, Duey and Louie, who have just had their birthday party. Many guests came and left a lot of gifts for them. There are quite a few gift items with duplication. As a parent your plan is to given them only gifts with duplication and for such gifts the twins get an equal number of items. For example, if you have only one stuffed elephant, neither gets it, if you have four Frisbee, then each gets two of them, and if you have seven play-dough's, then each gets three. You want to know how many of what you want to give each. Huey, their older and only other sibling, will go through the gifts and read you off their names, and so you will be typing the names on the keyboard, and when the brother finishes you will know what you need to know.
The goal is to write the code for this task. Here is an example of the execution of the code.
% java SplitGifts
Enter the name of a gift (“0” to quit):
Harmonica
Enter the name of a gift (“0” to quit):
Toy_Guitar
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
Harmonica
Enter the name of a gift (“0” to quit):
Barbie
Enter the name of a gift (“0” to quit):
Barbie
Enter the name of a gift (“0” to quit):
Barney
Enter the name of a gift (“0” to quit):
Barney
Enter the name of a gift (“0” to quit):
Barbie
Enter the name of a gift (“0” to quit):
Baby_Bop
Enter the name of a gift (“0” to quit):
Barney
Enter the name of a gift (“0” to quit):
Harmonica
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
Toy_Guitar
Enter the name of a gift (“0” to quit):
Frisbee
Enter the name of a gift (“0” to quit):
0
Duey and Louie will each get 1 Harmonica.
Duey and Louie will each get 1 Toy_Guitar.
Duey and Louie will each get 1 Twix.
Duey and Louie will each get 1 Barbie.
Duey and Louie will each get 1 Barney.
Here is another example.
% java SplitGifts
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
HiC
Enter the name of a gift (“0” to quit):
HiC
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
Twix
Enter the name of a gift (“0” to quit):
0
Duey and Louie will each get 2 Twix.
Duey and Louie will each get 1 HiC.
To solve this problem, we will use the following strategy:
HERE IS THE JAVA TEMPLATE THAT NEEDS TO BE USED:
import java.util.*;
public class SplitGifts {
/**
* test whether a name matches an element in a String array
*/
public static int find(String[] nameArray, String name) {
// search for a match using a for loop
// return the location as soon as a match is found
return -1;// no match has been found
}
/**
* the main method
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] gifts = new String[0];
String[] giftsRevised;
int[] counts = new int[0];
int[] countsRevised;
String name;
int position;
do {
//--------------------
// output the result
//--------------------
System.out.println("Enter the name of a gift ("0" to quit): ");
name = keyboard.next();
if (!name.startsWith("0")) {
//--------------------
// search for a match using the find method
//--------------------
//--------------------
// if matched, increment the counter by 1
//--------------------
if ...
//--------------------
// otherwise, extend both arrays
//--------------------
else ...
} while (!name.startsWith("0"));
//--------------------
// output the result
//--------------------
for (int index = 0; index < gifts.length; index ++) {
if (counts[index] >= 2) {
System.out.printf("Each twin gets %d %s.%n",
counts[index]/2, gifts[index]);
}
}
}
}