Commit 8dc03e7b by Joe Student

merged from lab07a

parents dc1d6d12 a6c598f2
package lec07.glab.casting;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: ag
......@@ -37,7 +39,7 @@ public class CastObjectsDriver {
//With reflection, we can see that we are NOT changing the object's type, it's still a Double, only now it's
//being stored in a superlcass reference which restricts the of methods we can call on the reference
//This is an automatic promotion, so the cast here is redundant
Number numMe = (Number) dubMe;
Number numMe = dubMe;
System.out.println("A Number reference pointing to same Double object:");
System.out.println(numMe.getClass().toString());
//one of the methods we can call from the Number reference
......@@ -58,7 +60,7 @@ public class CastObjectsDriver {
System.out.println("numMe and dubMe both point to the same object in memory space: " + (numMe == dubMe));
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
System.out.println(Double.valueOf((Double) comMe));
//Now let's cast this to an Object
//This is an automatic promotion, so the cast here is redundant
Object objMe = (Object) comMe;
......@@ -91,13 +93,15 @@ public class CastObjectsDriver {
//toggle uncomment/comment to show compile-time error
//Rectangle recMe = (Rectangle) numMe;
//However, if we try to cast comMe to a Rectangle (Rectangle implements Comparable) the compiler will NOT complain, but of course, we will still throw ClassCastException
//because the underlying object stored in comMe is still of type Double, and Rectangle is not in Double's class hierarchy.
//toggle uncomment/comment this code below to throw a ClassCastException
// Rectangle recMeAgain = (Rectangle) comMe;
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//Let's go full circle. Casting-down is trivial.
//Let's go full circle. Casting-down is trivial. When you cast down, you are actually opening the aperture.
//The underlying object that objMe points to is of type Double. If it weren't of type Double, we would get a ClassCastException
//We are effectively widening the filter to see all the methods of the original Double object
Double dubMeDown = (Double) objMe;
......
......@@ -28,7 +28,7 @@ public class CastPrimitivesDriver {
//Let's promote this to an int which is a 32-bit signed integer ranging from -2^31 to 2^31 - 1
int nMe = (int)sMe;
int nMe = sMe;
//no problem, we don't really need all the precision, but memory is cheap, and the value stays the same.
//when we copy the bits, we just put those bits into an 32-bit int like so ->
//upcasting is automatic (no cast required) and looses no precision
......@@ -36,7 +36,9 @@ public class CastPrimitivesDriver {
//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1
//0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1
System.out.println("Integer value of sMe : " + Integer.valueOf(nMe));
System.out.println("After upcasting, int value of sMe : " + Integer.valueOf(nMe));
//and the original value stored in sMe is unchanged.
System.out.println("Short value of sMe : " + Short.valueOf(sMe));
......@@ -53,13 +55,15 @@ public class CastPrimitivesDriver {
byte yMe = (byte) sMe;
System.out.println("Byte value of sMe : " + Byte.valueOf(yMe));
System.out.println("After downcasting, byte value of sMe : " + Byte.valueOf(yMe));
//If we cast to a double (64-bit floating point value), we lose no precision. The number just becomes 151.0
double dMe = (double) sMe;
//when we use the == operator on primitives, we are not checking for memory addresses, but rather values
System.out.println("These numbers, 151 and 151.0 are numerically equivalent : " + (sMe == dMe));
System.out.println("These numbers, 407 and 407.0 are numerically equivalent : " + (sMe == dMe));
System.out.println("#################################################################################");
......
......@@ -36,12 +36,12 @@ public class MockTable {
protected void drawCards(Graphics g) {
BufferedImage bufferedImage;
//use this counter to position the cards horizontally
int nC = 10;
int nXPos = 10;
for (Card card : crdHands) {
bufferedImage = SoundImageUtils.genBuffImage("/src/lec07/glab/jpanel_gui/imgs/" + card + ".png");
//nc = x-pos, 100 = y-pos
g.drawImage(bufferedImage, nC, 100, null);
nC = nC +25;
g.drawImage(bufferedImage, nXPos, 100, null);
nXPos = nXPos +25;
}
}
......
......@@ -8,6 +8,10 @@ public class RecursionDriver {
public static void main(String[] args) {
System.out.println(myFactorialRec(12));
System.out.println(myFactorialIter(5));
......
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