Showing posts with label java sample code. Show all posts
Showing posts with label java sample code. Show all posts

Tuesday, August 18, 2009

java code to hear Beep sound,Press any single Digit

import java.util.*;
public class Beep{


public static void main(String[] args) {

if(args[0].equals("1"))
System.out.println("\007");
if(args[0].equals("2"))
System.out.println("\007\007");
if(args[0].equals("3"))
System.out.println("\007\007\007");
if(args[0].equals("4"))
System.out.println("\007\007\007\007");
if(args[0].equals("5"))
System.out.println("\007\007\007\007\007");
if(args[0].equals("6"))
System.out.println("\007\007\007\007\007\007");
if(args[0].equals("7"))
System.out.println("\007\007\007\007\007\007\007");
if(args[0].equals("8"))
System.out.println("\007\007\007\007\007\007\007\007");
}

}

Thursday, March 26, 2009

java code to prtint a pattern

public class patternz
{
public static void main(String[] u)
{
for(int i=1;i<=20;i++)
{
for(int sp=20;sp>i;sp--)
{
System.out.print(" ");
}
for(int j=1;j{
System.out.print("* ");
}
System.out.println();
}
}

}

java code to reverse a String

import java.io.*;
public class reverse1
{
public static void main(String [] x)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Number");
Integer i=new Integer(br.readLine());
String ip=i.toString();
StringBuffer sf=new StringBuffer(ip);
String z=sf.reverse().toString();
System.out.println(z);
if(ip.equals(z))
System.out.println(" The Number "+ip+" is a palindrome");
else
System.out.println(" The Number "+ip+" is not a palindrome");

}
}

Left shift and right shift usig java

public class Shift
{
public static void main(String [] x)
{
int c=5;
System.out.println("After Left Shifting 5 by 2 points "+(c<<2 ));
System.out.println("After Right Shifting 5 by 2 points "+(c>>2 ));

}
}

Alphabatical table using arrays

import java.util.Arrays;
public class testarrays
{
public static void main(String [] x)
{
char [] array1=new char[26];
for (int i = 'A' ,j=0;i<='Z';i++,j++)
array1[j]=(char)i;
System.out.println(Arrays.toString(array1));
}
}

java code for TestBanking

import java.io.*;
class Bank
{
Customer customers[];
int noOfcustomers;
public Bank()
{
customers = new Customer[20];
noOfcustomers=0;
}

public void addcustomer(String name)
{
int i=noOfcustomers++;
 customers[i]= new Customer(name);
}

public int getNumOfcustomers()
{
return noOfcustomers;
}
public Customer getCustomer(int customer_index)
{
return customers[customer_index-1];
}
}

 class Customer
{
String name;
public String toString()
{
return(name);
}

public Customer(String n )
{
name=n;
}
}

class Testbanking
{
public String acceptDetails() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.println("Enter the first name of customer");
String x=br.readLine();
System.out.println();
System.out.println();
System.out.println("Enter the second name of customer");
String y=br.readLine();
return (x+" "+y);
}
public static void main(String [] arr)throws IOException
{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
Bank b = new Bank();
Testbanking tb=new Testbanking();
String choice;
do
{
b.addcustomer(tb.acceptDetails());
int number= b.getNumOfcustomers();
System.out.println("No of customers in the bank = "+number);
System.out.println();
for(int i=1;i<=number;i++)
System.out.println(b.getCustomer(i));
System.out.println();
System.out.println("Continue? Y/N");
choice=br1.readLine();
}
while(choice.toUpperCase().equals("Y"));
}
}

Wednesday, February 25, 2009

Sample java code for Calculator

import java.io.*;
public class Calculus
{
public static void main(String []w) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter The First Integer :- ");
String x=br.readLine();
int y=Integer.parseInt(x);
System.out.println();
System.out.print("Enter The Second Integer:- ");
x=br.readLine();
int z=Integer.parseInt(x);
System.out.println();
System.out.println("Enter Your Choice");
System.out.println();
System.out.println("1.Addition");
System.out.println("2.Substraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");

x=br.readLine();
int choice=Integer.parseInt(x);

switch (choice)
{
case 1:
System.out.println(y+"+"+z+"="+(y+z));
break;

case 2:
System.out.println(y+"-"+z+"="+(y-z));
break;

case 3:
System.out.println(y+"x"+z+"="+(y*z));
break;

case 4:
System.out.println(y+"/"+z+"="+(y/z));
break;

default:System.out.println("Enter listed choices....");

}


}
}