3.1
1. The vocabulary of a language is all of the symbols and words recognized by the language. Some examples of items in Java’s vocabulary are +, -, =, and 4.216
2. An example of a syntax rule in Java is that parentheses must always be arranged in the correct order: ( ), never ) (.
3. (x + y) * z means: add x to y and multiply the sum by z.
4. Programming languages need exact syntax and grammar, and are followed extremely literally, unlike English, where instructions can be more vague.
3.2
1. A double is a number variable that can use decimals. an int is an integer number variable, so it does not use decimals.
2. Syntax for non numeric data types must be defined before use. Numeric data types can be used as they are.
3. a. 2.35E1
b. 4.6E-2
4. a. 322100
b. 0.0556
5. One example of a string literal is “Enter first number to add: ” Another example is “The sum of the numbers is:”
6. A variable is called a variable because it does not have a fixed value; one is assigned to it, making it variable.
7. scanner, double, int, and string (not in chapter 2)
8. double payRate = $35.67
9. int a, b = 4, c
10. non-numeric data types cannot be stored in data type int, nor can numbers with decimals, such as 3.5235.
11. final double pound = kilogram * 2.2
12. x = 5, y = 10
a. x + y * 2 5 + 20 = 25
b. x-y*2 5 – 20 = -15
c. (x+y)*2 (15)*2 = 30
d. y % x 10/5 = 0
13.
a. a – * b + c: – and * are next to each other.
b. – (a + b) * c): c) has no open parentheses, only close parentheses.
c. (): there are no units to make this do anything.
14. x = 4.5 and y = 2
a. x / y 4.5/2 = 2.25
b. y / x 2 / 4.5 = .44
c. x % y remainder of 4.5 from 2 = .5
15.
a. x = z: valid
b. x = y * z: valid
c. z = x + y: syntax error
16. x = double, 4.5. y = int, 2.
a. (int) x * y = 9
b. (int) (x * y) = 9
17. y = (int) x
18. x = “Wizard” y = “Java”
a. y + x = “Java” + “Wizard”
b. y + y.length() + x = “Java” + “Java”.length() + “Wizard”
c. y + “\n” + x + “\n”
“Java”
“Wizard”
19. String myInfo = “Will Cook” + ”\n” + “19 Carter road” “\n” “783 0330″ “\n”
20. Methods are parameter based, and messages can correspond to them, but only if they have the same name as the method.
21. In a method’s signature, the type of value it returns is important for messages that are corrseponding to it. It’s name is important because a message’s name must be the same if they are to correspond. Also, the number and type of the parameters it expects are important for how the message corresponds to it.
22.
a. valid
b. valid
c. invalid – starts with a digit
d. valid
e. invalid – contains a “?”
23.
a. double circleDiameter
b. finaldouble incomeTaxDeduction
c. double drawRectangle
24. import x.y.z; x is the overall name of the package, y is the name of a subsection in the package, and z is the name of a particular class in that subsection.
25. import x.y.* would make the computer load all the classes under the given subsection at once.
3.3
1. a
System.out.print (”Enter your hourly wage: “);
wage = reader.nextDouble();
System.out.print (”your hourly wage is ” + wage + “.”);
b
System.out.print (”Enter your Social Security number: “);
ssn = reader.nextString();
System.out.print (”your Social Security number is ” + ssn + “.”);
2. nextInt retuns the first integer in the input line.
3. the program will return the object specified by the method.
3.4
1. an end of line comment includes all text following //, better for single lines of comments.
a multi line comment includes everything between an opening /* and a closing */, good for comments longer than one line.
2. begin most programs with comments about the purpose of the program.
certain segments of code should have comments preceding them to explain their functions.
3.5
1. syntax errors are checked for and detected during the compiling process. Run-time errors are found when trying to run the program, and logic errors can be found by the user when the program is run.
2. One example of a run-time error would be telling the program to divide by zero. This would keep the program from running. These errors are not detected during the compiling process because they can be written in ways that the program can understand, but not see as errors, and cannot be performed.
3.
a. run-time error
b. compile-time error
c. logic error