CSE143X Midterm Cheat Sheet
Primitive types: int, double, char, boolean
Math Method Description
--------------------------------------
Math.abs(n) absolute value of n
Math.max(x, y) maximum of x and y
Math.min(x, y) minimum of x and y
String Method Description
-----------------------------------------------------------------------------
charAt(index) character at a specific index
indexOf(text) index of a particular string (-1 if not found)
length() number of characters in string
substring(start, stop) characters from start (inclusive) to stop (exclusive)
substring(start) characters from start to end of string
toLowerCase() lowercase version of string
toUpperCase() uppercase version of string
equals(text) true if this string is the same as text
Scanner Method Description
------------------------------------------------------
nextInt() reads/returns input as int
nextDouble() reads/returns input as double
next() reads/returns input as String
Random Method Description
---------------------------------------------------------------------------
nextInt(n) returns a random int between 0 and (n-1) inclusive
Syntax Examples:
for (int i = 1; i <= n; i++) {
System.out.println("****");
}
if (n < 0) {
System.out.println("negative");
}
if (n < 0) {
System.out.println("negative");
} else {
System.out.println("not negative");
}
while (n < 200) {
n = n * 2;
}
Array examples:
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
for (int n : list) {
System.out.print(n + " ");
}