Debugging the following program . 10 errors . Java programming . Please add comm
ID: 3725421 • Letter: D
Question
Debugging the following program . 10 errors . Java programming . Please add comment , still new to programming .public class Broken { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sequenceID; int n;
while(true) { System.out.println("Available Numeric Sequences"); System.out.println("================="); System.out.println(" 1. Descending sequence"); System.out.println(" 2. Odd Numbers"); System.out.println(" 3. Even Numbers"); System.out.println(" 4. Every third number"); System.out.println(" 5. Numbers divisible by 5 but not 3") System.out.println();
System.out.print("What numeric sequence would you like to print (use integer to select, 1 to quit)? "); sequenceID = input.nextInt(); println();
if (sequenceID == 0) { break; } else if (sequenceID < 0 || sequenceID > 5) { System.out.println("Invalid sequence ID entered. Try again!"); continue; }
System.out.print("How many iterations? "); n = in.next();
if (n <= 0) { System.out.println("Iterations should be greather than 0"); continue; }
System.out.println();
if (sequenceID == 0) { for(int i = n; i > 0; i++) { System.out.printf("%3d", i); } } else if (sequenceID == 2) { for(int i = 0; i < n; i++) { System.out.printf("%3d", (2*i)+1); } } else if (sequenceID == 3) { for(int i = 1; i < n; i++) { System.out.printf("%3d", 2*i); } } else if (sequenceID == 4) { for(int i = 1; i <= n; i++) { System.out.printf("%3d", (3i)+1); } } else if (sequenceID == 5) { int i = 1; int count = 0; while (count < n) { if ((i % 5 == 0) && (i % 3 == 0)) { System.out.printf("%3d", i); count++; } i++; } } System.out.println(); System.out.println(); } in.close();
System.out.println(); System.out.println("Thanks for playing..."); } } Part 3: Debugging Ac Debugging is a common activity when working on programs. For this part, you will download Broken.java from Canvas and fix it. Understanding the problem is important so make sure that you comment this program to describe what it is doing. There are a total of 10 different compile time and runtime errors in this program that you must find and fix. br
Explanation / Answer
import java.io.*;
import java.util.*;
public class Broken{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sequenceID;
int n;
while(true) {
System.out.println("Available Numeric Sequences");
System.out.println("=================");
System.out.println(" 1. Descending sequence");
System.out.println(" 2. Odd Numbers");
System.out.println(" 3. Even Numbers");
System.out.println(" 4. Every third number");
System.out.println(" 5. Numbers divisible by 5 but not 3"); // semicolon at last was missing
System.out.println();
System.out.print("What numeric sequence would you like to print (use integer to select, 0 to quit)? "); // 1 is option for descending sequence so to quit program we should enter 0 not 1
sequenceID = in.nextInt(); // 'input' is not declared here we have object 'in'
System.out.println(); // invalid method to print on console, println should be used with System.out
if (sequenceID == 0) {
break;
} else if (sequenceID < 0 || sequenceID > 5) {
System.out.println("Invalid sequence ID entered. Try again!");
continue;
}
System.out.print("How many iterations? ");
n = in.nextInt(); // n is type of int so we should use nextInt() method
if (n <= 0) {
System.out.println("Iterations should be greather than 0");
continue;
}
System.out.println();
if (sequenceID == 1) { // sequenceID can't be 0 it should check it for 1
for(int i = n; i > 0; i--) { // we are moving from n to 0 so there should be i--
System.out.printf("%3d", i);
}
} else if (sequenceID == 2) {
for(int i = 0; i < n; i++) {
System.out.printf("%3d", (2*i)+1);
}
} else if (sequenceID == 3) {
for(int i = 1; i <= n; i++) { // to complete n iterations i should be <= to n not only <
System.out.printf("%3d", 2*i);
}
} else if (sequenceID == 4) {
for(int i = 1; i <= n; i++) {
System.out.printf("%3d", 3*i); // 3i is invalid statement it should be 3*i only to print every third number not 3*i+1
}
} else if (sequenceID == 5) {
int i = 1;
int count = 0;
while (count < n) {
if ((i % 5 == 0) && (i % 3 != 0)) { // number divisible 5 so i%5==0 but not divisible by 3 i.e; i%3!=0
System.out.printf("%3d", i);
count++;
}
i++;
}
}
System.out.println();
System.out.println();
}
in.close();
System.out.println();
System.out.println("Thanks for playing...");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.