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)
- Mathematical
- +, -, *, /, %
- ++, --
- +=, -=
- + (String concatenation)
- Relational
- ==, !=, < , <=, >, >=
- Logical
- &&, ||
- !
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 )
Usually not good for processing loops since there is no index counter to use; e.g. someArrayList.remove(ctr)
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)
Comparing Primitives and Objects
- Use == to compare primitives
- Use .equals() OR compareTo( Object o ) to compare objects
- Using "==" with objects only check to see if to references point to the same object! Only use if you want this!
Classes
- Instance variables
- Methods: getters and setters (or accessors and mutators)
-
Constructors
- Uses of "this" ( this.yourValue and this() )
- toString() method
- Visibility: "private" and "public" (no "protected")
- Class Design - Projects where students are required to design their own classes
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 (.html)
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, Fibbonacci sequences)
3. Number System Conversion (dec, bin, hex, oct)
Teaching the binary number system
Counting Octopus on YouTube
Q: Why do computer scientists confuse Halloween and Christmas?
A: Because they can't tell the difference between 31Oct and 25Dec.