Show Menu
Cheatography

Joes Java Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

String Methods

.toUpp­erC­ase()
converts string to upper case
.toLow­erc­ase()
Converts string to lower case
.index­of(­'ch')
Index of the selected letter
.subst­rin­g(x,y)
between values (y exclude)
.length()
returns the strings length
.repla­ce(­old­,new)
replaces old char with new
import java.u­­ti­l.S­c­a­nner;
Scanner input= new Scanne­­r(­S­y­st­­em.in);

Scanner Methods:
.nextL­­ine() ends with line
.next() ends with white space
.nextD­­ou­ble()
.nextInt()

Import-ant statements

Import Statements
import java.u­til.*;
import java.t­ext.*;
import java.s­wing.*;
import java.a­wt.*;
import java.s­win­g.J­Frame;
import java.u­til.ra­ndom;
import java.u­til.sc­anner;

Decima­lFormat df = new Decima­lFo­rma­t(0.##);
Random­Range r = new Random­Ran­ge();
Scanner sc = new Scanne­r(S­yst­em.in);
(sc.ne­xtint, nextli­ne)etc
 

Loops

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

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

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

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

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

Math methods

Math.p­ow(xy)
Math.s­qrt(x)
Math.PI()
Math.l­og(x)
Math.r­andom() * max+min
but util random is better

Class Diagram (UML)

+' = Public , '-' = Private
underlined = Static

// Constr­­uctors have the same name as the class.
// if return type is 'void' , no return statement is required.

Things That May Help

Default Construct
public "name thing"(){
}

Constr­uctor
public "name thing"(int x, string y){
datafield x = x;
datafield y = y;
}

Mutator
public void setThing( string thing){
datafield thing = thing;
}

Accessor
public string getThi­ng(){
return thing;
}

Creating a new instance
(class name) "­ins­tance name" = new (Class name);

altern­atively you can pass data of instance to the constr­uctor (non default) like this
(class name) "­ins­tance name" = new (Class name)(­var­iables matching input parame­ter);


Calling a method / passing it data
instan­ce.m­et­hod­nam­e(v­ari­able);
or
Instan­ce1.se­tNa­me(­"Ben Dover");