Java Homework #1 Model Solution 1. What is a source program? What is a compiler? A source program is a program that is written in a high-level language. The compiler translates the source program into a machine language program. 2. What is the "JVM"? What is the "JRE"? JVM stands for Java Virtual Machine. The virtual machine interprets Java bytecode. The "JRE" (Java Runtime Environment) includes the JVM, but also includes standard libraries and provides services to programs. (Don't confuse the JRE with the JDK (Java Development Kit). 3. What is an operating system? An operating system ("OS") is the most important program. It is the OS that runs on a computer and manages and controls all that computer's activities and all access to resources. 4. Identify and fix the errors in the following code: 1 public class Welcome { 2 public void Main(String[] args) { 3 System.out.println('Welcome to Java!); 4 } 5 ) Answer: There are five errors: Line 2: missing "static" keyword, and "Main" should be "main". Line 3: Wrong opening quote, and missing closing quote; both should be '"'. Line 5: ")" should be "}" 1 public class Welcome { 2 public static void main ( String [] args ) { 3 System.out.println( "Welcome to Java!" ); 4 } 5 } 5. If a NoClassDefFoundError occurs when you attempt to run a Java program, what is the cause of the error? This error will occur if the JVM tries to load a class file that does not exist. That may happen if you forget to compile it, put it in the wrong location, or give it the wrong name. Note a Java program usually is composed of multiple classes, but you only execute the one with "main". 6. Why doesn't the System class need to be imported? Class System is found in the java.lang package, whose classes are automatically imported. 7. Which of the following identifiers are valid? Which are Java keywords? Applet, applet, a++, --a, 4#R, $4, #44, apps, class, public, int, false, x, y, radius Valid Identifiers: radius, applet, Applet, $4, apps, x, y Keywords: class, public, int, false ("$" is legal, but not recommended. "false" is not technically a keyword.) 8. What are the benefits of using constants? How should you declare an int constant named SIZE with the value 20? Using constants means that the data is permanent and will not change. Some benefits of using constants: do not have to repeatedly type the same value, making a change to one item instead of all the entries that need to be changed, and having a descriptive name for a value makes the program easier to read. final int SIZE = 20; 9. Assume that int a=1 and double d=1.0 and each expression is independent. What are the results of the following expressions (that is, the resulting value of a or d)? 1. a = 46 / 9; Output: 5 2. a = 46 % 9 + 4 * 4 - 2; Output: 15 3. a = 45 + 43 % 5 * (23 * 3 % 2); Output: 48 4. a %= 3 / a + 3; Output: 1 5. d = 4 + d * d + 4; Output: 9.0 6. d += 1.5 * 3 + (++a); Output: 7.5 7. d -= 1.5 * 3 + a++; Output: -4.5 (Remember, "/" between Integers means quotient, not division.) 10. If today is a Tuesday, what will be the day 100 days from now? (Show the Java code to calculate that.) int day = 2; // Set to Tuesday (today): 0=Sunday, 1=Monday, ... day = (day + 100) % 7; // = 4; so Tuesday plus 100 days = Thursday. System.out.println( day ); // Use a switch to print day names, if desired. (Note that "day + 100 % 7" is not correct, since the result might be a value greater than seven!) 11. Are the following statements syntactically correct? If so, show the output. If not, state the problem. a. System.out.println("25 / 4 is " + 25 / 4); b. System.out.println("25 / 4.0 is " + 25 / 4.0); c. System.out.println("3 * 2 / 4 is " + 3 * 2 / 4 ); d. System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4); Yes, all the statements are correct: a. 25 / 4 is: 6 b. 25 / 4.0 is: 6.25 c. 3 * 2 / 4 is: 1 d. 3.0 * 2 / 4 is: 1.5 (It is tempting to say that the truncation in (a) and (c) is an error, but you aren't provided enough information to decide that. A comment would help you determine if there was a logic error here or not.) 12. How would you write the following arithmetic expression in Java? (Note all variable names used are one-letter long.) 4 3 + d(2+a) ------- - 9(a+bc) + ----------- 3(r+34) a+bd 4/(3*(r+34)) - 9*(a+b*c) + (3+d*(2+a))/(a+b*d) (You cannot omit the multiply operator in Java.) 13) Evaluate the following two expressions: 2 * 2 - 3 > 2 && 4 - 2 > 5 false 2 * 2 - 3 > 2 || 4 - 2 > 5 false 14) Write a Boolean expression that evaluates true if weight is greater than 50 pounds or height is greater than 60 inches: weight > 50 || height > 60