Variable Types
- Primitives: int, double, boolean ("clock" analogy for overrunning bounds of int)
-
Wrapper classes: Integer, Double, Boolean
Example: Integer myInteger = new Integer( 10 );
Example: Double myDouble = new Double( 3.14 );
Example: Boolean myBoolean = new Boolean( true );
- Java Constants (new for 2010 and 2011)
Integer.MAX_VALUE
Integer.MIN_VALUE
This will help with the reading of exam questions that presently contain comments such as < some integer value > and will reiterate that there are maximum and minimum values for integers.
- Static variables (aka "class variables") (new for 2010 and 2011)
Static final variables will continue to be a part of the Course Description.
- Strings
Example: String myName = new String( "Mud" );
Example: String myName = "Mud";
String Pooling Problem
String s1 = "Harry Potter"; //case 1
String s2 = "Harry Potter"; //case 2
In case 1, literal s1 is created newly and kept in the pool. But in case 2, literal s2 refer the s1, it will not create new one instead.
if(s1 == s2)
System.out.println("equal"); //Prints equal.
String n1 = new String("Harry Potter");
String n2 = new String("Harry Potter");
if(n1 == n2)
System.out.println("equal"); //No output.
Moral of the story: Always use "new" when creating Strings!
- 1D and 2D Arrays (2D arrays new for 2010 and 2011)
Example: int[] myArray = new int[10]; // 1D array of primitives
Example: int[][] myArray = new int[10][10]; // 2D array of primitives
Example: String[] myArray = new String[10]; // 1D array of Objects
Example: String[][] myArray = new String[10][10]; // 2D array of Objects
Example: List[][] myArray = new List[10][10]; // 2D array of objects that implement List interface
Projects: "Memory!", "Battleship"
- ArrayList
Exampe: List< Integer > someArrayList = new ArrayList< Integer >();
(note: List reference to an ArrayList...new in 2010)
Iteration
- for loop
for( int index = 0; index < someArrayList.size(); index++ )
{
}
for( int index = 0; index < someArray.length; index++ )
{
}
- for each loop ( aka enhanced for )
for( Car c : garageOfCars )
{
System.out.println( c.getName() );
}
- while loop
while( ctr < 100 )
{
ctr++;
}
-
The classic "for loop removal" problem
When removing adjacent elements in an ArrayList, an element may be missed if loop counter is not adjusted.
Possible solutions:
(a) decrement counter, OR
(b) use "while" instead of "if" within the body loop (elegant solution)
Decision-Making
- if statement
- if else statement
- switch statement (not tested)
Classes
- Instance variables
- Methods: getters and setters (or accessors and mutators)
- toString() method
-
Constructors
- Uses of "this" ( this.yourValue and this() )
Inheritance
- Abstract classes
- Interfaces (100% abstract class!)
- Polymorphism (Using superclass references for subclass objects)
- Reference types: concrete classes, abstract claseses, interfaces
- Uses for super ( super.yourMethod and super() in constructors)
1. Searching and Sorting
Google "searching and sorting simulations" for great graphical simulations of all types of sorts.
MathSite Searching/Sorting Simulations
Big-O Tutorial
- Sequential search
- Binary search
- Selection sort
- Insertion sort
- Mergesort (use students on stairs to show levels of recursion)
Here is the mini-project description for the Sorting/Searching Project
Sorting and Searching Mini-Project
Sorting and Searching Mini-Project Rubric (.pdf)
Sorting and Searching Mini-Project Rubric (.xls)
Timer/Stopwater Test Bench (.java files)
Some Search/Sorting Role Play Picutures
Sort 1 Sort 2 Sort 3
Sort 4 Sort 5 Sort 6
Sort 7 Sort 8 Sort 9
Sort 10 Sort 11 Sort 12
2. Recursion (introduce with Mergesort algorithm, fractals!)
3. Number System Conversion (dec, bin, hex, oct)
Java Collections
- List myList = new ArrayList();
- 2D Arrays - create, initialize, modify, traverse with nested loops (new for 2010)