Wednesday 22 August 2012


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ErrorDemo
{
    class ErrorDemo
    {
        public static void Main(String[] args)
        {
            int a, b, ans;
            int[] no = new int[5];
            try
            {
                a = int.Parse(args[0]);
                b = 10;
                ans = b / a;
                Console.WriteLine("ans=" + ans);
                no[4] = ans;
                Console.WriteLine(no[3]);
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Division by 0");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.WriteLine("After try catch block");
            Console.ReadKey();      
        }
    }
}

Friday 17 August 2012

TreeD.java


import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.applet.*;
import java.awt.*;
/*<applet code="TreeD" width=600 height=400>
</applet> */
public class TreeD extends JApplet implements TreeSelectionListener
{
JTree tr;
JLabel l1;
DefaultMutableTreeNode top,a,a1,a2,b,b1,b2,b3;
public void init()
{
top=new DefaultMutableTreeNode("Options");
a=new DefaultMutableTreeNode("A");
top.add(a);
a1=new DefaultMutableTreeNode("A1");
a.add(a1);
a2=new DefaultMutableTreeNode("A2");
a.add(a2);
b=new DefaultMutableTreeNode("B");
top.add(b);
b1=new DefaultMutableTreeNode("B1");
b.add(b1);
b2=new DefaultMutableTreeNode("B2");
b.add(b2);
b3=new DefaultMutableTreeNode("B3");
b.add(b3);
tr=new JTree(top);
JScrollPane s=new JScrollPane(tr);
add(s);
l1=new JLabel();
add(l1,BorderLayout.SOUTH);
tr.addTreeSelectionListener(this);
}
public void valueChanged(TreeSelectionEvent tse)
{
l1.setText("Selected path is" +tse.getPath());
}
}



//Use appletviewer TreeD.java to execute the program

Thursday 16 August 2012

JAVA download link!!

The following link can be used to download java development kit for your PC!!
http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.html

To download eclipse for java the link is:
http://www.eclipse.org/downloads/

!!Enjoy programming!!

Animal.java

//U need to copy these images to a folder where you are going to save this program!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.*;
/*<applet code="Animal" width=1000 height=700>
</applet>*/
public class Animal extends JApplet implements ActionListener,ListSelectionListener
{
JList li1;
JLabel l1;
JScrollPane s1;
JButton b1;
String [] Animals={"Dog","Elephant","Lion","Rat","Tiger"};
ImageIcon i1=new ImageIcon("Dog.png");
ImageIcon i2=new ImageIcon("Elephant.png");
ImageIcon i3=new ImageIcon("Lion.png");
ImageIcon i4=new ImageIcon("Rat.png");
ImageIcon i5=new ImageIcon("Tiger.png");
public void init()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
li1=new JList(Animals);
li1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
s1=new JScrollPane(li1);
l1=new JLabel();
b1=new JButton("Clear");
cp.add(s1);
cp.add(l1);
cp.add(b1);
li1.addListSelectionListener(this);
b1.addActionListener(this);
}
public void valueChanged(ListSelectionEvent le)
{
int i;
i=li1.getSelectedIndex();
if(i==0)
{
l1.setIcon(i1);
}
else if(i==1)
{
l1.setIcon(i2);
}
else if(i==2)
{
l1.setIcon(i3);
}
else if(i==3)
{
l1.setIcon(i4);
}
else if(i==4)
{
l1.setIcon(i5);
}
}
public void actionPerformed(ActionEvent ae)
{
l1.setVisible(false);
li1.clearSelection();
}
}