Homework Chapter 3 Key Section 1 1. what is the vocabulary of a language? Give an example of an item in the vocabulary of Java. o The words and symbols allowed in java. o for, if, and import. 2. Give an example of a syntax rule. o Each statement ends with a semicolon; 3. What does the expression (x+y)*z mean? o Add x and y and multiply the result by z. 4. Describe two differences between programming languages and natural languages: o The size of the language. Programming languages have few words. o The rigidity. Programming languages must be concise. o Literalness. Programming languages do exactly what you specify. Section 2 1. What is the difference between double and int data types? o Doubles have decimal and integers do not. o Doubles use more memory. o Doubles have a larger range of values. 2. How does the syntax for manipulating numeric data types and objects differ? o Numeric data types use operators such as + and *. Objects send messages and must be instantiated. 3. Convert the following floating-point numbers to exponential notation. o 23.5 -> 2.35 E1 o 0.046 -> 4.6E-2 4. Convert the following numbers from exponential notation to floating-point notation. o 32.21E4 -> 322,100 o 55.5E-3 -> 0.0556 5. Give two examples of string literals. o “This is a literal” o “x = “ 6. Why is a variable called a variable? o Its value can change during program execution. 8. Declare a floating point variable payRate and simultaneously initialize it to $35.67. o double payRate = 35.67; 9. Declare three integer variables (a, b, c) in a single declaration and simultaneously initialize b to 4. o int a, b = 4, c; 10. Give two examples of data that cannot be stored in a variable of type int. o 3.14159265 o “A string cannot be stored in an int” 11. There are approximately 2.2 pounds in a kilogram. Name and declare a constant to represent this value: o final double POUNDS_PER_KILOGRAM = 2.2; 12. Assume that the integer x is 5 and the integer y is 10. Give the values of the following expressions: o x + y * z = 25 o x – y + z = -3 o (x + y) * 2 = 30 o y%x=0 13. Find the syntax errors in the following expressions: o -* o missing ( o Nothing inside (). This actually can be used for part of the signature of methods. 14. Assume that x is 4.5 and y is 2. Write the values of the following expressions: o x / y = 2.25 o y / x = 0.444444444… o x % y = 0.5 15. Assume that x and y are type double and z is of type int. For each of the follwing assignment statements, state which are valid and which produce syntax errors. o x = z valid o x = y + z valid o z = x + y invalid 16. Assume that x is of type double and y is of type int. Also assume that x = 4.5 and y = 2. Write the values of the following expressions: o (int) x * y 8 o (int)(x * y) 9 17. Assume that x is of type double and y is of type int. Write a statement that assigns the value contained in x to y after rounding to the nearest whole number. o If x is positive: y = (int)(x + 0.5); o If x is negative: y = (int)(x – 0.5); 18. Assume that x refers to the string “Wizard” and y refers to the string “Java”. Write the values of the following expressions: o y+x = “JavaWizard” o y + y.length() + x = “Java4Wizard” o y + “\n” + x + “\n” = Java Wizard 19. Declare a variable of type String called myInfo and initialize it to your name, address, and phone number. Each item of information in this string should be followed by a newline character. o String myInfo = “Ricky\n1313 Mocking Bird Lane\n 555-1313”; 23. State whether each of the following or invalid user-defined symbols in Java: o pricePerSquareInch - valid o student2 – valid o 2GuysFromLexington – invalid o PI – valid o allDone? – invalid 24. Write names for the following that follow good programming practice: o circleDiameter o STANDARD_DEDUCTION o drawRectangle 25. Describe the role of x, y, and z in the statement import x.y.z; o x – the overall package (folder) o y – the subsection of the package (subfolder) o z – the class in the subsection 26. What happens when the computer executes import x.y.*; o All the classes in the subfolder y are attached to the program. Section 4 1. Describe the difference between an end-of-line comment and a multiline comment o //comment begins from double forward slash to end of line. o /*Everything between the forward slash star to the star forward slash is a comment*/ 2. State two rules of thumb for writing appropriate comments in a program: o State the purpose of the program. o Explain a variable. o Explain major sections of code. o Explain tricky or complex sections of code. Section 5 1. At what point in the program development process are syntax errors, run-time errors, and logic errors detected? o Syntax errors – compilation o Run-Time errors – execution o Logic errors – When the programmer figures it out. 2. Give an example of a run-time error and explain why the computer cannot catch it earlier in the program development process. o int x = 5, y = 5, z = x % y; o y = x / z; o The compiler does not know the value of z. (Note: Simply x = y / 0; would not be detected either) 3. State the type of error (compile-time, run-time, or logic) that occurs in each of the following pieces of code. o x = y / 0; run-time o x + y = z; compile o area = length + width; //logical Section 6 1. Describe how one can modify code so that the cause of a logic error can be discovered. o Write lines of code in your program that print out values of variables during program execution.