23 June, 2012

Find How Many Days in given Month


import java.util.*;
public class Fallswitchyear
{
public static void main(String[] args)
{
Scanner s=new Scanner (System.in);
System.out.printf("Enter Month no. -: ");
int n=s.nextInt();
if(n<1||n>12)
{
System.out.printf("Invalid Month");
}
switch(n)
{
case 4:
case 6:
case 9:
case 11:    System.out.println("Month have 30 Days");
    break;
case 2:    System.out.printf("Enter Year -: ");
    int y=s.nextInt();
    if((y%400==0)||(y%4==0 && y%100!=0))
    System.out.println("Month have 29 Days");
    else
    System.out.println("Month have 28 Days");
    break;
default :    System.out.println("Month have 31 Days");
    break;
}
}
}

22 June, 2012

Draw Circle in java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle extends JFrame
{
Dimension d;

public Circle()
{
setSize(400,400);
setTitle("Cirlce");
Oval o=new Oval();
add(o);
o.setBackground(Color.YELLOW);
}

public static void main(String[] args)
{
JFrame f=new Circle();
f.setVisible(true);
}

public class Oval extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
d=getSize();
System.out.println(d);
System.out.println(d.width/8);
System.out.println(d.height/8);

if(d.width>d.height)
g.fillOval(d.width/3,d.height/4,d.height/2,d.height/2);
else
g.fillOval(d.width/3,d.height/3,d.width/2,d.width/2);
}
}

}

Create Array Length & Element on Run Time

Create Array Length & Element on Run Time

public class Array
{
public static void main (String[] args)
{
System.out.println("Enter the Array size -: ");
int limit = Integer.parseInt(System.console().readLine());
int [] a = new int [limit];
for(int i=0;i<=a.length-1;i++)
a[i] = Integer.parseInt(System.console().readLine());
for(int i=0;i<=a.length-1;i++)
System.out.printf(" %d ,",a[i]);
System.out.println();
}
}

Creating a jar File of Java Program in Command Prompt

Creating a jar File of Java Program in Command Prompt


Start Command Prompt.
     Presss win key + R
     Write cmd then click OK.

Navigate to the folder that holds your class files:
     e.g -    C:\>cd myjavapro




 Compile your class(es):
       C:\myjavapro> javac *.java

 Create a manifest file:
    C:\myjavapro> echo Main-Class: mypro >manifest.txt

Create a jar file:
    C:\myjavapro> jar cvfm mypro.jar manifest.txt *.class

Test your jar:
    C:\myjavapro> mypro.jar

17 June, 2012

Find The Square Root



import  java.lang.Math;
class Squareroot
{
public static void main(String args[])
   {
      double x=5;
      double y;
      y=Math.sqrt(x);
      System.out.println("y= "  +  y);
 }
}

16 June, 2012

Count Words





import java.util.*;
public class Strlne
{
public static void main (String[] args)
{
Scanner s = new Scanner (System.in);
String strs;
int count=0;

System.out.println("Enter the Sentance & Press count Ctrl+z to count Word");

while(s.hasNext())
{
strs = s.next();
count = count+1;
System.out.printf("%s\n",strs);
}
System.out.printf("Total Lines are %d\n",count);
}
}

15 June, 2012

Leap Year



import java.util.*;
public class Leapyear
{
public static void main (String[] args)
{
Scanner s = new Scanner (System.in);
System.out.printf("Enter Year - :");
int year=s.nextInt();
if ((year%400==0)||((year%4==0) && (year%100!=0)))
System.out.printf("Year %d is Leap year",year);
else
System.out.printf("Year %d is not Leap year",year);
}
}

Read First Line of Any File

 Read First Line of Any File

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class FileOrganizer extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public static final int NUMBER_OF_CHAR = 30;
private JTextField fileNameField;
private JTextField firstLineField;
//******************************************************************
public FileOrganizer()
    {
        setSize(WIDTH, HEIGHT);
        //WindowDestroyer listener = new WindowDestroyer();
       // addWindowListener(listener);
        Container contentPane = getContentPane();
        contentPane.setLayout(new FlowLayout());
        JButton showButton = new JButton("Show first line");
        showButton.addActionListener(this);
        contentPane.add(showButton);
        JButton removeButton = new JButton("Remove file");
        removeButton.addActionListener(this);
        contentPane.add(removeButton);
        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        contentPane.add(resetButton);
        fileNameField = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(fileNameField);
        fileNameField.setText("Enter file name");
        firstLineField = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(firstLineField);
}
//******************************************************************
public void actionPerformed(ActionEvent e)
{
    String actionCommand = e.getActionCommand();
if (actionCommand.equals("Show first line"))
        showFirstLine();
else if (actionCommand.equals("Remove file"))
        removeFile();
else if (actionCommand.equals("Reset"))
        resetFields();
else
        firstLineField.setText("Unexpected error.");
}
//******************************************************************
private void showFirstLine()
{
    Scanner fileInput = null;
    String fileName = fileNameField.getText();
    File fileObject = new File(fileName);
if (!fileObject.exists())
        firstLineField.setText("No such file");
else if (!fileObject.canRead())
        firstLineField.setText("That file is not readable.");
else
    {
try
        {
            fileInput = new Scanner(fileObject);
        }
catch(FileNotFoundException e)
        {
            firstLineField.setText("Error opening the file " + fileName);
        }
        String firstLine = fileInput.nextLine();
        firstLineField.setText(firstLine);
        fileInput.close();
    }
}
//******************************************************************
private void resetFields()
{
    fileNameField.setText("");
    firstLineField.setText("");
}
//******************************************************************
private void removeFile()
{
 Scanner fileInput = null;
 String firstLine;
 String fileName = fileNameField.getText();
 File fileObject = new File(fileName);
  if (!fileObject.exists())
  firstLineField.setText("No such file");
  else if (!fileObject.canWrite())
  firstLineField.setText("Permission denied.");
  else
 {
   if (fileObject.delete())
   firstLineField.setText("File deleted.");
   else
   firstLineField.setText("Could not delete file.");
 }
}
//******************************************************************
public static void main(String[] args)
{
 FileOrganizer gui = new FileOrganizer();
 gui.setVisible(true);
}
//******************************************************************
}



Basic Arthimatic Operation



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ArthimaticOpration extends JFrame implements ActionListener
{
JTextField t1,t2,t3,t4,t5,t6 ;
public ArthimaticOpration()
{
setTitle("ArthimaticOpration For Add 2 Number");
setSize(290,240);
setLocation(400,250);
setLayout(new FlowLayout());

t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(15);
t4 = new JTextField(15);
t5 = new JTextField(15);
t6 = new JTextField(15);

JButton b = new JButton("ADD");
JButton b1 = new JButton("SUB");
JButton b2 = new JButton("MUL");
JButton b3 = new JButton("DIV");
JButton b4 = new JButton("OK");

add(t1);
add(t2);

add(b);
add(t3);

add(b1);
add(t4);

add(b2);
add(t5);

add(b3);
add(t6);

add(b4);

b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}

public void actionPerformed (ActionEvent e)
{
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());

int sum =a+b;
t3.setText(" " +sum);

int sub =a-b;
t4.setText(" " +sub);

double mul =a*b;
t5.setText(" " +mul);

int div =a/b;
int rem=a%b;
int r=(rem*10000)/b;
t6.setText(" " +div+"."+r);
}

public static void main (String[] args)
{
ArthimaticOpration c = new ArthimaticOpration();
c.setVisible(true);
}

}




Convert decimal to binary

Decimal to Binary



import java.util.*;
public class NumberSystem
{
    public static void main(String[] args)
    {
        Scanner s = new Scanner (System.in);
        System.out.printf("\nEnter The Value in Decimal No.-: ");
        int n = s.nextInt();
        int c = 2;
        String str = " ";

        for ( ; n>0; )
        {
            int r = n%c;
            if (r<10)
            str = " "+r+str;
            n = n/c;
        }

    System.out.printf("\nThe Converted Number is  %s\n",str);
    }
}

Creating Manu Bar in Java

Creating Manu Bar in Java


import javax.swing.*;
import java.awt.*;
import java.awt.Event;
public class MenuTest extends JFrame
{

public MenuTest()
{
Toolkit tk=Toolkit.getDefaultToolkit();

setTitle("Menu Bar & Menu Item");
setSize(500,350);
setLocation(200,100);
Image titleicon=tk.getImage("Notepad.png");
setIconImage(titleicon);
//setLayout(new FlowLayout());
JTextArea jta=new JTextArea();
add(jta);

JMenuBar mb =new JMenuBar();
setJMenuBar(mb);

//*****************************************
JMenu filemenu = new JMenu("File");
filemenu.setMnemonic('F');
mb.add(filemenu);

JMenuItem newmenu = new JMenuItem("New");
filemenu.add(newmenu);
//ImageIcon newmenuicon=new ImageIcon("Notepad.png");
//newmenu.setIcon(newmenuicon);
newmenu.setMnemonic('N');
//newmenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));

JMenuItem openmenu = new JMenuItem("Open");
filemenu.add(openmenu);
openmenu.setMnemonic('O');

JMenuItem exitmenu = new JMenuItem("Exit");
filemenu.add(exitmenu);
exitmenu.setMnemonic('X');

//*****************************************
JMenu editmenu = new JMenu("Edit");
editmenu.setMnemonic('E');
mb.add(editmenu);

JMenuItem cutmenu = new JMenuItem("Cut");
editmenu.add(cutmenu);

JMenuItem copymenu = new JMenuItem("Copy");
editmenu.add(copymenu);

JMenuItem pastemenu = new JMenuItem("Paste");
editmenu.add(pastemenu);

//*****************************************
JMenu helpmenu = new JMenu("Help");
helpmenu.setMnemonic('H');
mb.add(helpmenu);

JMenuItem aboutmenu = new JMenuItem("About");
helpmenu.add(aboutmenu);
aboutmenu.setMnemonic('A');

//*****************************************
}

public static void main(String[] args)
{
MenuTest f=new MenuTest();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

}