Sunday, 30 March 2014

Java program for character type input using scanner class

import java.io.*;
import java.util.*;
class usr_scanner
{
    public static void main(String args[])
    {
        try
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("Enter any character");
            char s=sc.next().charAt(0);
            System.out.println("You enter character is - "+s);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Tuesday, 18 March 2014

Java Program for Rename File

// rename file

import java.io.File;

class RenameFile
{
    public static void main(String args[])
    {
        File f=new File("c:\\kunal.txt");
        if(f.exists())
        {
            f.renameTo(new File("c:\\amol.txt"));
        }
    }
}

Java Program for Create New File

//wap that will create empty text file


//import java.iostream.*;
import java.io.*;
import java.io.File;
public class NewFile
{
    public static void main(String args[])
    {
        File file=new File("c:\\anant.txt");
        boolean Created=false;
       
        try
        {
            Created=file.createNewFile();
        }
        catch(IOException e)
        {
            System.out.println("error"+e);
            System.out.println("file is created"+file.getPath()+Created);
        }
    }
}



Java Program for LinkedList Demonstration

// wap to insert element and delete element and set another
//element to another position

import java.util.*;
class LinkedListDemo
{
    public static void main(String args[])
    {
        LinkedList l1=new LinkedList();
       
        l1.add(11);
        l1.add("A");
        l1.add("B");
        l1.add("C");
        l1.add("D");
        l1.add("E");
        l1.addLast("z");
        l1.addFirst("A1");
        l1.add(1,"A2");
       
        System.out.println("original content of l1:"+l1);
       
        l1.remove("E");
        l1.remove(2);
           
        System.out.println("content of l1 after deletion :"+l1);
       
        l1.removeFirst();
        l1.removeLast();
       
        //Object val=l1.get(2);
        l1.set(2,"W");
        System.out.println("l1 after change:"+l1);
    }
}


Java Program for Stack Demonstration

//stack demo
import java.util.*;

class StackDemo
{
    static void StackPush(Stack St,int a)
    {
        St.push(new Integer(a));
        System.out.println("push +a");
        System.out.println("stack:"+St);
    }
    static void Showpop(Stack St)
    {
       
        System.out.println("pop:-");
        Integer a=(Integer)St.pop();
        System.out.println(a);
    }
    public static void main(String args[])
    {
        Stack St=new Stack();
        System.out.println("Stack"+St);
        StackPush(St,42);
        StackPush(St,52);
        StackPush(St,62);
        StackPush(St,72);
        Showpop(St);
        Showpop(St);
        Showpop(St);
        Showpop(St);
   
        try
        {
            Showpop(St);
        }
        catch(EmptyStackException e)
        {
            System.out.println("stack underflow"+e);
        }
    }
}










       

Java Program for Delete File

//delete file

import java.io.File;

class DeleteFile
{
    public static void main(String args[])
    {
        File f=new File("c:\\ram.txt");
        if(f.exists())
        {
            f.delete();
        }
    }
}

Saturday, 18 January 2014

Simple Text editor in java


/**
 * @(#)Text_Editor.java
 *
 *
 * @author anant
 * @version 1.00 2013/9/17
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Text_Editor
{
    public static void main(String []args)
    {
        myframe m=new myframe();
        m.setTitle("Text Editor");
        m.setSize(500,500);
        m.setVisible(true);
        m.setLocation(500,100);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ActionListener,ItemListener
    {
        JTextArea txta_show;
        JComboBox cmb_font;
        JLabel lbl_size;
        JTextField txt_size;
        JCheckBox cb_bold,cb_italic;
        int i=0;
        String font;
        JButton btn_ok;
        mypanel()
        {
            setLayout(null);
           
            txta_show=new JTextArea();
            JScrollPane pane=new JScrollPane(txta_show);
            pane.setBounds(50,10,400,300);
            add(pane);
           
            cmb_font=new JComboBox();           
            cmb_font.addItem("Curlz MT");
            cmb_font.addItem("Forte");
            cmb_font.addItem("Freestyle Script");
            cmb_font.addItem("French Script MT");
            cmb_font.addItem("Gigi");
            cmb_font.addItem("Harrington");
            cmb_font.addItem("Informal Roman");
            cmb_font.addItem("Jokerman");
            cmb_font.setBounds(50,350,100,30);
            cmb_font.addItemListener(this);
            add(cmb_font);
           
            lbl_size=new JLabel("Size");
            lbl_size.setBounds(175,350,75,30);
            add(lbl_size);
           
            txt_size=new JTextField();
            txt_size.setBounds(210,350,50,30);
            add(txt_size);   
               
            cb_bold=new JCheckBox("Bold");
            cb_bold.setBounds(275,350,75,30);
            cb_bold.addActionListener(this);
            add(cb_bold);
           
            cb_italic=new JCheckBox("Italic");
            cb_italic.setBounds(350,350,100,30);
            cb_italic.addActionListener(this);
            add(cb_italic);   
               
            btn_ok=new JButton("Ok");
            btn_ok.setBounds(50,400,100,30);
            btn_ok.addActionListener(this);
            add(btn_ok);           
        }
        public void itemStateChanged(ItemEvent ie)
        {
            if(ie.getSource()==cmb_font)
            {
                i=cmb_font.getSelectedIndex();
                if(i==0)
                    font="Curlz MT";
                else if(i==1)
                    font="Forte";
                else if(i==2)
                    font="Freestyle Script";
                else if(i==3)
                    font="French Script MT";
                else if(i==4)
                    font="Gigi";
                else if(i==5)
                    font="Harrington";
                else if(i==6)
                    font="Informal Roman";
                else if(i==7)
                    font="Jokerman";
            }
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==btn_ok)
            {
                int size=Integer.parseInt(txt_size.getText());
                if(cb_bold.isSelected()&&cb_italic.isSelected())
                    txta_show.setFont(new Font(font,Font.BOLD+Font.ITALIC,size));
                else if(cb_bold.isSelected())
                    txta_show.setFont(new Font(font,Font.BOLD,size));
                else if(cb_italic.isSelected())
                    txta_show.setFont(new Font(font,Font.ITALIC,size));
                else
                    txta_show.setFont(new Font(font,Font.PLAIN,size));                   
            }
        }
    }
}