public class BadNews { public static final int MAX_ODD = 21; public static void
ID: 3721126 • Letter: P
Question
public class BadNews {
public static final int MAX_ODD = 21;
public static void writeOdds() {
// print each odd number
for (int count = 1; count <= (MAX_ODD - 2); count++) {
System.out.print(count + " ");
count = count + 2;
}
// print the last odd number
System.out.print(count + 2);
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
Explanation / Answer
Errors are corrected and comments provided with in the program
public class BadNews{
// Error 1 Below assignment can not be final since further down the program 11 reassigned
public static int MAX_ODD = 21;
public static void writeOdds() {
// Error 2 Compile time error count has to be defined out side for loop
// print each odd numbe
int count = 1;
// Error 3 Logic error since count is incremented by 2 with in loop there is not need to increment again
for (count = 1; count <= (MAX_ODD - 2);) {
System.out.print(count + " ");
count = count + 2;
}
// Error 4 add new line character to send next output to next line
// print the last odd number
System.out.print((count + 2 )+" ");
}
public static void main(String[] args) {
// write all odds up to 21
writeOdds();
// now, write all odds up to 11
MAX_ODD = 11;
writeOdds();
}
}
Output
1 3 5 7 9 11 13 15 17 19 21
1 3 5 7 9 11
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.