ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 06 Jun 2017 My Price 9.00

Splitting Gifts Between Twins

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:

  • We will use a String array by the name ofgiftsand an int array by the name ofcounts.giftsis the array of unique gift names that have been encountered so far and counts is the array of their counts. These two arrays have size 0 initially and during the execution of the program will have the same size. For each indexi, counts[i]is the count ofgifts[i].
  • Upon receiving a new name, we will do the following:
    • Use a method find to see whether the name appears in gifts and if so obtain the index at which the name occurs. The method takes two parameters, one is the String array in which a String is searched for and the other is the String to be searched for. The method returns the index of location of the String if the String appears and -1 otherwise.
    • If the index thus obtained is nonnegative, increment the count value at the index by 1.
    • Otherwise, add the new String togiftsand the count value of 1 to counts. Adding an element to an array can be done by:
      1. Defining a new array whose size is 1 plus the current size.
      2. Copying the entire elements from the current array to the new array at their corresponding positions.
      3. Add the new element to the new array as the last element.
      4. Assign the new array to the current array.

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]);
      }
    }
  }
}

Answers

(11)
Status NEW Posted 06 Jun 2017 03:06 AM My Price 9.00

-----------

Not Rated(0)