CSE 110 Lab 2: Introduction to variables Lab Exercise: Complete this portion of
ID: 3783620 • Letter: C
Question
CSE 110 Lab 2: Introduction to variables Lab Exercise: Complete this portion of the lab during your lab session, 1. Recognizing Syntax Errors When you make syntax errors in your program the compiler gives error messages and does not create the bytecode file(-class file). It saves time and frustration to learn what some of these messages are and what they mean. Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred. Your only choice is to carefully study your program to find the error. In the following you will introduce a few typical errors into a simple program and examine the error messages. scientist writes in a new language.) Hello Print a Hello World message public class Hello main method prints the greeting public static void main String args) system out printin (-Hello, Horld a Compile and run the program to see what it does, Then make the changes as stated below, answering the accompanying questions as 1. class name different from file name. Delete one l (el from the name of the dass (so the first non-Explanation / Answer
1.Class name different from file name
>>javac Hello.java //Compile
Hello.java:1: class Helo is public, should be declared in a file named Helo.java
public class Helo {
^
1 error
2.Misspelling inside string
No error because the class name and file name are same hence the class file gets created upon successful compilation.As l is deleted from the string in "", it prints string in println() statement as below
When we run program Output is:
Helo,World!
3.No ending mark :
As the ending " is removed and added l in Helo and all are within "" quotes it throws error ,as it is syntax error.
error: unclosed string literal
System.out.println("Hello,World!);
^
Hello.java:9: error: ';' expected
System.out.println("Hello,World!);
^
4.No begging quotation
Error:
error: ')' expected
System.out.println(Hello,World!");
^
C:/Hello.java:9: error: unclosed string literal
System.out.println(Hello,World!");
^
C:/Hello.java:9: error: ';' expected
System.out.println(Hello,World!");
^
5.No semi colon after statement:
Error:
C:/Hello.java:9: error: ';' expected
System.out.println("Hello,World!")
^
1 error
----------------------------Covertion code------------------------
import java.util.Scanner;
public class ConvertFootToInch
{
public static void main(String[] args)
{
int i,inches;
Scanner input = new Scanner(System.in);//read from user
System.out.print("Enter lenght in feet : ");
i = input.nextInt();
inches= i*12;
System.out.print("It is "+inches +"inches" );
}
}