SORTING
SELECTION (ints only) / INSERTION (anything via compareTo) /
MERGESORT (FASTER) - DIV/CONQUER
BIG O NOTATION -> SELECTION/INSERTION = On2 -> MS = Ologn
SEARCHING - LINEAR (SLOW - ONE
BY ONE) / BINARY (MUST BE SORTED IN ASCENDING ORDER DIV/CONQUER -
FASTER)
ABSTRACT CLASSES/INTERFACES
//Interface -- Can Not Create an Object From!!!
//YOU NEED TO KNOW COMPARABLE...
public interface Info {
public int tryIt(int i);
public void hiThere();
}
//A Class Implementing it..
public class NewInfo implements Info {
private int type;
//from interface
public int tryIt(int i) {
int x;
x = i * 5;
return x;
}
//from interface
public void hiThere() {
System.out.println("hi there!");
}
//regular method in NewInfo
public void doThat(int i) {
int i= 5 * 9;
}
}
Info w = new NewInfo(); // this is ok -- something smaller -> bigger
- w is a NewInfo
//here is the abstract class
//both method headers and methods with a body - Create Not Create An
Object From
public abstract class Info {
private int theOne;
public abstract int tryIt(int i); //an abstract method - no body
public void hiThere() {
System.out.println("hi there!"); }
}
}
//here is the child class
//method headers must be written with a body
//for all abstract class from parent
public class BabyInfo extends Info {
//an abstract method - provide a body for
public int tryIt(int i) {
int x = i + 7;
return (x);
}
public void noNo() {
System.out.println("No No!"); }
}
}
Info w = new BabyInfo(); // this is ok -- something smaller ->
bigger - w is a BabyInfo..
THE GRIDWORLD ABSTRACTGRID CLASS WHICH
IMPLEMENTS THE GRID INTERFACE - SO NEED TO USE
getGrid() which returns an object to work on the following:
int getNumRows()
returns the number of rows, or -1 if this grid is unbounded
int getNumCols()
returns the number of columns, or -1 if this grid is unbounded
boolean isValid(Location loc)
returns true if loc is valid in this grid, false otherwise
Precondition: loc is not null
E put(Location loc, E obj)
puts obj at location loc in this grid and returns the object
previously at that location (or null if the
location was previously unoccupied).
Precondition: (1) loc is valid in this grid (2) obj is not null
E remove(Location loc)
removes the object at location loc from this grid and returns the
object that was removed (or null if the
location is unoccupied)
Precondition: loc is valid in this grid
E get(Location loc)
returns the object at location loc (or null if the location is
unoccupied)
Precondition: loc is valid in this grid
ArrayList<Location> getOccupiedLocations()
returns an array list of all occupied locations in this grid
Example:
Grid gr = getGrid();
ArrayList<Location> theLocs = gr.getOccupiedLocations();
[ANY QUESTION ON WHAT ADJACENT MEANS???]
ArrayList<Location> getValidAdjacentLocations(Location loc)
returns an array list of the valid locations adjacent to loc in this
grid
Precondition: loc is valid in this grid
Grid gr = getGrid();
Location loc = getLocation();
ArrayList<Location> theLocs = gr.getValidAdjacentLocations(loc);
ArrayList<Location> getEmptyAdjacentLocations(Location loc)
returns an array list of the valid empty locations adjacent to loc
in this grid
Precondition: loc is valid in this grid
Grid gr = getGrid();
Location loc = getLocation();
ArrayList<Location> theLocs = gr.getEmptyAdjacentLocations(loc);
ArrayList<Location> getOccupiedAdjacentLocations(Location loc)
returns an array list of the valid occupied locations adjacent to
loc in this grid
Precondition: loc is valid in this grid
Grid gr = getGrid();
Location loc = getLocation();
ArrayList<Location> theLocs = gr.getOccupiedAdjacentLocations(loc);
ArrayList<E> getNeighbors(Location loc)
returns an array list of the objects -- -[THE ACTORS -- THE <E> IS
ACTORS] in the occupied
locations adjacent to loc in this grid
Grid gr = getGrid();
Location loc = getLocation();
ArrayList<Actor> theActors = gr.getNeighbors(loc);
LOCATION CLASS
Some Examples:
int row, col;
Location currentLoc = getLocation();
row=currentLoc.getRow();
col=currentLoc.getCol();
row++;
col++;
Location newLoc = new Location(row,col);
THE CONSTANTS IN LOCATION CLASS
(ALL PUBLIC AND STATIC SO CAN ACCESS DIRECTLY FROM CLASS LEVEL)
EXAMPLE:
System.out.println(Location.NORTH);
-or-
int myCompass = Location.NORTHWEST;
Compass directions:
public static final int NORTH = 0;
public static final int EAST = 90;
public static final int SOUTH = 180;
public static final int WEST = 270;
public static final int NORTHEAST = 45;
public static final int SOUTHEAST = 135;
public static final int SOUTHWEST = 225;
public static final int NORTHWEST = 315;
Turn angles:
public static final int LEFT = -90;
public static final int RIGHT = 90;
public static final int HALF_LEFT = -45;
public static final int HALF_RIGHT = 45;
public static final int FULL_CIRCLE = 360;
public static final int HALF_CIRCLE = 180;
public static final int AHEAD = 0;
|