0

So I am having trouble knowing what to return in the bottom method any help would be appreciated .
Here are the specifications for completing the StringArray class:

(a) In the class StringArray write method numInArray,using the method signature below. numInArray should return the number of times the string s occurs in array a.

For example, assume that array a is as shown below.

[0] [1] [2] [3] [4] [5] [6]
java is nice so nice it is

Here are some examples of calls to method numInArray.

Method call Returned value
numInArray(a, "java") 1
numInArray(a, "is") 2
numInArray(a, "nice") 2
numInArray(a, "ja") 0

Complete method numInArray below.

// postcondition: returns the number of times s occurs in a
public static int numInArray(String[] a, String s)

< your code goes here >

b) Again using the class StringArray write method printAllNums,using the method signature below. For every string s in array a, printAllNums should return the string s, followed by a colon and a space, then followed by the number of times that string occurs in array b. Use the \n control character to print a new line at the end of each line.
For example, assume that arrays a and b are as shown below.

 [0]   [1]     [2]  [3]

a: ice cream is nice

 [0]    [1]  [2]    [3]  [4]    [5]  [6]

b: java is nice so nice it is

The call printAllNums(a, b) should produce the following string:

ice: 0
cream: 0
is: 2
nice: 2

In writing printAllNums, you will probably want to utilize the method numInArray, presuming that it works properly.

Complete method printAllNums below.

// postcondition: for all k such that 0 <= k < a.length,
// returns the string in a[k] followed by a colon
// and a space and the number of times that string
// occurs in b. (\n is used to generate a new line)
public static String printAllNums(String[] a, String[] b)

< your code goes here >

public class StringArray
{
    public static int numInArray(String[] a, String s)
    {
        int count = 0; 
        for(int i = 0; i < a.length; i++) 
        { 
            if (s.equals(a[i]))
            { 
                count++; 
            } 
        } 
        return count ; 
    }

    public static String printAllNums(String[] a, String[] b)
    {

for (String aString : a){ 
System.out.println(aString + ": " + numInArray(b, aString)); 
} 
return ;
 } 
}
2
Contributors
1
Reply
13
Views
2 Weeks
Discussion Span
Last Post by JamesCherrill
1

It looks like the spec is badly written
Although the method is called "printAll Nums" the spec does NOT say print the numbers, it says return a string (that could be printed).
So, don't be distracted by the misleading method name, just read the spec carefully and return "the string s, followed by a colon and a space, then followed by the number of times that string occurs in array b" (and you can then test it by printing the returned string)

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.