Practical Numbers in Range

Last week i was breaking my head to get the solution for this Practical Number. In beginning it was taught to even understand what it is. Later i found the meaning of Practical Number from Wikipedia. My problem is to print list of practical number within a range. After some workout I got the solution to be like this,


import java.util.*;
public class PracticalNumbers {
 public int[] getPracticalNumbers(int from, int to) {
 ArrayList<Integer> result = new ArrayList<Integer>();
 ArrayList<Integer> divisor = new ArrayList<Integer>();
 for(int i=from;i<=to;i++)
 {
 if(check4Practical(i))
 {
 result.add(i);
 }
 }
 int []res=new int[result.size()];
 for(int i=0;i<result.size();i++)
 {
 res[i]=result.get(i).intValue();
 }
 if(res.length>0)
 {
 return res;
 }
 return new int[]{};
 }
 boolean check4Practical(Integer num)
 {
 ArrayList<Integer> divisors = new ArrayList<Integer>();

 for(int i=1;i<num;i++){
 if(num%i == 0)
 {
 divisors.add(i);
 }
 }
 int[] divisors_arr = new int[divisors.size()];
 for(int i =0;i<divisors_arr.length;i++)
 {
 divisors_arr[i] = divisors.get(i).intValue();
 }
 for(int i = num-1;i>0;i--)
 {
 int temp = i;
 for(int j= divisors_arr.length-1;j>=0;j--)
 {
 if(temp>0)
 temp -= divisors_arr[j];
 if(temp <0)
 temp+= divisors_arr[j];
 if(temp == 0)
 break;
 }
 if(temp!= 0 )
 return false;
 }
 return true;
 }

 public static void main(String[] arg) {
 PracticalNumbers pn = new PracticalNumbers();
 //test case
 int[] res = pn.getPracticalNumbers(1, 200);
 for (int i = 0; i < res.length; i++)
 System.out.print(res[i] + " ");
 }
}

Executing System .exe File of Windows from Core Java

One of my friend was trying to open system calculator and notepad from core Java coding, in windows XP. As he was not getting the solution, after some browsing I found the solution which works very well with xp as well as notepad. I found the trick to be very useful. If we are buliding application for windows, why should we crack our head to build application like calculator and notepad for our system?, which is already very well there in windows.

We have an exec method in Runtime class, which can be used to execute the windows executable file.

For Notepad:

Runtime.getRuntime().exec("C:/WINDOWS/system32/notepad.exe");

For Calculator:

Runtime.getRuntime().exec("C:/WINDOWS/system32/calc.exe");

These command should be in try block

try {
     Runtime.getRuntime().exec("C:/WINDOWS/system32/calc.exe");
 } catch (Exception ex) {
     ex.printStackTrace();
 }

I hope in some kind of project it may help you.

Installing MySQL in Ubuntu 9.10

As I begin to import my project into Ubuntu 9.10, I had to install MySQL too. Just at that time I realise, why don’t I publish this instruction for you all. At least, it will help some body. I hope you will be among that somebody.

For Installation and use MySQL you have to give some instruction in terminal:

Step 1:

sudo apt-get install mysql-server

This will install the latest MySQL-server into your machine.

Step 2:

sudo /etc/init.d/mysql start

Which will start the MySQL server.

Step 3:

mysql -u root -p

This instruction will ask you the MySql server that you have entered during installation.

Step 4:

create database db_test

This instruction will create a new database “db_test”

Step 5:

use db_test

which will let you inside the database.

Now use MySQL Query to work with it n Enjoy.

Installing Java in Ubuntu 9.10

As I did upgrade my operating system to Ubuntu 9.10, i found that the installation of Java became little easer. Previously in 9.04, I had to edit sources.list but this time directly update of the repository works.

For installation, Just i have to update my repository by

$ sudo apt-get update

Then I mapped the dependencies

$ sudo apt-get -f install

And just we need to install Java with following command

$ sudo apt-get install sun-java6-jre sun-java6-jdk sun-java6-plugin

(And to accept the configuration window, press TAB key to move the control)

Printing Report in Core Java

It was before one week, when I was searching and trying all possibilities to get report in Core Java, for my college project. I was thinking “Why Java, for each control you are the best but why not for printing report”.  But after long search I found a post by Sergey Groznyh in Java Webblog, where he had given the idea and example to generate print preview of swing Text Component. This really works for me after some Changes.I heartily  thanks Sergey Groznyh for his post. His post is in http://weblogs.java.net/blog/2007/09/05/generating-print-preview-swing-text-components.

In this post you will be able to preview a HTML page, where as in my project I had to preview the JTable content. For solution I add the JTable contents in a two dimensional array and using loop I add the content into a string and set that string to the editor pan, which really works.

Here us the code with changes:

/**
 *
 * @author prashant
 */
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.net.URL;

public class Form_JobRPT extends JInternalFrame
{
    JPanel panel_Content = new JPanel();
    static JButton btn_CompJobPrev;
    static JEditorPane ep = new JEditorPane();

    //MDIDesktopManager desktop;
    public Form_JobRPT()
    {

        setSize(1000,600);
        setTitle("Report Preview");
        setMaximizable(true);
        setIconifiable(true);
        setClosable(true);
        setResizable(true);

        final PrintPreview pp = new PrintPreview();

        ep.setEditable(false);
        ep.setContentType("text/html");

        String str="";
        str+="<html><head></head><body><table border=0 color='gray' cellpadding=5 cellspacing=0>";

        str+="<tr><th><font color='#0000FF'><u>Code</u></font></th>" +
                "<th><font color='#0000FF'><u>Company Name</u></font></th>" +
                "<th><font color='#0000FF'><u>Designation</u></font></th>" +
                "<th><font color='#0000FF'><u>Opening Date</u></font></th>" +
                "<th><font color='#0000FF'><u>Closing Date</u></font></th>" +
                "<th><font color='#0000FF'><u>Ann. Salary</u></font></th></tr>";
        for(int i=0;i<Form_JobTbl.dataRow;i++)
         {
            str+="<tr>";
            for(int j=0;j<Form_JobTbl.dataCol;j++)
            {
                str+="<td>";
                str+=Form_JobTbl.mydata[i][j]+"<b>";
                str+="</td>";
            }
            str+="</tr>";
         }
        str+="</table></body>";

        ep.setText(str);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel_Content);
        panel_Content.setLayout(null);
        panel_Content.setBounds(0, 0, getBounds().width, getBounds().height);
        btn_CompJobPrev=new JButton(new ImageIcon("icons/print_preview.png"));
        btn_CompJobPrev.setToolTipText("Print Preview");
        btn_CompJobPrev.setBounds(10, 30, 40, 40);
        panel_Content.add(btn_CompJobPrev);
        btn_CompJobPrev.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                pp.showPreview(ep);
            }
        });
        int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
        JScrollPane jsp = new JScrollPane(ep,v,h);
        jsp.setBounds(10, 100, getBounds().width-5, getBounds().height-150);
        panel_Content.add(jsp);
    }
}

/**

*

* @author prashant

*/

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.net.URL;

public class Form_JobRPT extends JInternalFrame

{

JPanel panel_Content = new JPanel();

static JButton btn_CompJobPrev;

static JEditorPane ep = new JEditorPane();

//MDIDesktopManager desktop;

public Form_JobRPT()

{

setSize(1000,600);

setTitle(“Report Preview”);

setMaximizable(true);

setIconifiable(true);

setClosable(true);

setResizable(true);

final PrintPreview pp = new PrintPreview();

ep.setEditable(false);

ep.setContentType(“text/html”);

String str=”";

str+=”<html><head></head><body><table border=0 color=’gray’ cellpadding=5 cellspacing=0>”;

str+=”<tr><th><font color=’#0000FF’><u>Code</u></font></th>” +

“<th><font color=’#0000FF’><u>Company Name</u></font></th>” +

“<th><font color=’#0000FF’><u>Designation</u></font></th>” +

“<th><font color=’#0000FF’><u>Opening Date</u></font></th>” +

“<th><font color=’#0000FF’><u>Closing Date</u></font></th>” +

“<th><font color=’#0000FF’><u>Ann. Salary</u></font></th></tr>”;

for(int i=0;i<Form_JobTbl.dataRow;i++)

{

str+=”<tr>”;

for(int j=0;j<Form_JobTbl.dataCol;j++)

{

str+=”<td>”;

str+=Form_JobTbl.mydata[i][j]+”<b>”;

str+=”</td>”;

}

str+=”</tr>”;

}

str+=”</table></body>”;

ep.setText(str);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(panel_Content);

panel_Content.setLayout(null);

panel_Content.setBounds(0, 0, getBounds().width, getBounds().height);

btn_CompJobPrev=new JButton(new ImageIcon(“icons/print_preview.png”));

btn_CompJobPrev.setToolTipText(“Print Preview”);

btn_CompJobPrev.setBounds(10, 30, 40, 40);

panel_Content.add(btn_CompJobPrev);

btn_CompJobPrev.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

pp.showPreview(ep);

}

});

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;

int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;

JScrollPane jsp = new JScrollPane(ep,v,h);

jsp.setBounds(10, 100, getBounds().width-5, getBounds().height-150);

panel_Content.add(jsp);

}

}

VLC player installation in Ubuntu 9.10

As we all knows, VLC media player is the best media player for Ubuntu, which support almost media format. The installation method of VLC media player in new Ubuntu 9.10, I found it to be little changed. Now we have to install the trusted key and the after repository, we do have to install it. Here are the certain steps that work in my system:

Step 1:

$ sudo apt-key adv –recv-keys –keyserver keyserver.ubuntu.com 7613768D

This will install the trusted key for VLC.

Step 2:

$ sudo gedit /etc/apt/sources.list

Opens the repository file.

Step 3:

Add these two repository links and save in it.

deb http://ppa.launchpad.net/c-korn/vlc/ubuntu jaunty main
deb-src http://ppa.launchpad.net/c-korn/vlc/ubuntu jaunty main

Step 4:

$ sudo apt-get update

to update the repository.

Step 5:

$ sudo apt-get install vlc

To installation the VLC player…

Customizing the Number Formats

While working in my college project, I required the selected date from database to be display in a way that day and month should be in two digits (i.e. 01-05-1995). For that I had found some solution among which I like the two methods much.

The first one is constructing the pattern, where we use the formatting property of DecimalFormat with a pattern string. Then We send the pattern in DecimalFormat constructor and a double value in format() function.

Here is a small example which had fulfilled my requirements.

public class GetNumFormat {

public static void main(String[] args)

{

int date[]={1,3,1995};

java.text.DecimalFormat nft = new java.text.DecimalFormat(“#00″);

//nft.setDecimalSeparatorAlwaysShown(false)

System.out.println(nft.format(date[0])+”-”+nft.format(date[1])+”-”+date[2]);

int x = 1;

System.out.println(String.format(“%02d”, date[0])+”-”+

String.format(“%02d”, date[1])+”-”+

String.format(“%02d”, date[2]));

}

}

And in second method, We can change the format using C like formatting using String function format(), which i have implemented in second print statement.

To get more detailed knowledge in  pattern construction I would like you to follow sun tutorial in http://java.sun.com/docs/books/tutorial/i18n/format/decimalFormat.html

Introduction To Nanotechnology

Till day before yesterday, I had only heard the name of nanotechnology. As one of my friend tell some introduction of it, I was very much curious to know about this technology. So last few hours I spend for searching some information about it, and finally I got some idea as an introduction. After gaining some idea about the small but large technology called nanotechnology, I design a presentation so that it might be helpful for those who want to get some introduction knowledge about it.

Click Here To Download.

Rules for Variable Argument List (var-args)

As Java alows to create methods that can take variable no of arguments, we do have some rules to be followed such as:

  • Var-arg Type: While declaring var-arg parameters, we shoud specify the argument type which can be either primitive type or object type.

void example1(int… x){}

void example2(ClassA… obj){}

  • Basic Syntax: For the declaration of var-arg parameter, we will provide ellipsis(…) followed by type then a space and then a name of thee array that will holds the received parameter.

void example(int… x){}

  • Other Parameter : We can even provide other parameters in a method that is not var-arg.

void example(int x, char y,char… z){}

  • Var-arg limits: The var-arg mustbe the last parameter in the method’s signature, and we can have only one var-arg in a method.

void example(int x, char y,char… z){}

Developer should be thankful to Canonical Launchpad Team

Great job has been done by Canonical Launchpad team for making Launchpad open source. We should congrats as well as thankful to them, for making an environment for achievement.

Detail information we can read here:

http://blog.canonical.com/?p=192
http://www.ubuntu.com/news/canonical-open-sources-launchpad