There are at least 3 problems/bugs in the program. Find and correct each of the
ID: 3776941 • Letter: T
Question
There are at least 3 problems/bugs in the program. Find and correct each of the problems. Be sure to verify that you have found all of the bugs by showing your program to one of the LAs or the TA in the lab. You must be able to demonstrate how to do breakpoints, how to trace the program, line by line, and how to watch the variables. Additionally, make sure you put a comment inside of each method, problem1(), problem2(), and problem3(), that describes in detail, how you solved the "bug" in the method. This will be part of your grade in this lab. Good luck!
package Debug;
/**
*
* @author kcria004
*/
public class FixMeDomain
{
boolean stop = false;
int count = 100;
String choice = "";
public void problem1()
{
while(!stop)
{
while(count > 0)
{
System.out.println("Count is: " + count);
}
do
{
System.out.println("Count is " + count);
}
while(count < 50);
}
}
public void problem2()
{
for(int i = 0, y = 20; i < 10 && y > 10; i--, y++)
{
System.out.println("Something");
switch(i)
{
case 0:
System.out.println("Case 0");
case 1:
System.out.println("Case 1");
case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
case 4:
System.out.println("Case 4");
case 5:
System.out.println("Case 5");
case 6:
System.out.println("Case 6");
case 7:
System.out.println("Case 7");
case 8:
System.out.println("Case 8");
case 9:
System.out.println("Case 9");
case 10:
System.out.println("Case 10");
default:
System.out.println("Default");
}
}
}
public void problem3()
{
String word = "supercalifragilisticexpialidocious";
int iCount = 0;
int i = 0;
int index;
while (i < word.length())
{
index = word.indexOf("i", i);
if (index > -1)
{
System.out.println("Got it!");
iCount++;
}
i++;
}
System.out.println("There are this many i's in " + word + ": " + iCount);
}
}
package Debug;
public class FixMeTester
{
public static void main(String[] args)
{
FixMeDomain fixMe = new FixMeDomain();
fixMe.problem1();
fixMe.problem2();
fixMe.problem3();
}
}
Explanation / Answer
************************FixMeTester.java****************************
public class FixMeTester
{
public static void main(String[] args)
{
FixMeDomain fixMe = new FixMeDomain();
fixMe.problem1();
fixMe.problem2();
fixMe.problem3();
}
}
****************************FixMeDomain.java********************************************
public class FixMeDomain
{
boolean stop = false;
int count = 100;
String choice = "";
public void problem1()
{
int stopCount=105; //explained below
while(!stop) //this while loop will execute for infinite time
//to end this we set stop = true at the end of do while loop
{
//this while(count > 0) loop run for infinte loop
//count =100 which is always greater than 0 so infinite loop.
//to fix this use stopCount variable, so modifying the following loops
while(count>0 && count<stopCount)
{
System.out.println("Count is: " + count);
stopCount--;
}
do
{
System.out.println("Count is " + count);
}
while(count < 50 );
stop=true;
}
}
public void problem2()
{
// in this loop
//for(int i = 0, y = 20; i < 10 && y > 10; i--, y++)
// as i=0 and i-- will go for infinite times.
//also y=20 and y++ will execute for infinte times.
//do changing i-- to i++ AND y++ to y--
//so corrected loop will be
for(int i = 0, y = 20; i < 10 && y > 10; i++, y--)
{
System.out.println("Something");
//also add break statement to each case except default
switch(i)
{
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
case 2:
System.out.println("Case 2");
break;
case 3:
System.out.println("Case 3");
break;
case 4:
System.out.println("Case 4");
break;
case 5:
System.out.println("Case 5");
break;
case 6:
System.out.println("Case 6");
break;
case 7:
System.out.println("Case 7");
break;
case 8:
System.out.println("Case 8");
break;
case 9:
System.out.println("Case 9");
break;
case 10:
System.out.println("Case 10");
break;
default:
System.out.println("Default");
}
}
}
public void problem3()
{
String word = "supercalifragilisticexpialidocious";
int iCount = 0;
// int i = 0; no use of this i variable
//initialize the index=0
int index = 0;
//change following while loop's condition from
//while (i < word.length()) to while (index>-1)
while (index>-1)
{
// change index = word.indexOf("i", i) to following
index = word.indexOf("i",index+1);
if (index > -1)
{
System.out.println("Got it!");
iCount++;
}
// i++; no use
}
System.out.println("There are this many i's in " + word + ": " + iCount);
}
}
*******************************output********************************************************
Count is: 100
Count is: 100
Count is: 100
Count is: 100
Count is: 100
Count is 100
Something
Case 0
Something
Case 1
Something
Case 2
Something
Case 3
Something
Case 4
Something
Case 5
Something
Case 6
Something
Case 7
Something
Case 8
Something
Case 9
Got it!
Got it!
Got it!
Got it!
Got it!
Got it!
Got it!
There are this many i's in supercalifragilisticexpialidocious: 7
BUILD SUCCESSFUL (total time: 0 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.