CS 121 Midterm Review Sheet NOTE: This is not an exhaustive list of things on the midterm, just a reminder of some of the concepts that may be on the midterm. 1) Computer Hardware * RAM is volatile storage, but Secondary storage devices such as hard drives are non-volatile storage. * Java is compiled to bytecode. This bytecode is then interpreted by the Java Virtual Machine into machine code, for a particular machine. 2) Print statements * Write simple print statements using System.out.println(). * Know the difference between System.out.print() and System.out.println() * Write print statements containing both Strings and primitive types (ints, doubles, floats) using the "+" operator * Should know how evaluation works within println for a mixture of Strings and ints (i.e. left to right evaluation). System.out.println(3+5+"a"+3+5); > 8a35 * Use parens () to force mathematical evaluations in the println(). 3) Declarations and Assignments * Know about primitive data types (double, int, boolean, char) as well as the String class. * Know how to declare variables using the primitive data types (and the String class), with legal variable names and assign legal values to them. * Know how to add together Strings (also known as concatentation) using the "+" operator. 4) Math operations * Operator precedence: Know that multiplication comes before addition and subtraction, and that if two operations (like addition and subtraction) have equal precedence, then an expression is evaluated from left to right. Evaluate simple mathematical statements such as int cat = 3; cat = 1 + 7*cat; System.out.println(cat); * Know how to use Math.random() to get a random number within an arbitrary range, such as a random number between 10.0 and 15.0 as shown below: double starting_point = 10.0; double desired_range = 5.0; double between_10_and_15 = desired_range * Math.random() + desired_starting_point; 5) Java syntax * For example, make sure there are curly braces at the start and end of class, method definitions and for/while loops. Also, put semicolons at the end of statements. * Know the basic outline of a Java class such as: public class InfantTester{ } * To have a runnable program, exactly one of your classes must include the main() method: public static void main (String[] args){ } 6) Methods * Know how to write a method definition: Specify the return type, method name, arguments. All of our methods are public for now. * Know how to write the code for simple methods such as double average(int a, int b, int c) { double avg = (a + b + c)/ 3.0; return avg; } 7) Classes * Know the components of a class : Attributes, Constructors, Methods. Note that while many classes include all these components, it is not required to include any of these components. * Know how to instantiate an instance of a class with "new" and the proper arguments. Infant myKid = new Infant("Lizzie",4); * Understand how the constructor initializes variables. * Know how to call a method within a class with the correct arguments such as myKid.someMethod(someArgument); * Know how to write methods in the class definition that modify or return attributes. * Know how to the use charAt(), length() methods in the String class. Also know that the first position in a String starts at 0. * Know that instances of a class can be passed to methods as arguments, just like the primitive data types such as int or boolean. * Know that the "import" statement makes classes available to be used in the given class such as: import java.util.*; 8) Scanner * Know how to use Scanner to get input from the console such as Scanner scan = new Scanner(System.in); String s = scan.next(); * Can also get integer values using scan.nextInt() and double values using scan.nextDouble() 9) Boolean conditionals * Know that boolean expressions such as (1 == 2) or (child.numYears() == 12) evaluate to either true or false. * Know the different boolean operators &&, ||, ! and relational operators such as >, <, ==. 10) Loops * for loops. Know how to use for loops to do repeated actions. Know what the 3 fields of a for loop are for. Understand that the test condition of a for loop can be any boolean expression. * Know difference between for and while loops. * Be able to follow the execution of a loop and keep track of the intermediate values in the loop. 11) Casting * Know about casting, either through a casting operator like (double) or automatic casting, as when a digit is cast to a String using the "+" operator. * Dividing two integers will return an integer, not a double. * Casts have higher precedence than arithmetic. Use parentheses to force the arithmetic evaluation. For example, using Dr Java: double a = 3.5; double b = 1.2; (int) a / b >2.5 (int) (a/b) >2 In the first case, a is cast to an int and then divided by a double. In the second case, a is divided by b and then cast to an int. Notice the different resulting values. 12) Conditionals * if/else structure use boolean expressions to test conditions. * switch statements. Need to use "break" if you want to "fall through" and stop checking conditions.