Review for Test 1 (Chapter 1-5) 1. Introduction to Computers, Programs, and Java a) What is a computer? b) What is a computer program? c) A bit is a binary digit 0 or 1. A byte is a sequence of 8 bits. d) Programming language Machine language Assembly language High-level language: source program e) What is a compiler? f) What is the operation system (OS)? g) Java Java source code file must end with.java extension Every class is compiled into a separate bytecode file that has the same name as the class and ends with the.class extension To compile a Java course code file from the command line, use the javac command To run a Java class from the command line, use the java command Every Java program is a set of class definitions To run a Java program, the program must have a main method Java source programs are case sensitive. h) Programming errors Syntax errors Runtime errors Logic errors 2. Elementary Programming a) Identifiers b) Variables c) import statements d) assignment operator = e) constant f) Java primitive data types g) Numeric operations: +, -, *, /, % h) Augmented assignment operators: +=, -=, *=, /=, %= i) Increment operator (++) and the decrement operator (--) j) Type casting
Exercises: 2.1 Write a statement to declare a variable named x of the double type with an initial value 4.5. 2.2 Write a statement to declare a constant named MAX of the int type with value 45. 2.3 15 % 2 = 2.4 Write an expression that returns an integer from casting 1.7. 2.5 (int)(24.768 * 100) / 100.0 = 2.6 (int)(24.768 * 100) / 100 = 2.7 Use an augmented assignment operator to write a statement to add 5 into variable x. 2.8 Write an expression for 2.5 5.4. 2.9 What is the data type for a variable that stores the value 2.5F or 2.5f? 2.10 What is a possible data type for a variable that stores the value 32768? 2.11 What is a possible data type for a variable that stores the value 2147L?
3. Selections a) boolean type b) Relational operators: <. <=, ==,!=, >, >= c) selection statements one way if statements two way if-else statements nested if statements multi-way if-else statements switch statements d) Boolean expression e) Boolean operators: &&,,!, ^ f) Conditional expression:? : g) Operator precedence Exercises: 3.1 Write an expression that returns a random integers between 0 and 9, inclusive. 3.2 What is (int)math.random() * 10? 3.3 Assume age = 16; ticketprice = age >= 16? 20 : 10; What is ticketprice? 3.4 Assume x is an integer. Write a simple expression that returns true if x is even or false if x is odd. 3.5 What is true ^ false? 4. Mathematical Functions, Characters, and Strings a) Methods type Instance methods Static methods
b) Mathematical methods random pow etc. c) char data type and methods isdigit isuppercase etc. d) escape sequence and escape characters e) String data type and methods length concat subsring equals etc. f) formatting outputs: printf Exercises: 4.1 Write an expression that returns the sine of angle degree 36.5 using Math.sin and Math.toRadians. 4.2 What is Math.round(5.4)? 4.3 What is a literal for character A? 4.4 What is the string literal that consists of a single character A? 4.5 Write an expression that tests whether a character ch is a digit. 4.6 Write an expression that returns the first character in string s.
4.7 What is the output of System.out.print("ABCD".substring(2))? 4.8 What is the output of System.out.print("ABCD".indexOf("BC"))? 4.9 What is the output of System.out.printf("%-6.2f%-6.2f", 3.503, 4.355)? 5. Loops a) Loop statements while do-while for b) infinite loop c) sentinel value d) break and continue keyword Exercises: 5.1 How many times is the following loop executed? int count = 1; while (count < 5) { count += 3; 5.2 How many times is the following loop executed? int count = 1; do { count += 3; while (count < 5); 5.3 How many times is the following loop executed? for (int count = 1; count < 5; count += 3) { // Execute the body 5.4 What is count after the loop is finished? int count = 1; do { count += 3;
while (count < 5); 5.5 What is the output of the following code? for (int i = 1; i < 10; i += 2) { System.out.printf("%1d", i); 5.6 What is the output of the following code? int x = 85; for (int i = 2; i < x; i++) { if (x % i == 0) { System.out.print(i); break; 5.7 What is the output of the following code? int x = 85; for (int i = 2; i < x; i++) { if (x % i == 0) { System.out.print(i + " "); continue;