Tuesday, November 1, 2011

what is an enum type alternative in java ?

In this blog, I will discuss what is an enum alternative in java and where we should use an enum alternative?

An enum type is a type whose variables have predefined values. An enum is special enum declaration class in Java. Using enum type can reduce bugs in our code. Java Enum type is such really nice feature in Java. But there are a few places where we might use an enum type alternative.

Why we should use an enum type alternative ?
  • Enum type is introduced in JDK 5. We can not use it before JDK 5.
  • All of enum type are subclass of java.lang.Enum.Therefore, enum type can not extend anything else. Multiple inheritance is not supported in Java.
  • java.lang.Enum class provides name(), equals(), hashCode(), clone() and compareTo() final methods. So, Any Enum type can not override final methods.
In the below, I am proposing a one of enum type alternatives solution.
public class EnumTest {
private Day day;
public static class Day {
private int value;
private String name;
public Day(int a,String name) {
this.value = a;
this.name = name;
}
public String name() {
return this.name;
}
public static Day valueOf(String name) {
Day day;
if (name.equals("MON")) {
day = MON;
} else if (name.equals("TUE")) {
day = TUE;
} else if (name.equals("WED")) {
day = WED;
} else if (name.equals("THU")) {
day = THU;
} else if (name.equals("FRI")) {
day = FRI;
} else if (name.equals("SAT")) {
day = SAT;
} else if (name.equals("SUN")) {
day = SUN;
} else {
throw new IllegalArgumentException(name + " is not a constant in Day Class");
}
return day;
}
public static final Day MON = new Day(1,"MON");
public static final Day TUE = new Day(2,"TUE");
public static final Day WED = new Day(1,"WED");
public static final Day THU = new Day(2,"THU");
public static final Day FRI = new Day(1,"FRI");
public static final Day SAT = new Day(2,"SAT");
public static final Day SUN = new Day(1,"SUN");
}
public EnumTest(Day day) {
this.day = day;
System.out.println(this.day.value);
System.out.println(this.day.name());
}
public static void main(String[] args) {
EnumTest enumTest = new EnumTest(Day.MON);
Day friday = Day.valueOf("FRI");
System.out.println(friday.name);
}
}

Monday, October 31, 2011

JDK 1.7 Language Enhancements

In this blog, I will mainly discuss Java SE 7 platform language enhancements with examples.

The following new language enhancements are shipped in Java SE 7 platform.
  • String Values With Switch Statements
  • Improved Integral Literals
  • "Try with resources" statements
  • Diamond Operator <>
  • Simplified varargs method invocation
  • Multi-catch blocks

Strings Values with Switch Statements

Before Java SE 7, We can use integer or byte value in switch statement. In Java SE 7, string value can be enabled in switch statement.

Improved Integral Literals

Binary Literal can be represented directly instead of hexadecimal.

we can insert underscore(_) as deliminator in number. It can not be used at starting and ending.



Saturday, April 30, 2011

Iterator for 2-D Array in a clockwise inward sprial

Objective:

Implement an iterator for a 2-dimensional array data structure that moves in a clockwise inward spiral

Example:

Input A[4,4] array

01 02 03 04

05 06 07 08

09 10 11 12

13 14 15 16

The above 2-D array would iterate the following way

01 02 03 04 08 12 16 15 14 13 09 05 06 07 11 10

Solution: The problem can be solved in many different ways. I have solved this problem in Java Programming language using recursive way.

public class InwardSprial {

public void top_bottom(T[][] a, int x1, int y1, int x2, int y2) {

for (int i = y1; i < y2; i++) {
display(a[x1][i]);
}
x1 = x1 + 1;
if(!hasNext((x2-x1),(y2-y1))) {
return;
}

for (int j = x1; j < x2; j++) {
display(a[j][y2 - 1]);
}
y2 = y2 - 1;
if(!hasNext((x2-x1),(y2-y1))) {
return;
}
bottom_top(a, x1, y1, x2, y2);
}

private void display(T value) {
System.out.print(value + "\t");
}

private boolean hasNext(int x, int y) {
if (x==0 || y==0) {
return false;
}
return true;
}

private void bottom_top(T[][] a, int x1, int y1, int x2, int y2) {
int i = 0, j = 0;

for (i = y2 - 1; i >= y1; i--) {
display(a[x2 - 1][i]);
}
x2 = x2 -1;
if(!hasNext((x2-x1),(y2-y1))) {
return;
}

for (j = x2 - 1; j >= x1; j--) {
display(a[j][y1]);
}
y1 = y1 +1;
if(!hasNext((x2-x1),(y2-y1))) {
return;
}

top_bottom(a, x1, y1, x2, y2);
}

public static void main(String[] args) throws IOException {

int x=5,y=4;
Integer[][] a = new Integer[x][y];
int num =0;
for (int i=0; i is = new InwardSprial();
is.top_bottom(a, 0, 0, x, y);
}

}

Sunday, December 7, 2008

How to create domU in Open Solaris

Step 1 : Add the packages with the following commands

pkg install SUNWvirtinst
pkg install SUNWurlgrabber
pkg install SUNWlibvirt
pkg install SUNWxvmhvm
pkg install SUNWxvmdom
pkg install SUNWxvm
pkg install SUNWgccruntime
pkg install SUNWgnutls
pkg install SUNWlibsdl
pkg install FSWxwpft
pkg install FSWxwrtl

Step 2: Add the following entry in /rpool/boot/grub/menu.lst
----
title Solaris xVM
bootfs rpool/ROOT/opensolaris
kernel$ /boot/$ISADIR/xen.gz
module$ /platform/i86xpv/kernel/$ISADIR/unix /platform/i86xpv/kernel/$ISADIR/unix -B $ZFS-BOOTFS
module$ /platform/i86pc/$ISADIR/boot_archive
----

Step 3: reboot the system

Wednesday, July 23, 2008

How to install GD library in Solaris

Step 1: Download GD Graphics library in the Solaris website based on Processor/OS

http://sunfreeware.skynet.be/
Result: you get gd-2.0.35-solXXX-YYY-local.gz
XXX may be 7, 8, 9, 10. YYY may be x86, SPARAC

Step2: Check the /usr/sfw/lib directory

You should have the following *.so file
libgcc_s.so
libgd.so

If any of the above files are missing, you can download it from http://sunfreeware.skynet.be/ based on Processor/OS and run two commands :

* gunzip libgcc-2.0.35-solXXX-YYY-local.gz
Result: you get libgcc-2.0.35-solXXX-YYY-local
* pkgadd libgcc-2.0.35-solXXX-YYY-local

Step3: Run the following two commands after successful completion of step 2

* gunzip gd-2.0.35-solXXX-YYY-local.gz (Result: you get gd-2.0.35-solXXX-YYY-local)
* pkgadd –d gd2.0.35-solXXX-YYY-local