CSE 142 Midterm

Cheat Sheet

 

 

for (initialization; test; update) {

statement(s);

}

public static void name(parameters) {

    statement(s);

}

 

if (test) {

    statement(s);

} else if (test) {

    statement(s);

} else {

    statement(s);

}

 

public static type name(parameters) {

    statement(s);

    ...

    return expression;

}

while (condition) {

    statement(s);

}

Scanner console = new Scanner(System.in);

Random r = new Random();

 

 

Math

 

 

Scanner

 

Method                   

Description

 

Method

Description

Math.abs(value)

absolute value

 

nextInt()

reads/returns input as int

Math.min(v1, v2)

smaller of two values

 

nextDouble()

reads/returns input as double

Math.max(v1, v2)

larger of two values

 

next()

reads/returns input as String

Math.round(value)

nearest whole number

 

 

Math.sqrt(value)

square root

 

Random

 

Math.pow(b, e)

base to the exponent power

 

Method

Description

 

 

 

nextInt(max)

random integer from 0 to max-1

 

 

String

 

Method

Description

contains(str)

true if this string contains the other's characters inside it

endsWith(str), startsWith(str)

true if this string starts/ends with the other's characters

equals(str)

true if this string is the same as str

equalsIgnoreCase(str)

true if this string is the same as str, ignoring capitalization

indexOf(str)

index in this string where given string begins (-1 if not found)

length()

number of characters in this string

replace(str1, str2)

replace all occurrences in this string of str1 with str2

substring(i, j)

characters in this string from index i (inclusive) to j (exclusive)

substring(i)

characters in this string from index i (inclusive) to end

toLowerCase(), toUpperCase()

a new string with all lowercase or uppercase letters

charAt(i)

returns char at index i