You are on page 1of 19

PRACTICAL FILE

JAVA PROGRAMMING

NAME : KRISHMA TRISAL


CLASS : CSE-B
ROLL NO.: 25641

SUBMITTED TO : DHEERAJ KUMAR SAHNI


INDEX:
1. JAVA PROGRAM TO PRINT A PATTERN
2. JAVA PROGRAM TO PRINT STAR PATTERN
3. JAVA PROGRAM TO SHOW ABSTRACTION
4. JAVA PROGRAM TO PRINT FACTORIAL OF A
NUMBER
5. JAVA PROGRAM TO CHECK WHETHER STRING
IS PALINDROME OR NOT
6. JAVA PROGRAM TO SHOW BUBBLE SORT
7. JAVA PROGRAM TO SHOW THE USE OF SWING
8. JAVA PROGRAM TO SHOW THE USE OF
REFLECTION
9. JAVA PROGRAM TO SHOW THE USE OF
COLLECTION
10.JAVA PROGRAM TO SHOW THE USE OF
EXCEPTION HANDLING
Java program to print the following pattern 5
11111
22222
33333
44444
55555

Code:

public class number_pattern_2 {


public static void main(String args [ ] )
{
for (int i = 1; i <= 5; i ++)
{
for (int j = 1; j <=5; j ++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
Output:
Java program to print the following pattern 3
*
**
***
****
*****

Code:

public class star_pattern {


public static void main(String[] args) {
int i, j, n = 7;
System.out.println("Right angle triangle");
for (i = 1; i < n; i++) {
for (j = 1; j <= i; j++) {
System.out.print(" *");
}
System.out.println("");
}
}
}
Output:
Write a program in java to show abstraction.
Code:
abstract class Bike {
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Output:
Write a program for Factorial Program in Java

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

// printing the message from InvalidAgeException


object
System.out.println("Exception occured: " + ex);
}
System.out.println("rest of the code...");
}
}

Output:

You might also like