Exercises 1.11
- Find any syntax errors or logical errors in each of the following English
sentences.
- My younger sister was born two years before I were.
- The comma in this sentence is in the wrong place.
- This last parts is. easy
- Rewrite the following program using meaningful identifiers, indentation,
comments, and blank space to make the program easier to
understand.
class A{public static void main(String[]args){
System.out.println("In what year were you born?");
int a = In.getlnt();
System.out.println(IIWhat is your name?");
String b = In.getString();
System.out.println("Let me confirm that information. II);
System.out.println(IIYour name is "+b
+" and you were born in "+a+".");}}
- The following program contains five known syntax errors. Find them
and rewrite the program with the errors corrected.
class BadNews
/* a really terrible mess
{
public static void main (string[] args)
{
int i = 34.0, j = 2;
System.out.println('Values are i);
System.out.println(j);
}
}
- To gain more familiarity with error messages that will be produced
by the Java compiler, take a correct program that you have written
and then introduce each of the following errors in that program (one
at a time), noting any error message(s) produced in each case.
- missing the declaration of a variable
- attempting to print the value of a variable that has been declared
but not initialized
- attempting to change the value of an identifier that has been
declared with the final attribute
- using a reserved word as a variable identifier
- attempting to make a narrowing conversion without the use of
a cast
- omitting the */ required to close a comment that begins with
/*
- omitting the first semi-colon in the program
- omitting an opening brace bracket
- omitting a closing brace bracket
|
|