import java.lang.*; import java.io.*; /** An Experimental polymorphic object. A Thing can a large number of different types of thing at different times and can identify its type and value in most cases. The precide type of Thing is determined when it is constructed. */ class Thing extends Object { /** The thing's current object */ Object thing; /** Identifies the class of the current thing

@return The name of the actual actual class of the polymorphic thing */ public String myClass(){ return thing.getClass().toString(); } /**prints out the class and value of the current thing. */ public void whoAmI(){ System.out.println( "I am in " + myClass() + " with value \"" + thing.toString() +"\""); } /**Default constructor for Thing of unknown type*/ public Thing(){super();thing=new Object();} /**Default constructor for an Object of any class*/ public Thing(Object x){thing=x;} // above handles all Objects but not basic data types /** Construct a Thing that is actually an int */ public Thing(int x){thing=(Object)(new Integer(x));} /** Construct a Thing that is actually a long*/ public Thing(long x){thing=(Object)(new Long(x));} /** Construct a Thing that is actually a float*/ public Thing(float x){thing=(Object)(new Float(x));} /** Construct a Thing that is actually a double*/ public Thing(double x){thing=(Object)(new Double(x));} /** Construct a Thing that is actually a char*/ public Thing(char x){thing=(Object)(new Character(x));} /** Construct a Thing that is actually a boolean*/ public Thing(boolean x){thing=(Object)(new Boolean(x));} // no Byte: public Thing(byte x){thing=(Object)(new Byte(x));} // no Short: public Thing(short x){thing=(Object)(new Short(x));} /** Converts a Thing into a string*/ public String toString(){return thing.toString();} /** If the thing is an Integer then its value can be found else an exception may be thrown */ public Integer toInteger(){return (Integer)thing;} }//Thing /** Execute the Doit class to try out the Thing class */ class Doit{ /** Application Starting point Uses "Doit.doit()" to get the work done */ public static void main(String argv[]){ doit(); System.exit(0); }//main /** A piece of shorthand for printing a string*/ private static void p(String s) { System.out.println(s);} /** Try out a variety of Things: strings, ints,... used by "main" when `Doit` is run as an application */ public static void doit() { Thing x=new Thing("abc"); p("x"); x.whoAmI(); p("new Thing (x)"); (new Thing(x)).whoAmI(); p("new Thing()"); (new Thing()).whoAmI(); Thing y=new Thing(123); p("y=new Thing(123);"); y.whoAmI(); y = new Thing("xyz"); p("y = new Thing(\"xyz\");"); y.whoAmI(); y=new Thing(1.23); p("y=new Thing(1.23); "); y.whoAmI(); y=new Thing(1==2); p("y=new Thing(1==2);"); y.whoAmI(); p("y=new Thing('A');"); y=new Thing('A'); y.whoAmI(); p("y=new Thing(new Exception());"); y=new Thing(new Exception()); y.whoAmI(); Thing array[]={ new Thing(2), new Thing('A'), new Thing(x) }; p("Thing array[]={ new Thing(2), new Thing('A'), new Thing(x) };"); for(int i=0; i<3;i++){ p("array[" + i + "]"); array[i].whoAmI(); }//end for //following raises an exception since y is no longer an integer try{ System.out.println( "y is in " + y.myClass() + " with value " + y.toInteger().toString() ); } catch (Exception e) { System.out.println("an exception occurred: " + e.getMessage()); e.printStackTrace(); //Esceptions are ALSO possible things! p("y as an exception"); y=new Thing(e); y.whoAmI(); } }//doit }//Doit