The follow code does not compile. Find and list all the reasons that it will not
ID: 3916131 • Letter: T
Question
The follow code does not compile. Find and list all the reasons that it will not compile.
import java.util.Scanner;
public class ScannerDemo
{
Public static void main(String []args)
{
Scanner keyboard = new Scanner(system.in);
System.println.out(“Enter the number of pods followed by’):
system.println.out(“the number of peas in pod:”);
int numberofPods = keyboard.nextInt();
int peasPerPod == keybord.nextInt();
int totalNumberOfPeas = numberofPods*peasPerpod;
system.Out.print(numberofPods ++ “ pods and “);
system.out.println(peasPerPod ++ “ peas per pod.”)
System.out.println(“The total number of peas = ‘
++ totalNumberOfPeas);
}
}}
Explanation / Answer
Reasons it won't compile:
1. In the declaration of main function, change the case of 'Public' to 'public'. Public is not any access specifier identified by Java.
2. Scanner keyboard = new Scanner(system.in);
system.println.out(“the number of peas in pod:”);
System Class comes with a capital 'S' in the start. Make it 'System'. It has been used wrongly at 4 places in the program
3. System.println.out(“Enter the number of pods followed by’):
System.println.out(“the number of peas in pod:”);
The correct syntax would be -> System.out.println(String arg);
4. A string literal always has to be enclosed in double quotes, not in single quotes and a statement ends with a semi colon, not with a colon.
5. int peasPerPod == keybord.nextInt();
A single '=' is used for assignment. '==' is used for comparing.
6. Correct spelling should be used for the variable keyboard.
7. Java variables are case sensitive.
int totalNumberOfPeas = numberofPods*peasPerpod;
Make is peasperPod
8. System.out.print(numberofPods ++ "pods and ");
'+' is used for concatenation, not '++'
9. System.out.println(peasPerPod + " peas per pod.")
It should end with semicolon
10. System.out.println(“The total number of peas = ‘++ totalNumberOfPeas);
Double quotes should be used to enclose the String
11. There's an extea curly brace '}' in the end.
A compiling program would be->
import java.util.Scanner;
public class ScannerDemo
{
public static void main(String []args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in pod:");
int numberofPods = keyboard.nextInt();
int peasPerPod = keyboard.nextInt();
int totalNumberOfPeas = numberofPods*peasPerPod;
System.out.print(numberofPods + "pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas =" + totalNumberOfPeas);
}
}