Question 2A Consider the following Java program which is des
Question 2A Consider the following Java program, which is designed to do the following: • Prompt the user to enter a whole number from the keyboard • Store this value to a variable of appropriate data type • Use a while loop to calculate the sum of numbers from one up to and including this value • Display the calculated sum with a message on the computer screen Unfortunately, as it stands, the program has seven errors. Identify and briefly explain any five errors in the program. You do not need to show corrected code. Bullet points are acceptable. You can refer to the line numbers in your explanations if you wish.
1 import java.util.Scanner;
2 public class Sum 3
public static main(String[] args) {
4 Scanner s = Scanner(System.in);
5 int number;
6 int n = 1;
7 System.out.println(“Enter a whole number:”)
8 number = s.nextLine();
9
10 while(n <= number) {
11 total += n;
12 n++;
13 }
14 System.println(“Total is:”, total);
15 }
16 }
Solution
1 import java.util.Scanner;
2 public class Sum 3 // ERROR. There should not be any space in the name of the class . Here Sum 3 is wrong . Also there is no opening { brace after class name
public static main(String[] args) {
4 Scanner s = Scanner(System.in); // ERROR . Scanner class object should be used like
Scanner s= new Scanner(System.in);
5 int number;
6 int n = 1; // ERROR n is not defined
7 System.out.println(“Enter a whole number:”)
8 number = s.nextLine(); //ERROR : number is integer type so it should input int from Scanner object s. nextLine() function will input String . It should be number = s.nextInt();
9
10 while(n <= number) { // ERROR: n is undefined
11 total += n; //ERROR: total is undefined
12 n++;
13 }
14 System.println(“Total is:”, total); // ERROR . It should be System.out.println(\"Total is:\"+total);
15 }
16 }


