Number Conversions
Binary (Base 2 -> 0s and 1s)
Hex (Base 16 0-9 A-F)
Octal (Base 8 0-7)
Errors
Syntax/Compile
Logical
Runtime
Exceptions:
Arithmetic (div by 0)
NullPointer (object not fully created)
ArrayIndexOutOfBounds (ref. invalid index - off by one)
Program - Control Flow
If/else
Loops: for (old school), for (with extenders), while
Math Operators: +, -, *, /, %, ++, --, +=, ! etc.
Logic Operators: ==, <=, >= <, > !=
Binary Operators: &&, || (short circuiting)
De Morgan's Laws
!((a || b) && (a && b)) = (!a && !b) || (!a || !b)
String Class
length, substring (overloaded), indexOf, compareTo
equals, equalsIgnoreCase
Math Class (static - no object
required)
sqrt, pow, random (mult by int and cast to int to get whole #), abs
(overloaded int/double)
Integer Class - Wrapper Class
for int primitive data
Integer.MAX_VALUE and Integer.MIN_VALUE
Object Class - All
Objects Inherit following methods from Object which is parent of
all!
boolean equals (Object obj) - checks if this object and obj memory
aliases
String toString() - returns back memory address of this object
Anatomy of a Basic Class
instance variables - must define as private to enforce encapsulation
constructors (sometimes overloaded)
accessor methods vs mutator methods
method overloading vs method overriding
public methods can be executed/accessed from another class
Class Inheritance
A class which extends another class - becomes a child/sub class of
the
class it is extending - its parent/super class.
When a child extends a parent, it inherits all the public properties
of parent
the this reserved word allows an object to refer to itself
the super reserved word allows an object to talk to a parent method
super() - calls parent's constructor -- must be first line is
child's constructor.
super(n);
super.toString(); calls parent's toString method - which implies the
child has overridden
parent's toString method
Interfaces
an interface is a collection of abstract methods -- a placeholder in
a system.
it is NOT a class and can not be allocated memory - the new operator
cannot be used on!
if a class, implements an interface it must provide a body (a full
method) for all methods in the interface - otherwise a syntax error!
Interface f = new Bingo(); //is ok -- f has Bingo in it...
Abstract Classes
an abstract class is a combination of both an interface and normal
class.
When a child extends an abstract class has a parent, it inherits all
the public properties of
parent AND MUST PROVIDE a body (a full method) for all methods in
the ABSTRACT CLASS!
!a placeholder in a system!
!it is NOT a normal class and can not be allocated memory - the new
operator cannot
be used on!
Polymorphism
is the process of binding objects to its respective class type at
runtime rather than compile time.
used in ArrayLists..
can only be used with inheritance!
small into big... Holiday day = new Christmas(); //good -- day is a
Christmas object
Christmas day = new Holiday(); //bad syntax error..
--REMEMBER THAT THE THIS IS THE OBJECT -- Christmas above...
Arrays
A Collection of like items (one d - just rows). referenced via an
index... index starts at 0 and goes
to length minus 1 -- length is a constant.. --ARRAYS ARE FIXED IN
LENGTH!
-each individual element in array takes on properties of its data
type.. thus:
int[] numbers = new int[10]; //each cell has 0 in it..
String[] names = new String[10]; //each cell has null in it..
--> can use initializer lists to set up -> int[] numbers = {10, 20,
30};
for (int x=0; x < numbers.length; x++) {
numbers[x]=55;
}
Double Arrays
A Collection of like items (two d - rows (vertical) and columns
(horizontal)).
Always accessed via two fors.. inner for loop which deals directly
with cells - columns
int[][] numbers = new int[10][10]; //100 cells total
for (int x=0; x < numbers.length; x++) {
for (int y=0; y < numbers[0].length; y++) {
numbers[x][y]=55;
}
}
List Interface - ArrayLists
An ArrayList implements the List Interface -- Lists are dynamic -
they grow and compress
as needed...
ArrayList<String> names = new ArrayList<String>();
ArrayList class methods:
add - overloaded - one arg -- appends, add two args - first arg int
insert at that spot
size() - how many items in list
set(int, object) - set object at int with new object (a replace)
get(int) - return object back at spot int
remove(int) - remove object at spot int in list
old school for loops:
for (int x=0; x < names.size(); x++)
System.out.println(names.get(x));
with Extenders:
for (String n : names)
System.out.println(n);
Classes using an array of other
objects
public class Whatever {
private String name;
public String getName() {return name;}
}
public class Something {
private final MAX_AMOUNT=100;
private Whatever[] W = new Whatever[MAX_AMOUNT];
public String toString() {
String combine="";
for (Whatever A: W)
combine+=A.getName() + " ";
return combine;
}
}
Classes using an ArrayList of
other objects
public class Whatever {
private String name;
public String getName() {return name;}
}
public class Something {
private ArrayList<Whatever> W = new
Whatever<Whatever>();
public String toString() {
String combine="";
for (Whatever A: W)
combine+=A.getName() + " ";
return combine;
}
}
Recursion
The process of a method calling itself until a base condition is
hit.
if no base condition, infinite recursion.
public void mystery(int n) {
//base condition follows
if (n != 0) {
print(n + " ");
mystery(n - 1);
}
}
mystery(2);
Rules - When Passing Args/Params
to Methods
When passing a primitive data type (int, boolean, double) or String
to a method - you are passing
a copy of the value in the variable -- not the memory address of the
variable - so any change in the method
to an arg which is a primitive data type or String - is not
permanent - everything else is an object - so you
are passing a memory address and changes stick!
public void someThing(String name, int[] numbers) {
name="bingo";
numbers[0]=79;
}
String n="nick";
int[] nums = {1, 2, 3};
someThing(n, nums);
System.out.println("n= " + n + " nums= " + nums);
Static methods & variables
Methods and instance variables declared with modifier static are
methods and variables which are associated
at the class level - not an object - so only one copy of them
associated with the class - each object does not get
a copy of! static variables are used a lot for counters (how many
objects of something being created) - static
methods - are used when a method will be used frequently, does not
require an object, and is accessing a
static variable -- static variables can only be accessed -
manipulated via static methods
GridWorld
Actor Classes (useful methods in - such as: getDirection etc..):
Rock - nothing..
Bug - attempts to move one spot in direction it is facing - if it
does, leaves flower in prior spot
(can move to blank spots or flowers)
if cannot move, turns 45 degrees clockwise..
BoxBug - same as bug .. child of bug -- except when cannot move, act
method overriden to turn twice.
Flower - every times it acts, it darkens 0.05%
Critter - Act: getActors, processActors, getMoveLocations,
selectMoveLocation, makeMove
(eats everything except Rock & Critters)
ChamelonCritter - child of Critter - processActors and makeMove
overriden
ChamelonCritter does not eat - changes color to the one selected in
processActors
Location & AbstractGrid Class (implements Grid interface - needs
Grid Object - ArrayLists returned)
|