14 September, 2012

Palindrome

Checks whether a given String is a Palindrome or not

 

import java.util.*;
public class palindrome
{
public static void main(String a[])
{
System.out.println("Enter any string");
Scanner s=new Scanner(System.in);
String str=s.next();

for(int i=0,j=str.length()-1;i<=j;i++,j--)
{
if(str.charAt(i)==str.charAt(j));
else
{
System.out.println("Not Palindrome : "+str);
System.exit(1);
}
}
System.out.println("Palindrome : "+str);
}
}

10 September, 2012

Sort the Name List

import java.util.*;
class Sortst
{
public static void main(String a[])
{
System.out.println("How many string u want to enter");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String st[]=new String[n+1];
for(int i=0;i<n;i++)
{
System.out.println("Enter string "+(i+1));
st[i]=s.next();
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(st[i].compareTo(st[j])>0)
{
String t=st[i];
st[i]=st[j];
st[j]=t;
}
}
}
System.out.println("Strings in order are :\n\n");
for(int i=0;i<n;i++)
System.out.println(st[i]);
}
}

07 September, 2012

Bubble Sort

import java.util.*;
class bubblesort
{

public static void main(String[] args)
{
int []a = {10,2,13,40,25,6,27,38,9} ;
bubblesort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void bubblesort(int[] a)
{
for(int i=0;i<=a.length-2;i++)
{
    for(int j=0;j<=a.length-2-i;j++)
        if(a[j+1]<a[j])
        {
            int t=a[j];
            a[j]=a[j+1];
            a[j+1]=t;
        }
}
}

}

Insertion Sort

import java.util.*;
class insertionsort
{

public static void main(String[] args)
{
int []a = {5,25,12,28,9,12,15,13,10,7} ;
insertionsort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void insertionsort(int[] a)
{
 for(int j=1;j<a.length;j++)
 {
  int key=a[j];
  int i;
  for(i=j-1;i>=0&&a[i]>key;i--)
     a[i+1]=a[i];
  a[i+1]=key;
 }
}

}

Selection Sort

import java.util.*;
class selectionsort
{

public static void main(String[] args)
{
int []a = {15,25,2,8,19,22,5,30,0,7} ;
selectionsort(a);
for(int i=0;i<=a.length-1;i++)
    System.out.println(a[i]);
}

public static void selectionsort(int[] a)
{
for(int i=0;i<=a.length-2;i++)
{
    int pos=i;
    for(int j=i+1;j<=a.length-1;j++)
        if(a[j]<a[pos])
            pos=j;
            int t=a[pos];
            a[pos]=a[i];
            a[i]=t;
}
}

}

Matrix Multiplication

import java.util.*;
public class Matrixmult
{
public static void main (String [] args)
{

int [] [] a ={ {1,2,3} , {4,5,6} , {7,8,9} };
int [] [] b ={ {3,3,3} , {2,2,2} , {1,1,1} };
int m=3, n=3 , r=3;
int [] [] c = new int [m][r];

for (int i=0; i<=m-1; i++)
{       for(int j=0; j<=n-1; j++)
    for(int k=0;k<=r-1;k++)
     c[i][k] = c[i][k]+a[i][j]*b[j][k];
}

for (int i=0; i<=m-1; i++)
{       for(int j=0; j<=n-1; j++)
    System.out.printf("%d\t",c[i][j]);
      System.out.println();
}

}
}

06 September, 2012

Age Validation.

import java.util.*;
public class AgeException
{
public static void main (String [] args)
{
Scanner s = new Scanner(System.in);
while(true)
{
try
{
System.out.println("Enter ur Age");
int n=s.nextInt();
int x=age(n);
return;
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public static int age(int n)throws Exception
{
if (n<18 || n>60)
throw new Exception ("Please Enter age18 to 60");
System.out.println("You r selected");
return n;
}
}

Use StringTokenizer class

Java program that displays input integer and the sum of all the integers 

( use StringTokenizer class )



import java.io.*;
import java.lang.*;
import java.util.*;
class StringTokenizerExample
{
  public static void main(String args[])
 {
  int n;
  int sum=0;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter integer seperated by space");
  String str=s.nextLine();
  StringTokenizer st=new StringTokenizer(str," ");
  while(st.hasMoreTokens())
  {
    String temp=st.nextToken();
    n=Integer.parseInt(temp);
    System.out.println(n);
    sum=sum+n;
   }
    System.out.println("sum="+sum);
 }
}

All real solutions to the quadratic equation

Java program that prints all real solutions to the quadratic equation ax2+bx+c=0.


import java.lang.*;
import java.util.*;
class Quadratic
{
public static void main(String args[])
{
int a,b,c,d;
double e,f;

Scanner s = new Scanner(System.in);

System.out.println("Enter value a = ");
a=s.nextInt();
System.out.println("Enter value b = ");
b=s.nextInt();
System.out.println("Enter value c = ");
c=s.nextInt();
System.out.println("Your equation : "+a+"x2 + "+b+"x + "+c+" = 0");

d=(b*b)-(4*a*c);
if(d<0)
{
    System.out.println("No real roots");
}
else
{
    e=((-b+Math.sqrt(d))/(2*a));
    f=((-b-Math.sqrt(d))/(2*a));
    System.out.println("Roots are :"+e+" , "+f);
}

}
}

Prints out all prime numbers upto that integer

Program that prints out all prime numbers upto that integer


import java.io.*;
import java.lang.*;
import java.util.*;
class Prime
{
  public static void main(String args[])
  {
    int i,j,n;
    int count=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter Number : ");
    n=s.nextInt();
    for(i=1;i<=n;i++)
    {
      count=0;
      for(j=1;j<=i;j++)
      {
        if(i%j==0)
         count++;
      }
      if(count==2)
      System.out.println("\t"+i);
     }
   }
}


05 September, 2012

Fibonacci by Recursive Functions

Write a java program that uses recursive functions

import java.lang.*;
import java.util.*;
class FibonacciRec
{
   public static void main(String args[])
   {
      int a=0,b=1;
      Recurssion fr= new Recurssion();
      Scanner s = new Scanner(System.in);
      System.out.println("Enter length of series");     

      int n1= s.nextInt();
      int n=n1-3;
      System.out.println("Fibonacci series for "+n1+" is");
      System.out.print(" "+a+" "+b+" ");
      fr.recurssion(n,a,b);
   }
}
class Recurssion
{
  int i=0;
  public void recurssion(int n,int a ,int b)
  {
      int c;
      if(i<=n)
      {
        c=a+b;
        a=b;
        b=c;
        System.out.print(c+" ");
        i++;
        recurssion(n,a,b);
      }
   }
}

02 July, 2012

Find out the given number is Armstrong or not.

import java.util.*;
class Armstrong
{
public static void main(String []args)
{
int sum=0;
Scanner s=new Scanner(System.in);
System.out.print("Enter Number = ");
int n=s.nextInt();
int temp=n;

for(int i=0;i<=n ; i++)
{
int r=n%10;
sum=sum+(r*r*r);
n=n/10;
}

if(sum==temp)
System.out.printf("\n%d is Armstrong",temp);
else
System.out.printf("\n%d is not Armstrong",temp);

}
}

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);
}

}