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.
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;
}
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;
}
public static void main(String[] args) {
String[] p0;
String p1;
int count = 0;
p0 = new String[]{"table","chair","table","table","lamp","door","lamp","table","chair"};
p1 = "table";
if( runTest(1, p0, true, p1) ) count++;
p0 = new String[]{"a","a","a","b","b","b"};
p1 = "a";
if( runTest(2, p0, true, p1) ) count++;
p0 = new String[]{"icecream","peanuts","peanuts","chocolate","candy","chocolate","icecream","apple"};
p1 = "chocolate";
if( runTest(3, p0, true, p1) ) count++;
p0 = new String[]{"soul"};
p1 = "soul";
if( runTest(4, p0, true, p1) ) count++;
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
----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
----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.