Show Menu

Java Exam Cheat Sheet by

java

Simple Java Program

public class HelloWorld{
    //Main method must take String[] args
    public static void main(String[] args){
        //Main functionality here
    }
}

Parsing

int *name = Interg­er.p­ar­seI­nte­ger­(str)
double *name = Double.pa­rse­Dou­ble­(str)
Casting: (int)(­some other type); (doubl­e)(­some other type)

Java Object

public class ObjectName{

    //variables that are private to this object
    private type variableName;
   
    //Constructor gets called when new method is created --> Multiple constructors
    //can exist
    public ObjectName(*parameters){
        //Whatever happens in constructor
    }
}

Math Methods

Math.p­­ow(a, b)
Math.PI()
Math.l­­og(x), Math.l­­og­10(x)
Math.s­­qrt(x)
Math.floor rounds down
Math.c­­eil() rounds up
Math.r­­an­dom() Unif[0,1)
Math.m­­in(), Math.max()
Uniform Random Int between [1,6] --> (int)(­Mat­h.r­andom() + 1)

Intege­rStack

IntegerStack intStack = new IntegerStack();

//Add an element to the stack
intStack.push(int);

//Removing the top element from the stack
intStack.pop();

//checking if empty
intStack.isEmpty();
Might need to import Intege­rStack --> Class specific object --> not java util

Knapsack

P = integer array (n+1, V+1)
for(v = 0 ... V)
P(0,v) = 0;
for (i = 1 ... n)
for (v = 0 ... V)
if (volumes(i-1) <= v)
P(i,v) = max(profit(i-1) + P(i-1,v-volumes(i-1)),
P(i-1,v));
else
P(i,v) = P(i-1,v)
return P(n,V);
 

Statements

If Statem­ent
if ( expre­ssion ) {
 ­ ­st­ate­ments
} else if ( expre­ssion ) {
 ­ ­st­ate­ments
} else {
 ­ ­st­ate­ments
}

While Loop
while ( expre­ssion ) {
 ­ ­st­ate­ments
}

Do-While Loop
do {
 ­ ­st­ate­ments
} while ( expre­ssion );

For Loop
for ( int i = 0; i < max; ++i) {
 ­ ­st­ate­ments
}

For Each Loop
for ( var : colle­ction ) {
 ­ ­st­ate­ments
}

Switch Statem­ent
switch ( expre­ssion ) {
 ­ case value:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ case value2:
 ­ ­ ­ ­st­ate­ments
 ­ ­ ­ ­break;
 ­ ­def­ault:
 ­ ­ ­ ­st­ate­ments
}

Exce­ption Handling
try {
 ­ ­sta­tem­ents;
} catch (Exce­pti­onType e1) {
 ­ ­sta­tem­ents;
} catch (Exception e2) {
 ­ ­cat­ch-all statem­ents;
} finally {
 ­ ­sta­tem­ents;
}
for loop is more general: for(int i; boolea­­nM­e­t­ho­­d(i), increm­­en­t­M­et­­hod­­(i){}
increm­­enting in short:
i = i + 1; --> i++;
i = i - 1; --> i--;
i += a;
i -+ a;

Insertion Sort

Insertion Sort:
for (i = 1 ... n-1)
for (j = i ... 1)
if (a(j-1) > a(j))
swap(a(j-1),a(j))
else break;
 

String Methods

.toUpp­erC­ase();
toLowe­rCa­se();
.subst­­ri­n­g­(i,j) j is excluded
.length()
.compa­­re­T­o­(str) *lexic­ogr­aphic ordering (-1, 0, 1)
.equal­­s(str)
.index­­Of(e)
.conca­­t(str)
.charAt(i)
.conta­­ins(e)

Arrays

type[] arrayName = new type[length]
E.g.
boolean[] visitedNode = new boolean[this.numberOfNodes];

ArrayList

create
ArrayL­­is­t­<­ty­­pe> name = new ArrayL­­is­t­<­ty­­pe>();
access element
list.g­­et(i)
update element
list.s­­et(i, e)
return length
list.s­­ize()
add element somew­here
list.a­dd(e)
add element at i
list.a­dd(i,e)
remove element
list.r­­em­ove(i or e)
remove all elements
list.c­­lear()
import java.u­­ti­l.A­r­r­ay­­List;

Queues

Queue<type> q = new Queue<type>();

//put element in queue
q.enqueue(e);

//remove element in queue
q.dequeue();

//check if empty
q.isEmpty();

//check size
q.length(); or q.size();
We use a class specific Queue method --> not the java utils one

GCD

public static int GCD (int m, int n){
    int temp;
    while (n%m != 0){
      temp = m;
      m = n%m;
      n = temp;
      if (m==0) return 1;
    }
    return m;
  }

Download the Java Exam Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/curae_java-exam.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Spring Framework 4 Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet