Click here to Skip to main content
15,884,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone suggest me that what changes do I have to make in findBestSeller() method to get the desired output? My 2 outputs are correct but 3 are wrong out of 5. Thanks in advance.

Java
          public class QuizIIC
     {
      
	public static String findBestSeller(String[] items)
	{

     int count = 1, tempCount;
     String popular = items[0];
     String temp = "0";
     for (int i = 0; i < (items.length - 1); i++)
     {
      temp = items[i];
      tempCount = 0;
      for (int j = 1; j < items.length; j++)
      {
       if (temp == items[j])
        tempCount++;
      }
    if (tempCount > count)
    {
      popular = temp;
      count = tempCount;
    }
     }
     return popular;
    }	
   
  //Boolean runTest Method    
   
      private static boolean runTest(int testNum, String[] p0, boolean hasAnswer, String p1) 
       {
		System.out.print("Test #" + testNum + ": [" + "{");
		for (int i = 0; p0.length > i; ++i) {
			if (i > 0) {
				System.out.print(",");
			}
			System.out.print("\"" + p0[i] + "\"");
		}
		System.out.print("}");
		System.out.println("]");
		String answer;
		answer = QuizIIC.findBestSeller(p0);
		boolean res;
		res = true;
		if (hasAnswer) {
			System.out.println("Desired answer:");
			System.out.println("\t" + "\"" + p1 + "\"");
		}
		System.out.println("Your answer:");
		System.out.println("\t" + "\"" + answer + "\"");
		if (hasAnswer) {
			res = answer.equals(p1);
		}
		if (!res) {
			System.out.println("Sorry!");
		} else if (hasAnswer) {
			System.out.println("Correct!");
		} 
		System.out.println();
		return res;
	}

//Main Method
   
	public static void main(String[] args) {
		String[] p0;
		String p1;
      int count = 0;		
		// ----- test 1 -----
		p0 = new String[]{"table","chair","table","table","lamp","door","lamp","table","chair"};
		p1 = "table";
		if( runTest(1, p0, true, p1) ) count++;
		// ------------------
		
		// ----- test 2 -----
		p0 = new String[]{"a","a","a","b","b","b"};
		p1 = "a";
		if( runTest(2, p0, true, p1) ) count++;
		// ------------------
		
		// ----- test 3 -----
		p0 = new String[]{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","apple"};
		p1 = "chocolate";
		if( runTest(3, p0, true, p1) ) count++;
		// ------------------
		
		// ----- test 4 -----
		p0 = new String[]{"soul"};
		p1 = "soul";
		if( runTest(4, p0, true, p1) ) count++;
		// ------------------
		
		// ----- test 5 -----
		p0 = new String[]{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"};
		p1 = "a";
		if( runTest(5, p0, true, p1) ) count++;
		// ------------------
      
		System.out.print( count + " out of 5" );
		if (count == 5) 
      {
			System.out.println("!");
		}
      else 
      {
         System.out.println(".");
      }
	}
    }  


What I have tried:

//Desired Output

Java
  ----jGRASP exec: java QuizIIC -Xlint:unchecked
Test #1: [{"table","chair","table","table","lamp","door","lamp","table","chair"}]
Desired answer:
    "table"
Your answer:
    "table"
Correct!

Test #2: [{"a","a","a","b","b","b"}]
Desired answer:
    "a"
Your answer:
    "a"
Correct!

Test #3: [{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","apple"}]
Desired answer:
    "chocolate"
Your answer:
    "chocolate"
Correct!

Test #4: [{"soul"}]
Desired answer:
    "soul"
Your answer:
    "soul"
Correct!

Test #5: [{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"}]
Desired answer:
    "a"
Your answer:
    "a"
Correct!

5 out of 5!

 ----jGRASP: operation complete.


// My Output

Java
    ----jGRASP exec: java QuizIIC
 Test #1:          [{"table","chair","table","table","lamp","door","lamp","table","chair"}]
   Desired answer:
   "table"
 Your answer:
  "table"
  Correct!

Test #2: [{"a","a","a","b","b","b"}]
Desired answer:
"a"
 Your answer:
"b"
 Sorry!

Test #3:       [{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","app le"}]
Desired answer:
"chocolate"
Your answer:
"peanuts"
Sorry!

Test #4: [{"soul"}]
Desired answer:
"soul"
Your answer:
"soul"
Correct!

 Test #5: [{"a","b","b","b","c","c","c","c","a","a","a","a","b","b"}]
 Desired answer:
 "a"
 Your answer:
  "b"
 Sorry!

 2 out of 5.

 ----jGRASP: operation complete.
Posted
Updated 15-Nov-16 22:03pm
v3
Comments
Patrice T 15-Nov-16 17:31pm    
What is the rule to choose the winner when 2 or more have max count ?
[no name] 15-Nov-16 17:47pm    
And the reason you cannot debug this yourself is .... ?

I am not sure what the following code is supposed to do, but it looks a little suspect to me.
Java
for (int i = 0; i < (items.length - 1); i++)
{
 temp = items[i];
 tempCount = 0;
 for (int j = 1; j < items.length; j++)
 {
  if (temp == items[j])
   tempCount++;
 }

For each element in the array you compare it with all the others. But you do the full array compare each time so that a lot of the comparisons will be repeated, suggesting that you will get invalid results in some cases.

Maybe it should really be something like:
Java
for (int i = 0; i < (items.length - 1); i++)
{
 temp = items[i];
 tempCount = 0;
 for (int j = i; j < items.length; j++) // j should start at the next item after temp.
 {
  if (temp == items[j])
   tempCount++;
 }

You are also resetting tempcount for each time round the outer loop so when this finishes it will most likely not have the value you are looking for.
 
Share this answer
 
You fail test #2 because this loop
Java
for (int j = 1; j < items.length; j++)

start counting at second word.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900