Professional Documents
Culture Documents
JAVA PROGRAMMING
Code:
Code:
Code:
public class factorial
{
public static void main(String args[]){
int i,fact=1;
int number=5;
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is:
"+fact);
}
}
Output:
Java program to check whether given string is a
palindrome or not.
Code:
public class checkingpallindrome
{
public static void main(String[] args) {
String string = "Kayak";
boolean flag = true;
string = string.toLowerCase();
for(int i = 0; i < string.length()/2; i++){
if(string.charAt(i) !=
string.charAt(string.length()-i-1)){
flag = false;
break;
}
}
if(flag)
System.out.println("Given string is
palindrome");
else
System.out.println("Given string is not a
palindrome");
}
}
Output:
Java program to show bubble sort.
Code:
public class bubblesort
{
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};
System.out.println("Array Before Bubble
Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
bubbleSort(arr);
System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Output:
Write a program to show swing.
Code:
import javax.swing.*;
public class swing1 extends JFrame{//inheriting JFrame
JFrame f;
swing1(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);
add(b);//adding button on frame
setSize(400,500);
setLayout(null);
setVisible(true); }
public static void main(String[] args) {
new swing1();
}
}
Output:
Write a program showing use of reflection
Code:
import java.lang.reflect.*;
import java.awt.*;
public class reflection
{
public static void main(String[] args){
Rectangle r = new Rectangle();
showConstructors(r);
}
static void showConstructors(Object o){
Class c = o.getClass();
Constructor[] theConstructors =
c.getConstructors();
for(int i =0; i<theConstructors.length; i++){
System.out.println("(");
Class[]parameterTypes =
theConstructors[i].getParameterTypes();
for(int k= 0; k<parameterTypes.length; k++){
String parameterString =
parameterTypes[k].getName();
System.out.print(parameterString + "");
}
System.out.println(")");
}
}
}
Output:
Write a program using collection.
Code:
import java.util.*;
public class collection
{ public static void main(String args[]){
PriorityQueue<String> queue=new
PriorityQueue<String>();
queue.add("Amit Sharma");
queue.add("Vijay Raj");
queue.add("JaiShankar");
queue.add("Raj");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()) { System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()) {
System.out.println(itr2.next()); } } }
Output:
Write a program to show use of exception
handling.
Code:
class exception_handling extends Exception
{
public exception_handling (String str)
{
super(str);
}
}
class TestCustomException1
{
// method to check the age
static void validate (int age) throws
exception_handling{
if(age < 18){
throw new exception_handling("age is not valid to
vote");
}
else {
System.out.println("welcome to vote");
}
}
// main method
public static void main(String args[])
{
try
{
// calling the method
validate(13);
}
catch (exception_handling ex)
{
System.out.println("Caught the exception");
Output: