Show Menu
Cheatography

Java Midterm Cheat Sheet by

String Methods

.toUpp­erC­ase()
.equal­s(str)
.toLow­erC­ase()
.index­Of(e)
.subst­rin­g(i,j) j is excluded
.conca­t(str)
.length()
.charAt(i)
.compa­reT­o(str)
.conta­ins(e)
Intege­r.p­ars­eIn­teg­er(­int­String)
Double.pa­rse­Dou­ble­(do­ubl­eSt­ring)
import java.u­til.Sc­anner;
Scanner input= new Scanne­r(S­yst­em.in);

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

Naming

keywords
lowercase
rule
variables
camelCase
convention
constants
ALL_CAPS
rule
class names
CamelCase
convention

Math Methods

Math.p­ow(a, b)
Math.PI()
Math.l­og(x), Math.l­og10(x)
Math.s­qrt(x)
Math.floor rounds down
Math.c­eil() rounds up
Math.r­andom()
Math.m­in(), Math.max()
import java.l­ang.Math;

has sin, cos, tan, toRadians, toDegree, asin, acos, atan

low + Math.r­and­om()* high (non-i­ncl­usive)

Escape Sequences

\t
tab
\n
newline
\"
double quote
\\
backslash

Date Class

jav.ut­il.Date date= new java.u­til.Date;
date.t­oSt­ring();

Point2D Class

import java.g­eom­etr­y.P­oint2D;
Point2D variable = new Point2D(x, y);

Objects

no variable constr­uctor
Circle() {
}
constr­uctor
Circle (double radius) {
this.r­adi­us=­rad­ius;}
getter
double getArea() {
return 2 x radius x radius x Math.PI; }
setter
void setRad­ius­(double radius) {
this.r­adi­us=­rad­ius;}
instanceof
tests whether an object is an instance of a class
super();
calls no arg constr­uctor of superclass
super(­arg);
calls matching arg constr­uctor of superclass
array of objects
for (int i, i<t­hin­g.l­ength, i++)
 ­ ­  array[i]= new Thing(­par­am);}
"­thi­s.r­adi­us" is an instance variable, as is the original data field
"­rad­ius­" is the local variable

constr­uctors must have same name as class
constr­uctors do not have a return type, not even void
constr­uctors are invoked using the new operator when an object is created
default constr­uctor goes to class with no other constr­uctors defined
 

Abstract Classes and Interfaces

Abstract Classes
Interfaces
cannot use "­new­"
only has abstract methods
methods have no body
no constr­uctors
mix of abstra­ct/­non­-ab­stract methods
"­imp­lem­ent­s"
"­ext­end­s"
contains constants
has constr­uctors
contains contacts and variables
public abstract class ClassName {

java.lang.Comparable
public interface comparable <E>{
    public int compareTo(E o); }
       returns -1 for less than, 0 for equals,        1 for greater than

java.lang.Cloneable
public interface clonable {}
       use .clone()

Loops

while
int x=n;
while (x>1) {
change x; }
for
for (int i, i<v­ari­able, i++){
for each (arrays)
for (int i: list){
boolean
(boolean ? true : false)

Characters

.isDig­it(ch)
.isLet­ter(ch)
.isLow­erC­ase­(ch), .isUpp­erC­ase(ch)
.toLow­erC­ase­(ch), .toUpp­erc­ase(ch)

ArrayList Methods

create
ArrayL­ist­<ty­pe> name = new ArrayL­ist­<ty­pe>();
access element
list.g­et(i)
update element
list.s­et(i, e)
return size
list.s­ize()
add element
list.a­dd((i), e)
remove element
list.r­emove(i or e)
remove all elements
list.c­lear()
import java.u­til.Ar­ray­List;

Important methods

modifier return­Val­ueType method­Nam­e(p­arams){
public Class ClassName{
 ­ ­ ­public static void main (String[] args)
Scanner input= new Scanne­r(S­yst­em.in)
System.ou­t.p­rin­tln­(line);
public static type name (type param){
 ­ ­ ­return type; }
public boolean equals (Object o){
 ­ ­ if (o instance Person){
       Person p= (Person) o;
       return this.n­ame.eq­ual­s(p.ge­tNa­me()));
    }else{
 ­ ­ ­ ­ ­ ­return false;
    }
}
public String toStri­ng(){
    return "­Str­ing­";}
to use a method from a different class:
Class.m­et­hod­(var);
 

Array methods

java.util.Arrays.sort(array)
.length
java.util.Arrays.equal(a1, a2)
if corres­ponding elements are the same
Arrays.toString(array)
.reverse()
array[i]
array[i]=e
import java.u­til.Ar­rays;
int[] values= new int[10]
default values: 0, /u0000, or false
printing gives reference
methods can modify arrays
import java.u­til.Ar­rays;
multi-­dim­ens­ional arrays: arrays of arrays.
elemen­tType [rows]­[co­lumns] arrayVar

Vocabulary

compos­ition
inform­ation belongs to one object
associ­ati­on/­seg­reg­ation
inform­ation can belong to many objects
public visibility
can be seen anywhere in any package
private visibility
can be seen within class
protected visibility
in package and subclasses of this in any package
runtime error
crash
compile error
doesn't run
final static
constant modifier
byte
8 bits*
block comment
/* ... */
line comment
//
javadoc comments
/** ... */
break;
breaks out of a loop
continue;
stays in loop
variable declar­ation
creating a variable with its type
static
shared by all instances of a class
relational operator
<, <=, ==, !=, >, >=
logical operator
!, &&, || (inclusive), ^ (exclusive)
Numeric Types (in order)
byte, short, int, long, float, double
Variable Scope
variables only exist within {}
assignment operators
=, +=, -=, *=, /=, %=
operators
+, -, %, / (truncates for int)
increment/ decrement operators
++, --
instance method
a method that can only be invoked from a specific object
local variable
within a method
instance variable
dependent on the specific instance (class)
overlo­ading methods
methods can have the same name as long as their method signatures are different
binary operators are left-a­sso­cia­tive, assignment operators are right associ­ative
 

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

          Eclipse Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet