Commit f139202f by Adam Gerber

Merge branch 'lab05c'

Conflicts:
	src/lec04/glab/greg/CalendarTestHaleys.java
	src/lec05/glab/casting/PromotionAndCastingPrimitives.java
	src/lec05/glab/eventmodel/MouseClicker.java
	src/lec05/glab/swing/Painter.java
	src/lec06/glab/mortalkombat/Mortal.java
	src/lec06/glab/mortalkombat/MortalKombat.java
	src/lec06/glab/notepad/MyNotepad.java
parents a4fa3d8a 4ae5f15b
package lec05.glab.autobox_example;
import java.util.ArrayList;
/**
* Created by ag on 10/27/2014.
*/
public class AutoBoxDriver {
public static void main(String[] args) {
}
}
...@@ -7,88 +7,7 @@ public class CastingObjects { ...@@ -7,88 +7,7 @@ public class CastingObjects {
public static void main(String[] args) { public static void main(String[] args) {
//will use this for reflection
Class clsSome;
System.out.println("Cast-down examples");
// ===============================================
// ==Assign a subclass object to a superclass reference
// ===============================================
//when considering the use of a cast, you must cast down the class hierarchy
Object objSome = new Integer(17);
System.out.print("an Integer object stored in an Object reference: ");
System.out.print(objSome);
//simple reflection
clsSome = objSome.getClass();
System.out.println(" and the class is " + clsSome.getName());
// ===============================================
// ==cast down to Number
// ===============================================
//notice that when uncommented, this compiler complains
//Number numSome = objSome;
Number numSome = (Number)objSome;
System.out.print("cast down; now an Integer object stored in an Number reference: ");
System.out.print(numSome);
System.out.print(" : numSome == objSome " + (numSome == objSome) );
//simple reflection
clsSome = numSome.getClass();
System.out.println(" and the class is " + clsSome.getName());
// ===============================================
// ==cast down to Integer
// ===============================================
Integer intSome = (Integer) numSome; //this could store a Double or Long or Float, so you must explicitly cast it.
System.out.print("cast down; now an Integer object stored in an Integer reference: ");
System.out.print(intSome);
System.out.print(" : numSome == intSome " + (numSome == intSome) );
//simple reflection
clsSome = intSome.getClass();
System.out.println(" and the class is " + clsSome.getName());
//"casting up" the class hierarchy is not required, just assign
System.out.println("'Cast-up' examples: casting up is not required, just assign");
Number numAnother = intSome;
Object objAnother = intSome;
System.out.print("An Integer object stored in an Integer reference: ");
System.out.println(intSome);
System.out.print("An Integer object stored in a Number reference: ");
System.out.println(numAnother);
System.out.print("An Integer object stored in an Object reference: ");
System.out.println(objAnother);
//why would you want to cast?
//implicit parameters passed as arguments to methods aren't automatically converted
//so you must cast them
//example:
Object objDimension = new Dimension(5,8);
//notice that this complains when uncommented
//Rectangle recOne = new Rectangle(objDimension);
//it's possible (because of polymorphism) that the object
//stored in objDimension is not a dimension and so Java
//imposes this casting discipline on the programmer to ensure the correct type is used
//try to uncomment this and cast
//objDimension = new Double(45.23);
//however a cast-down solves the problem
Rectangle recOne = new Rectangle((Dimension)objDimension);
System.out.println(recOne);
} }
......
package lec05.glab.debug;
import javax.swing.*;
import java.util.Scanner;
/**
* Created by ag on 10/27/2014.
*/
public class CheckedDriver {
public static void main(String[] args) {
boolean bSuccessOrCancelled = false;
String strPath = "";
Scanner scanner = new Scanner(System.in);
}
}
package lec05.glab.debug;
import javafx.scene.shape.Circle;
import java.awt.*;
/**
* Created by ag on 10/27/2014.
*/
public class UncheckedDriver {
public static void main(String[] args) {
int[] nNums = new int[10];
Object obj = new Point(2,13);
Circle cir = null;
}
}
package lec05.glab.debug;
import java.io.IOException;
/**
* Created by ag on 10/27/2014.
*/
public class YourLocalFileNotFoundException extends IOException {
public YourLocalFileNotFoundException(String message) {
super("I couldn't find your local file: " + message);
}
}
package lec05.glab.reflection;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
/**
* Created by ag on 10/27/2014.
*/
public class AnonAbstractReflectorTest {
public static void main(String[] args) {
//uncomment and see what happens; you can't instantiate an interface
//UNLESS you implement all of its contracted methods inline
// ActionListener lis2 = new ActionListener();
// ActionListener lis1 = new ActionListener(); //can't instantiate an abstract class, EXCEPT anonymously like above
}
}
package lec05.glab.reflection;
/**
* Created by ag on 10/27/2014.
*/
public class AnonInterfaceReflectorTest {
//create an anonymous inner class such as runnable
public static void main(String[] args) {
}
}
package lec05.glab.reflection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by ag on 10/27/2014.
*/
public class ReferenceAnonDriver {
public static void main(String[] args) {
}
}
package lec05.glab.reflection;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* Created by ag on 10/27/2014.
*/
public class ReflectTester {
//store an HashMap in a Map Interface
public static void main(String[] args) {
//store a concrete class in an abstract super-class reference
}
}
...@@ -32,7 +32,7 @@ public class Reflector { ...@@ -32,7 +32,7 @@ public class Reflector {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. // e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println("That's now a valid class name, try again."); System.out.println("That's now a valid class name, try again.");
continue; continue;
} }
...@@ -103,9 +103,9 @@ public class Reflector { ...@@ -103,9 +103,9 @@ public class Reflector {
System.out.print(" "); System.out.print(" ");
Class[] interfaces = cl.getInterfaces(); Class[] interfaces = cl.getInterfaces();
for (int nC = 0; nC < interfaces.length; nC++) { for (int nC = 0; nC < interfaces.length; nC++) {
if (nC > 0) System.out.print(", "); if (nC > 0) System.out.print(", ");
System.out.print(interfaces[nC].getName()); System.out.print(interfaces[nC].getName());
} }
System.out.println(); System.out.println();
System.out.println(); System.out.println();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment