CSE 142 Final Cheat Sheet for (initialization; test; update) { statement(s); ... } while (condition) { statement(s); } if (test) { statement(s); } else if (test) { statement(s); } else { statement(s); } public static type name(parameters) { statement(s); ... return expression; } type name = value; // variable declaration and initialization Type objectName = new Type(parameters);// object construction Math Method Math.abs(value) Math.min(v1, v2) Math.max(v1, v2) Math.round(value) Math.sqrt(value) Math.pow(b, e) Description absolute value smaller of two values larger of two values nearest whole number square root base to the exponent power Random Method nextInt(max) Description random integer from 0 to max-1 String Method contains(str) endsWith(str), startsWith(str) equals(str) equalsIgnoreCase(str) indexOf(str) length() replace(str1, str2) substring(i, j) toLowerCase(), toUpperCase() charAt(i) Scanner Method nextInt() next() nextDouble() nextLine() hasNextInt() hasNext() hasNextDouble() hasNextLine() Description true if this string contains the other's characters inside it true if this string starts/ends with the other's characters true if this string is the same as str true if this string is the same as str, ignoring capitalization index in this string where given string begins (-1 if not found) number of characters in this string replace all occurrences in this string of str1 with str2 characters in this string from index i (inclusive) to j (exclusive) a new string with all lowercase or uppercase letters returns char at index i Description reads/returns input token as int reads/returns input token as String reads/returns input token as double reads/returns line as String returns true if there is a next token and it can be read as an int returns true if there is a next token to read returns true if there is a next token and it can be read as a double returns true if there is a next line to read Declaring and using Arrays type[] name = new type[length]; type[] name = {VAL1, VAL2, VAL3, ...}; name[index]= value; Classes Field (data inside each object) private type name; Method (behavior inside each object) public type name(parameters) { statements; } Constructor (code to initialize new objects) public className(parameters) { statements; } toString method (called when an object is printed) public String toString() { code that produces/returns a String; } Inheritance public class name extends superclass { } Critter classes public class name extends Critter { fields constructor public Color getColor() { statement(s) that return a Color; } public Action getMove() { statement(s) that return either Action.INFECT, Action.HOP, Action.RIGHT, or Action.LEFT } } public String toString() { statement(s) that return a String; }