This is a draft cheat sheet. It is a work in progress and is not finished yet.
String Methods
.toUpperCase() |
converts string to upper case |
.toLowercase() |
Converts string to lower case |
.indexof('ch') |
Index of the selected letter |
.substring(x,y) |
between values (y exclude) |
.length() |
returns the strings length |
.replace(old,new) |
replaces old char with new |
import java.util.Scanner;
Scanner input= new Scanner(System.in);
Scanner Methods:
.nextLine() ends with line
.next() ends with white space
.nextDouble()
.nextInt()
Import-ant statements
Import Statements
import java.util.*;
import java.text.*;
import java.swing.*;
import java.awt.*;
import java.swing.JFrame;
import java.util.random;
import java.util.scanner;
DecimalFormat df = new DecimalFormat(0.##);
RandomRange r = new RandomRange();
Scanner sc = new Scanner(System.in);
(sc.nextint, nextline)etc |
|
|
Loops
If Statement
if ( expression ) {
statements
} else if ( expression ) {
statements
} else {
statements
}
While Loop
while ( expression ) {
statements
}
Do-While Loop
do {
statements
} while ( expression );
For Loop
for ( int i = 0; i < max; ++i) {
statements
}
Switch Statement
switch ( expression ) {
case value:
statements
break;
case value2:
statements
break;
default:
statements
} |
|
|
Math methods
Math.pow(xy) |
Math.sqrt(x) |
Math.PI() |
Math.log(x) |
Math.random() * max+min |
but util random is better |
Class Diagram (UML)
+' = Public , '-' = Private
underlined = Static
// Constructors 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"(){
}
Constructor
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 getThing(){
return thing;
}
Creating a new instance
(class name) "instance name" = new (Class name);
alternatively you can pass data of instance to the constructor (non default) like this
(class name) "instance name" = new (Class name)(variables matching input parameter);
Calling a method / passing it data
instance.methodname(variable);
or
Instance1.setName("Ben Dover"); |
|