Chapter 2 Java Syntax and Semantics, Classes, and Objects 1

2

Java Syntax and Semantics, Classes, and Objects

Answers to Exam Preparation Exercises

1. / Valid: data, y, PAY_DAY, num5
Invalid: item#1, 3Set, bin-2, Sq Ft
2. / Dwits
a.XYZInvalid—must end with 1, 2, or 3
b.123Invalid—must start with X, Y, or Z
c.XlValid
d.23YInvalid—must start with X, Y, or Z
e.XY12Valid
f.Y2YInvalid—must end with 1, 2, or 3
g.ZY2Valid
h.XY23XlValid
3. / program (15), algorithm (14), compiler (3), identifier (1), compilation phase (12), execution phase (10), variable (11), constant (2), memory (13), syntax (6), semantics (8), block (7)
4. / Reserved words: char, new
User-defined words: sort, INT, Public
5. / False; reserved words cannot be used as variable names.
6. / False; in a Java program containing just one method, that method must be named main.
7. / a. s1 = blues2 = bird
b. Result:bluebird
c. Result: bluebird
d. Result: blue bird
8. / A rollingstonegathersnomoss
9. / Only one character can be stored into a variable of type char.
10. / There are no characters in an empty string.
11. / False; a variable of type String can not be assigned to a variable of type char.
12. / True; a literal string can be assigned to a variable of type String.
13. / The identifier computer is used to name something, such as an application or a variable. "computer" is the character string made up of the characters: 'c', 'o', 'm', 'p', 'u', 't', 'e' and 'r'.
14. / 1425B Elm St.
Amaryllis
, Iowa
15. / This program may be corrected in several ways. Here is one correct version:
public class LotsOfErrors
{
public static void main(String[] args)
{
final String FIRST = "Martin";
final String MID = "Luther";
final String LAST = "King";
String name;
name = FIRST + ' ' + MID + ' ' + LAST + " Jr.";
System.out.println(name);
}
}
16. / the name of an instance, a dot, and the method name
17. / print and println
18. / ” invoking a method” means telling a method to apply itself to the object or class to which it is attached.
19. / A parameter list is the technique used to pass information into a method.
20. / An application
21. / The nextLine method inputs a string keyed by the user
22. / The name of a constructor must be identical to the name of the class. Because we begin class names with uppercase letters, the constructor must also begin with an uppercase letter.

Answers to Programming Warm-Up Exercises

1. / System.out.println("Nell B. Dale");
2. / System.out.println("The moon ");
System.out.println("is ");
System.out.println("blue.");
3. / String make;
String model;
String color;
char plateType;
char classification;
4. / System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Color: " + color);
System.out.println("Plate type: " + plateType);
System.out.println("Classification: " + classification);
5. / //******************************************************************
// PrintName application
// This program prints a name in two different formats
//******************************************************************
Import java.util.Scanner;
public class PrintName
{
public static void main(String[] args) throws IOException
{
String FIRST = "Herman";// Person's first name
String LAST = "Herrmann";// Person's last name
String MIDDLE = "G";// Person's middle initial
String firstLast; // Name in first-last format
String lastFirst; // Name in last-first format
String firstInitialLast// Name in first, initial, last format
Scanner in; // Input stream for strings
in = newScanner( System.in );
System.out.print("Enter first name: "); // Prompt for first name
first = in.nextLine(); // Get first name
System.out.print("Enter last name: "); // Prompt for last name
last = in.nextLine(); // Get last name
System.out.print("Enter middle initial: "); // Prompt for middle initial
middle = in.nextLine(); // Get middle initial
firstLast = first + " " + last; // Generate first format
System.out.println("Name in first-last format is " + firstLast);
lastFirst = last + ", " + first + ", "; // Generate second format
System.out.println("Name in last-first-initial format is " +
lastFirst + middle + ".");
firstInitialLast = first + " " + middle + ". " + last;
System.out.println("Name in first-initial-last format is " +
firstInitialLast):
}
}
6. / a.System.out.println("Four score");
System.out.println("and seven years ago");
b.System.out.println("Four score");
System.out.println("and seven");
System.out.println("years ago");
c.System.out.println("Four score");
System.out.println("and");
System.out.println("seven");
System.out.println("years ago");
d.System.out.println("Four");
System.out.println("score");
System.out.println("and");
System.out.println("seven");
System.out.println("years");
System.out.println("ago");
7. / These statements are shown consecutively, but are spread throughout an application.
import java.util.*;
Scanner in;
String aString;
in = new Scanner(System.in);
aString = in.nextLine();
8. / No answer is expected.