Tuesday Dec 15, 2009

Lightweight and Heavyweight components - Mess it !

So, if you are a Swing Developer, you have heard many stories where someone messed Lightweight component with Heavyweight component. In one line " A heavyweight component is one that is associated with its own  native screen resource (commonly known as a peer). A lightweight component is one that "borrows" the screen resource of an ancestor  (which means it has no native resource of its own -- so it's "lighter")."

 Now many times you have heard "Don't mix lightweight and heavyweight". What will happen ? Alright, here is a small code : ............

[Read More]

Saturday Sep 13, 2008

Last week.. a competition code !

Last week, IEC celebrates OpenSource Mela. In code war, we got a programming contest in which the problem statement is :

5 \* 4 / 2 - 5 + 2 = 7 (evaluated from left to right). So, input format is:

5 4 2 5 2 7

and output is to put into a valid expression format(all possible format). So, if its 2 2 4, then 2+2=4 and 2\*2=4 is possible. I have written some code for this, which goes here :

package problemstatement1;

import java.util.List;

public class Main {

    String input = "2 0 2";
    String output = new String();  // 5 \* 4 / 2 - 5 + 2 = 7
    int resultVal = 0;
    String operatorseq = new String();
    int result = 0;
    int totalcounter = 0;
    boolean flag = true;
 
    // converting input into easy format
 
    String[] inputtoken = input.split(" ");
    int[] numberseq = new int[inputtoken.length - 1];
    int totaloperator = numberseq.length - 1;
 
    public void isValid() {
        if (inputtoken.length < 3) {
            System.out.println("Usages ... Input Parameter should be three or more ");
            System.exit(0);
        }
    }

    public void processInput() {
        try {
        resultVal = Integer.parseInt(inputtoken[inputtoken.length - 1]);
        } catch(NumberFormatException e) {
            System.out.println("Parsing error" + "  " + e);
            System.exit(0);
        }
        for (int i = 0; i < numberseq.length; i++) {
            try {
            numberseq[i] = Integer.parseInt(inputtoken[i]);
            } catch(NumberFormatException e) {
                System.out.println("Parsing error" + "  " + e);
                System.exit(0);
              }
        }
    }

    public void showInput() {
        for (int i = 0; i < numberseq.length; i++) {
            // System.out.println(numberseq[i]);
        }
    }

    public void getPermutation() {
        GenerateOperators gen = new GenerateOperators("+-\*/", totaloperator);
        List<String> v = gen.getVariations();
        System.out.println("Possible Solutions");
        for (int i = 0; i < v.size(); i++) {
            operatorseq = v.get(i);
            manupulate();
        }
    }

    public void manupulate() {
 
        result = 0;
 
        for (int i = 0; i < operatorseq.length(); i++) {
            if (i == 0) {
                if ((operatorseq.charAt(i)) == '+') {
                    result = result + (numberseq[i] + numberseq[i + 1]);
                }
                if ((operatorseq.charAt(i)) == '-') {
                    result = result + (numberseq[i] - numberseq[i + 1]);
                }
                if ((operatorseq.charAt(i)) == '\*') {
                    result = result + (numberseq[i] \* numberseq[i + 1]);
                }
                if ((operatorseq.charAt(i)) == '/') {
                    try {
                    result = result + (numberseq[i] / numberseq[i + 1]);
                    } catch(Exception e) {
                        flag = false;
                        // don't do anything
                    }
                }
            } else {
                if ((operatorseq.charAt(i)) == '+') {
                    result = result + numberseq[i + 1];
                }
                if ((operatorseq.charAt(i)) == '-') {
                    result = result - numberseq[i + 1];
                }
                if ((operatorseq.charAt(i)) == '\*') {
                    result = result \* numberseq[i + 1];
                }
                if ((operatorseq.charAt(i)) == '/') {
                    try {
                        result = result / numberseq[i + 1];
                    } catch(Exception e) {
                        flag = false;
                        // don't do anything
                    }
                }
            }
        }
        if (result == resultVal && flag == true) {
            totalcounter++;
            System.out.println("");
            for (int i = 0; i < numberseq.length - 1; i++) {
                System.out.print(numberseq[i] + "" + operatorseq.charAt(i));
            }
            System.out.print(numberseq[numberseq.length - 1]);
            System.out.print("= " + result);
        }
     }

    public void count() {
        if(totalcounter == 0 ) {
            System.out.println("NO EXPRESSION POSSIBLE");
            System.exit(0);
        }
        else {
            System.out.println("");
            System.out.println("Total Possible Solution :" + totalcounter);
        }
    }
    public static void main(String[] args) {
        Main main = new Main();
        main.isValid();
        main.processInput();
        main.showInput();
        main.getPermutation();
        main.count();
    }
}

and one more file which is : 

package problemstatement1;

import java.util.List;
import java.util.ArrayList;

public class GenerateOperators {

   private String a;
   private int n;

   public GenerateOperators(String a, int n) {
       this.a = a;
       this.n = n;
   }

   public List<String> getVariations() {
       int l = a.length();
       int permutations = (int) Math.pow(l, n);
       char[][] storeCombination = new char[permutations][n];
       for (int x = 0; x < n; x++) {
           int temp = (int) Math.pow(l, x);
           for (int p1 = 0; p1 < permutations;) {
               for (int al = 0; al < l; al++) {
                   for (int p2 = 0; p2 < temp; p2++) {
                       storeCombination[p1][x] = a.charAt(al);
                       p1++;
                   }
               }
           }
       }

       List<String> result = new ArrayList<String>();
       for (char[] permutation : storeCombination) {
           result.add(new String(permutation));
       }
       return result;

   }

   public static void main(String[] args) {
       GenerateOperators gen = new GenerateOperators("AAAMMBR", 7);
       List<String> v = gen.getVariations();
             for (int i=0;i<v.size();i++) {
           String s1 = v.get(i);
           System.out.println(s1);
       }

   }
}

Feel free to use this code :-).



Saturday Jun 14, 2008

Add JRE/ Run Old JRE - 6u10 :)

Yesterday, I have started making a presentation on J2SE 6u10. And I will be like delighted again seeing the new features of Plug-in which we called Next Generation Plug-in. Some of the common problems I used to face while doing my job :

Installation and Un-Installation of JRE's - This was a region of major concern. Most of the time I never install JDK or JRE, I love to untar or unzip it, so that it will not play with my registry. But the problem with those when I run Applet. Untar JDK/JRE not used to be a suitable candidate for applet because it takes only the installed JRE. But now, we can point our applet to the untared/unziped JRE and it will all work. Uncheck all the other JRE's which is not required, run the applet. It will all rock :)



I have clicked on the Find button and added the JRE 1.7. Now, I unchecked all the other JRE and ran my applet. It will run on JRE 1.7 (Remember IE6+ and FF3 or + only)

Here is the snap shot of old Java Control Panel:





And now we will also get rid of running applet on older installed JDK/JRE. Initially it used to be if I have to check one applet on 2 different JRE, I have to install and uninstall it again. But now it all work with one checkBox option :)

Monday Apr 28, 2008

Multi-threading in Java !

Today I finished my presentation of Multi-threading in Java. This presentation not speaks anything in great detail. I tried to make it as basic as I can. I have tried to cover the common problems of locking and synchronization. Also, some of the aspects of JDK1.5.

Here is the presentation. Your comments are most welcome :-) 

 

Friday Apr 25, 2008

Online Java Output !

Again one idea, implementation of which I am not able to find on internet. Most of the time for testing purpose we need to run small piece of codes basically the non-UI code. And we do it a lot when we prepare for certification exams like SCJP or SCJA. Some small tricky questions ! Not only this most of the time JDK version matters because one can't run generics code with JDK 1.4 backwards.

Why not to make a small web based tool, which takes the java file as an input from user and give option to user to select which JDK version is required probably by radio button(radio button is a nice name, its like radio in which you can select only one station at a time, I wonder why not television button :-) ) and we show the output of that java file on a JSP page or we can also write it somewhere on a file, as user demand, after all user is God :-).  For UI code, it will simple return a message like its a UI code and cant be displayed.

So, implementation is little like JFileChooser for selecting input file and for writing output file. Radio buttons for selection of JDK and thats it ! Rest my web server will take load of all JDK version and it will be the duty of code to run the java file on the appropriate JDK version.

Please give your useful comment on idea and also if there is anything exist like this. I would love to use it rather than writing :-).

Thursday Nov 29, 2007

FOSS.IN - OpenJDK

So,

Sun is ready for the big event FOSS.IN going to happen in Bangalore, India. Our J2SE team is going to give two demo. One is about OpenJDK and other is all about JavaFX, the new cutting edge technology. Both underwent through the dry run before the actual event occurs. We are ready with OpenJDK build on Win32 and off course Solaris. Without too much of the change Solaris scripts files can be mapped on Linux as well. If anyone of the blog reader(s) are interested in making build of OpenJDK, the please leave a comment or just drop me a mail. Thanks for Igor Nekrestyanov and Sandeep Konchady to provide lot of help.

We have made the build from command line and from Netbeans 6 both. Still lot of things to do :-)



Sunday Jul 01, 2007

xjc.exe

Today I was looking some options in jdk/bin and suddenly I came across
a file name xjc.exe in JDK6. Quickly I checked the existence of this file in JDK 5.0 and JDK
1.4.2. File was not present. Goggling didn't provided too much of help
and I tried with myself :)

I just copied an XSD file from somewhere:

FileName: unknowntest.xsd

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
 <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="pop" type="xs:decimal"/>
   </xs:sequence>
 </xs:complexType>
</xs:element>
</xs:schema>

 and passed this XSD file into xjc option:
Usage: xjc [-options ...] ... [-b ] ...

> xjc unknowntest.xsd

parsing a schema...
compiling a schema...
generated\\Country.java
generated\\ObjectFactory.java

Ah I got 2 Java files - Country.java and ObjectFactory.java
Look at these 2 files ( I am removing the documentation part of it, to make it small)

package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"pop"
})
@XmlRootElement(name = "country")
public class Country {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected BigDecimal pop;

/\*\*
\* Gets the value of the name property.
\*
\* @return
\* possible object is
\* {@link String }
\*
\*/
public String getName() {
return name;
}

/\*\*
\* Sets the value of the name property.
\*
\* @param value
\* allowed object is
\* {@link String }
\*
\*/
public void setName(String value) {
this.name = value;
}

/\*\*
\* Gets the value of the pop property.
\*
\* @return
\* possible object is
\* {@link BigDecimal }
\*
\*/
public BigDecimal getPop() {
return pop;
}

/\*\*
\* Sets the value of the pop property.
\*
\* @param value
\* allowed object is
\* {@link BigDecimal }
\*
\*/
public void setPop(BigDecimal value) {
this.pop = value;
}

}

and
objectFactory don't have too much in it. Just creating a Country class
instance. Oh so it is something cool, its making all the getter and
setter method for me, what else I need :).

Remember, such type
of methods are too much in demand when you are writing any jsp page.
So, if you are clear with the data type, just make an xsd file and xjc
will convert in into java code.

I still doubt what xjc refers for ... may be xml to java converter :)

 

About

Hi, I am Vaibhav Choudhary working in Sun. This blog is all about simple concept of Java and JavaFX.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today