_2playOnTheFringe();
/* The code Compiles - which was amazing 2me - but - jvm wasn’t gonna let me get away that easy - tryna - get to print instantiation to the console: print: new Alien(); i think i have to convert the hashCode to a string representation (or maybe override toString() - or - something ?)
-Compiles fine - But - An exception was thrown @rt
(int) (o = x>y ? new Alien(): new Vulcan()) oops! class cast exception
com.me.Vulcan cannot be cast to java.lang.Integer Exception in thread "main" java.lang.ClassCastException
(playing around w/Bucky’s tutorials 59-60: in an OCA type-a-way) */
import static java.lang.System.out; import java.util.Arrays;
public class AlphaCentauri_Ab {
public static void main(String... _$_){ Alien.elfoneHome(new Romulan());/* put - try/catch here instead */ } } class Alien { //Add objects To the Array public static void elfoneHome(Alien et){ Alien[] rayGun = new Alien[3]; et = new Alien(); /* i want it to print: new Alien() in the array - but - it won’t - ( not yet - anyway )*/ for(int x=0 ; x< rayGun.length; x++){ rayGun[x] = et; print(Arrays.toString(rayGun)); print("rayGun[x]= " + rayGun[x]); } } //custom print: static void print(Object o){ out.println(o); } @Override /* doing this bc (the program runs fine w/out this) -but- outputs hashcode - im tryna “force” it to output: new Alien() new Vulcan() */ public int hashCode(){ /*Integer return gets unboxed (below)*/ Integer x = new Integer(1), y= new Integer(2), q = 50; Integer z= new Integer(0); /* cool multi-initialization - above - mixing int & Integer */
Object o = new Alien(); /*polymorphic instantiaton*/ o = new Romulan(); /* tryna turn a Romulan into an Integer - bc - they’re both Objects - jvm said NO - */
print("(int) (o = x>y ? new Alien(): new Vulcan()) ") ; try{ /* tryna return: new Alien() -or- new Vulcan() instead of hashcode: */ return (int) (o = x>y ? new Alien(): new Vulcan()) ; }catch(ClassCastException cce) { print("oops! class cast exception"); print(cce.getMessage()); } throw new ClassCastException(); } } class Romulan extends Alien { } class Vulcan extends Alien{ }
//------
/* Alternate method2Add object to the Array: Using VarArgs */
static void elfoneHome(Alien et, Alien... varLien){ varLien = new Alien[3]; et = new Alien(); for(int x=0 ; x< varLien.length; x++){ varLien[x] = et; print(Arrays.toString(varLien)); print("varLien[x]= " + varLien[x]);
//the easy way out - just print this:
print("new Alien added to the array @index: " + x);
}









