Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For each of the attached files, Debugging1.java, Debugging2.java, and Debugging3.java, find the problems (bugs) in the code and fix them. Complete the following table: Debugging1.java
For each of the attached files, Debugging1.java, Debugging2.java, and Debugging3.java, find the problems (bugs) in the code and fix them. Complete the following table:
| Debugging1.java | Debugging2.java | Debugging3.java |
What is the problem? |
|
|
|
How did you find it? Did you need to use a debugger? |
|
|
|
How did you fix it? |
|
|
|
Here are the codes:
Debugging1.java
package lab04; public class Debugging1 { /** * Find how many times the letter g occurs in a String. * This program has bugs in it. */ public static void main(String[] args) { int letterCount = 0; String checkWord = "Debugging"; String singleLetter = ""; for (int k = 0; k < checkWord.length(); k++) { singleLetter = checkWord.substring(1,1); if (singleLetter == "g") letterCount++; } System.out.println("g was found " + letterCount + " times"); } }
Debugging2.java
package lab04; public class Debugging2 { public static void main(String[] args) { // ------Problem 2a. Print the numbers 1 to 10------------ for (int i=1; i<=10; i++); { System.out.println("Number is " + i); } //--------Problem 2b. Comparing two Strings-------------- String s1 = "abcd"; String s2 = "abcd"; String s3 = "abcdef"; if (s1 == s2) System.out.println(s1 + " and " + s2 + " are equal"); else System.out.println(s1 + " and " + s2 + " are NOT equal"); if (s1 == s3) System.out.println(s1 + " and " + s3 + " are equal"); else System.out.println(s1 + " and " + s3 + " are equal"); //------------------------------------------------------- /** * Problem 2c. The following two loops are supposed to do * the same thing. The while loop works fine, Prints sum = * 55. However, the for-loop has some problems. Fix them. */ int num = 10; int sum = 0; while(num >= 0) { sum = sum + num; num--; } System.out.println("Sum = " + sum); num = 10; sum = 0; for(int k = 0; k < num; k++) { num = sum + num; } System.out.println("Sum = " + sum); } }
Debugging3.java
package lab04; import java.util.Scanner; // Scanner class used for getting user input public class Debugging3 { // AverageAndSmallest // This program will get three numbers from the user // and determine the smallest number and the average // The main method that begins execution of Java application public static void main(String args[]) { // variable declarations int num1; // first number to compare int num2; // second number to compare int num3; // third number to compare int smallest = 0; // variable to hold smallest double average; // variable to hold average // create Scanner to capture input from console Scanner input = new Scanner(System.in); // get user input, num1 and num2 System.out.print("Enter first number: "); num1 = input.nextInt(); System.out.print("Enter second number: "); num2 = input.nextInt(); System.out.print("Enter third number: "); num3 = input.nextInt(); // Compare and determine the smallest if (num1 > num2) smallest = num2; if (num1 < num2) smallest = num1; if (num3 < smallest) smallest = num3; // Calculate the average average = num1 + num2 + num3 / 3; // Display average and the smallest System.out.printf("Average is %.2f ", average); System.out.printf("Smallest is %d ", smallest); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started