Java Midterm Cheat Sheet
by sefergus via cheatography.com/31341/cs/9489/
String Methods Date Class Abstract Classes and Interfaces
.toUpperCase() .equals(str) jav.util.Date date= new java.util.Date; Abstract Classes Interfaces
.toLowerCase() .indexOf(e) date.toString(); cannot use "new" only has abstract
.substring(i,j) j is excluded .concat(str) methods
Point2D Class methods have no body no constructors
.length() .charAt(i)
import java.geometry.Point2D; mix of abstract/non-ab‐ "implements"
.compareTo(str) .contains(e)
Point2D variable = new Point2D(x, y); stract methods
Integer.parseInteger(intString)
"extends" contains
Double.parseDouble(doubleString)
Objects constants
import java.util.Scanner;
no Circle() { has constructors
Scanner input= new Scanner(System.in);
variable } contains contacts and variables
constr‐
Scanner Methods: public abstract class ClassName {
uctor
.nextLine() ends with line
constr‐ Circle (double radius) {
.next() ends with white space java.lang.Comparable
uctor this.radius=radius;}
.nextDouble() public interface comparable <E>{
.nextInt() getter double getArea() { public int compareTo(E o); }
return 2 x radius x radius x Math.PI; } returns -1 for less than, 0 for equals,
Naming setter void setRadius(double radius) { 1 for greater than
keywords lowercase rule this.radius=radius;}
java.lang.Cloneable
variables camelCase convention instanceof tests whether an object is an instance
public interface clonable {}
of a class
constants ALL_CAPS rule use .clone()
super(); calls no arg constructor of superclass
class names CamelCase convention
super(‐ calls matching arg constructor of Loops
Math Methods arg); superclass
while int x=n;
array of for (int i, i<thing.length, i++)
Math.pow(a, b) Math.PI() while (x>1) {
objects array[i]= new Thing(param);}
Math.log(x), Math.l‐ Math.sqrt(x) change x; }
og10(x) "this.radius" is an instance variable, as is for for (int i, i<variable, i++){
the original data field
Math.floor rounds Math.ceil() rounds for each (arrays) for (int i: list){
"radius" is the local variable
down up boolean (boolean ? true : false)
Math.random() Math.min(),
constructors must have same name as
Math.max() Characters
class
import java.lang.Math; constructors do not have a return type, not .isDigit(ch) .isLetter(ch)
even void .isLowerCase(ch), .toLowerCase(ch),
has sin, cos, tan, toRadians, toDegree, asin, constructors are invoked using the new .isUpperCase(ch) .toUppercase(ch)
acos, atan operator when an object is created
default constructor goes to class with no ArrayList Methods
low + Math.random()* high (non-inclusive) other constructors defined
Escape Sequences
\t tab
\n newline
\" double quote
\\ backslash
create ArrayList<type> name = new
ArrayList<type>();
access list.get(i)
element
update list.set(i, e)
element
return list.size()
size
add list.add((i), e)
element
remove list.remove(i or e)
element
By sefergus Published 16th October, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sefergus/ Last updated 16th October, 2016. Learn to solve cryptic crosswords!
Page 1 of 3. http://crosswordcheats.com
Java Midterm Cheat Sheet
by sefergus via cheatography.com/31341/cs/9489/
ArrayList Methods (cont) Vocabulary Vocabulary (cont)
remove all elements list.clear() composition information belongs to instance a method that can only be
one object method invoked from a specific object
import java.util.ArrayList;
association/‐ information can belong to local within a method
Important methods segregation many objects variable
modifier returnValueType methodName(p‐ public visibility can be seen anywhere in instance dependent on the specific
arams){ any package variable instance (class)
public Class ClassName{ private visibility can be seen within class overlo‐ methods can have the same
public static void main (String[] args) protected in package and ading name as long as their method
visibility subclasses of this in any methods signatures are different
Scanner input= new Scanner(System.in)
package binary operators are left-associative,
System.out.println(line);
runtime error crash assignment operators are right associative
public static type name (type param){
return type; } compile error doesn't run
public boolean equals (Object o){ final static constant modifier
if (o instance Person){ byte 8 bits*
Person p= (Person) o; block comment /* ... */
return this.name.equals(p.getName()));
line comment //
}else{
javadoc /** ... */
return false;
comments
}
} break; breaks out of a loop
public String toString(){ continue; stays in loop
return "String";} variable declar‐ creating a variable with
to use a method from a different class: ation its type
Class.method(var); static shared by all instances of
a class
Array methods relational <, <=, ==, !=, >, >=
java.util.Arrays.sort(array) .length operator
java.util.Arrays.equal(a1, a2) if corres‐ logical operator !, &&, || ( inclusive), ^
ponding ( exclusive)
elements Numeric Types byte, short, int, long,
are the (in order) float, double
same
Variable Scope variables only exist within
Arrays.toString(array) .reverse() {}
array[i] array[i]=e assignment =, +=, -=, *=, /=, %=
import java.util.Arrays; operators
int[] values= new int[10] operators +, -, %, / (truncates for
default values: 0, /u0000, or false int )
printing gives reference increment/ ++, --
methods can modify arrays decrement
import java.util.Arrays; operators
multi-dimensional arrays: arrays of arrays.
elementType [rows][columns] a
rrayVar
By sefergus Published 16th October, 2016. Sponsored by CrosswordCheats.com
cheatography.com/sefergus/ Last updated 16th October, 2016. Learn to solve cryptic crosswords!
Page 2 of 3. http://crosswordcheats.com