Pages

Friday, February 13, 2015

Got Crazy with nested classes :)

This time I tried enclosing 23 levels of sub classes with a combination of static and plain nested subclasses and guess what!!! It worked for me. Guess My demo of nested sub classes in java is world class rofl

Step 1: created a class like the following

package com.oz.core.NestedClasses;

public class OuterClass {

public static class NestedClass {

public class SubNestedClass {
public class Level4 {
public class Level5 {
public class Level6 {
public class Level7 {
public class Level8 {
public class Level9 {
public class Level10 {
public class Level11 {
public class Level12 {
public class Level13 {
public class Level14 {
public class Level15 {
public class Level16 {
public class Level17 {
public class Level18 {
public class Level19 {
public class Level20 {
public class Level21 {
public class Level22 {
public class Level {
Level() {
System.out
.println("Road To Eldorado WHOOO...HOOOO");
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

}

Step 2: instantiated the classes Lol

package com.oz.core.NestedClasses;

import com.oz.core.NestedClasses.OuterClass.NestedClass.SubNestedClass.Level4.Level5;

public class Main {
public static void main(String args[]) {
OuterClass outCls = new OuterClass();
OuterClass.NestedClass nesCls=new OuterClass.NestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass=
nesCls.new SubNestedClass();
OuterClass.NestedClass.SubNestedClass subNestedClass2=
new OuterClass.NestedClass().new SubNestedClass();

OuterClass.NestedClass.SubNestedClass.Level4 lvl4= subNestedClass2. new Level4();
OuterClass.NestedClass.SubNestedClass.Level4.Level5.Level6.Level7.Level8.Level9.Level10
.Level11.Level12.Level13.Level14.Level15.Level16
.Level17.Level18.Level19.Level20.Level21.Level22
.Level level = lvl4.new Level5().new Level6().new Level7().new Level8().new Level9().new Level10()
.new Level11().new Level12().new Level13().new Level14().new Level15().new Level16().new Level17().new Level18()
.new Level19().new Level20().new Level21().new Level22().new Level();
}
}

Step 3: ran the main program to see the output on the console ☺

Console Output:

Road To Eldorado WHOOO...HOOOO

Wednesday, February 11, 2015

LOL with Arrays in Java

Well I do get crazy when I start experimenting some times. Never dared to do this in real life aparting from pushing my limits when it came to swallowing stuffs and touching electric bulbs. But I really do feel like I get very crazy when it comes to my computers. It does not cost me much ehh... apart from the time I spent on doing it :D

Coming to the point, I grew interest in Nano technology, Astronomy, Relativity, Time Travel, Beyond 3 Diminsions... and there I was, I am not that good in science yet to show you guys the fourth dimension, but how about it in Java ROFL. I went up till 23 dimensions LOL. 23-Dimensional array.

I wrote down the following class

package com.oz.core.Arrays;

public class ArrayExperiments {

public static void main(String args[]) {
int[] intArray = new int[10];
int[][] int2dArray = new int[10][10];
int[][][] int3dArray = new int[10][10][10];
int [][][][] int4dArray=new int [10][10][10][10] ;
int [][][][][] int5dArray=new int [10][10][10][10][10] ;
int [][][][][][] int6dArray=new int [10][10][10][10][10][10] ;
int [][][][][][][] int7dArray=new int [10][10][10][10][10][10][10] ;
int [][][][][][][][] int8dArray=new int [10][10][10][10][10][10][10][10] ;
int [][][][][][][][][] int9dArray=new int [10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][] int10dArray=new int [10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][] int11dArray=new int [10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][] int12dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][] int13dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][] int14dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][] int15dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][] int16dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][] int17dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][] int18dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][][] int19dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][][][] int20dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][][][][] int21dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][][][][][] int22dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;
int [][][][][][][][][][][][][][][][][][][][][][][] int23dArray=new int [10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10][10] ;



int5dArray[1][2][3][4][5]=999 ;
int6dArray[1][2][3][4][5][6]=1999 ;
int7dArray[1][2][3][4][5][6][7]=2999 ;

System.out.printf("%d + %d + %d = "+
int5dArray[1][2][3][4][5]+
int6dArray[1][2][3][4][5][6]+
int7dArray[1][2][3][4][5][6][7],
int5dArray[1][2][3][4][5],
int6dArray[1][2][3][4][5][6],
int7dArray[1][2][3][4][5][6][7]);

}

}

And I ran it eagerly to see what happens. LOL the following happened. I did it again, but this time the crashed the JVM and not the LOTUS DOMINO SERVER rofl

JVMDUMP006I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" - please wait.
JVMDUMP032I JVM requested Heap dump using 'C:\Accurev_work\Ozinisle\heapdump.20150211.175635.9948.0001.phd' in response to an event
JVMDUMP010I Heap dump written to C:\Accurev_work\Ozinisle\heapdump.20150211.175635.9948.0001.phd
JVMDUMP032I JVM requested Java dump using 'C:\Accurev_work\Ozinisle\javacore.20150211.175635.9948.0002.txt' in response to an event
JVMDUMP010I Java dump written to C:\Accurev_work\Ozinisle\javacore.20150211.175635.9948.0002.txt
JVMDUMP032I JVM requested Snap dump using 'C:\Accurev_work\Ozinisle\Snap.20150211.175635.9948.0003.trc' in response to an event
JVMDUMP006I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" - please wait.
JVMDUMP010I Snap dump written to C:\Accurev_work\Ozinisle\Snap.20150211.175635.9948.0003.trc
JVMDUMP013I Processed dump event "systhrow", detail "java/lang/OutOfMemoryError".
JVMDUMP032I JVM requested Heap dump using 'C:\Accurev_work\Ozinisle\heapdump.20150211.175705.9948.0004.phd' in response to an event
JVMDUMP010I Heap dump written to C:\Accurev_work\Ozinisle\heapdump.20150211.175705.9948.0004.phd
JVMDUMP032I JVM requested Java dump using 'C:\Accurev_work\Ozinisle\javacore.20150211.175705.9948.0005.txt' in response to an event
JVMDUMP010I Java dump written to C:\Accurev_work\Ozinisle\javacore.20150211.175705.9948.0005.txt
JVMDUMP032I JVM requested Snap dump using 'C:\Accurev_work\Ozinisle\Snap.20150211.175705.9948.0006.trc' in response to an event
JVMDUMP010I Snap dump written to C:\Accurev_work\Ozinisle\Snap.20150211.175705.9948.0006.trc
JVMDUMP013I Processed dump event "systhrow", detail "java/lang/OutOfMemoryError".
Exception in thread "main" java.lang.OutOfMemoryError
Exception in thread "Attach API wait loop" at com.oz.core.Arrays.ArrayExperiments.main(ArrayExperiments.java:14)
java.lang.OutOfMemoryError
at com.ibm.tools.attach.javaSE.TargetDirectory.getTargetDirectoryPath(TargetDirectory.java:66)
at com.ibm.tools.attach.javaSE.AttachHandler.connectToAttacher(AttachHandler.java:274)
at com.ibm.tools.attach.javaSE.AttachHandler$WaitLoop.checkReplyAndCreateAttachment(AttachHandler.java:371)
at com.ibm.tools.attach.javaSE.AttachHandler$WaitLoop.waitForNotification(AttachHandler.java:366)
at com.ibm.tools.attach.javaSE.AttachHandler$WaitLoop.run(AttachHandler.java:396)

And this time let me be @%$*&$! Less greedy

package com.oz.core.Arrays;

public class ArrayExperiments {

public static void main(String args[]) {
int[] intArray = new int[10];
int[][] int2dArray = new int[10][10];
int[][][] int3dArray = new int[10][10][10];
int [][][][] int4dArray=new int [10][10][10][10] ;
int [][][][][] int5dArray=new int [10][10][10][10][10] ;



int2dArray[1][2]=999 ;
int3dArray[1][2][3]=1999 ;
int4dArray[1][2][3][4]=2999 ;

System.out.printf("%d + %d + %d = "+
int2dArray[1][2]+
int3dArray[1][2][3]+
int4dArray[1][2][3][4],
int2dArray[1][2],
int3dArray[1][2][3],
int4dArray[1][2][3][4]);

}

}

And this gave a decent out put

999 + 1999 + 2999 = 99919992999

Wait what!!!! Lol rofl

A day for Enumeration perhaps

I had written a post about how to implement a Switch Case using String in past. I had to do a similar thing now and felt like it time I dig more about Enum Classes and objects.

I understand that an object definition using the Enum keyword is similar to a class definition and can be put in its own file. And that the Enum objects by default will extend java.lang.Enum implicitly

An enum type is a special kind of Java class used to define collections of constants, methods etc. It can hold if statements, switch statements, fields, methods, iterations etc. Following is a simple example of my test with an Enum Object and its results

Enum Object definition

package com.oz.core;

public enum Level {
      HIGH(3), // calls constructor with value 3
      MEDIUM(2), // calls constructor with value 2
      LOW(1) // calls constructor with value 1
      ; // semicolon needed when fields / methods follow

      private final int levelCode;

      Level(int levelCode) {
            this.levelCode = levelCode;
      }

      public int getLevelCode() {
            return this.levelCode;
      }
}

Implementation class definintion

package com.oz.core;

public class EnumerationSample {

      public static void main(String args[]) {

            EnumerationSample self = new EnumerationSample();
            self.caseWithStringArgs("medium");
            self.caseWithStringArgs("high");
            self.caseWithStringArgs("critical");
           
            Level level = Level.valueOf("LOW");
            System.out.println("level="+level);
            System.out.println("level.compareTo(Level.valueOf(\"HIGH\"))="+level.compareTo(Level.valueOf("HIGH")));
            System.out.println("level.compareTo(Level.valueOf(\"LOW\"))="+level.compareTo(Level.valueOf("LOW")));
            System.out.println("level.compareTo(level.HIGH)="+level.compareTo(level.HIGH));
            System.out.println("level.compareTo(level.LOW)="+level.compareTo(level.LOW));
            System.out.println("level.getLevelCode()="+level.getLevelCode());
            System.out.println("level.hashCode()="+level.hashCode());
            System.out.println("level.name()="+level.name());
            System.out.println("level.ordinal()="+level.ordinal());
            System.out.println("level.toString()="+level.toString());
            System.out.println("level.equals(Level.LOW)="+level.equals(Level.LOW));
            System.out.println("level.equals(Level.MEDIUM)="+level.equals(Level.MEDIUM));
            System.out.println("level.equals(Level.valueOf(\"LOW\"))="+level.equals(Level.valueOf("LOW")));
            System.out.println("level.equals(Level.valueOf(\"MEDIUM\"))="+level.equals(Level.valueOf("MEDIUM")));
            System.out.println("level.getClass()="+level.getClass());
            System.out.println("level.getDeclaringClass()="+level.getDeclaringClass());
           
            for (Level lvl : level.values())    {
                  System.out.println("lvl="+lvl);
            }
           
            /*System.out.println("level="+Level.valueOf(arg0, arg1));
            System.out.println("level="+level.notify());
            System.out.println("level="+level.notifyAll());
            System.out.println("level="+level.wait());
            System.out.println("level="+level.wait(arg0, arg1));*/           
           
      }
     
      String caseWithStringArgs(String arg)     {
           
            String evaluatedString = "Not in list";
            try   {
                  Level level = Level.valueOf(arg.toUpperCase());
                 
                  switch (level) {
                  case HIGH:
                        evaluatedString = "case statement for High";
                        break;

                  case MEDIUM:
                        evaluatedString = "case statement for Medium";
                        break;

                  case LOW:
                        evaluatedString = "case statement for Low";
                        break;

                  default:
                        break;
                  }
            } catch     (IllegalArgumentException exp)      {
                 
            } finally   {
                  System.out.println(evaluatedString);
                  return evaluatedString;
            }          
      }
}

Following are the set of obtained console statements

case statement for Medium
case statement for High
Not in list
level=LOW
level.compareTo(Level.valueOf("HIGH"))=2  //HIGH=3, LOW=1, so HIGH-LOW=2
level.compareTo(Level.valueOf("LOW"))=0   //LOW=1, LOW=1, so LOW-LOW=2
level.compareTo(level.HIGH)=2             //HIGH=3, LOW=1, so HIGH-LOW=2
level.compareTo(level.LOW)=0              //LOW=1, LOW=1, so LOW-LOW=2
level.getLevelCode()=1                    //LOW=1
level.hashCode()=650651336
level.name()=LOW                          //name
level.ordinal()=2                         //ordinal = 2 - returns its position in its enum declaration
level.toString()=LOW
level.equals(Level.LOW)=true
level.equals(Level.MEDIUM)=false
level.equals(Level.valueOf("LOW"))=true
level.equals(Level.valueOf("MEDIUM"))=false
level.getClass()=class com.oz.core.Level
level.getDeclaringClass()=class com.oz.core.Level     //Two enum constants e1 and e2 are of the same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass().
lvl=HIGH          //printed because of the iteration statement with in enum
lvl=MEDIUM        //printed because of the iteration statement with in enum
lvl=LOW           //printed because of the iteration statement with in enum


Hope this helps