You are on page 1of 49

PRACTICAL FILE

OF
PROGRAMMING IN
JAVA
(Practical BVSD-45)

Submitted to: Submitted by:


Ms. Mandeep Kaur Mohit Kohli
BVSD-2nd Year
Clg R.no:3260710004
Uni R.no:191121703
INDEX
S.No Program Page no. Sign

1 WAP to print message on the Command Prompt: "Welcome to JAVA". 2


2 WAP to add two numbers. 3
3 WAP using conditional operator to find: 4-5
a) Maximum of 2 numbers
b) Minimum of 3 numbers
4 Java Program to demonstrate the example of for loop which prints table of 1. 6
5 WAP to find Sum of a digit of a number. 7
6 WAP to find reverse of a number. 8
7 WAP to find whether given number is palindrome or not. 9
8 WAP to create a calculator(+, -, x, /) using switch. 10-11
9 WAP to calculate the grade of the student using switch. 12-13
10 Java Program to create and call a default constructor . 14
11 Java Program to create and call a parameterized constructor. 15
12 Java Program to show the concept of Constructor Overloading. 16
13 Write a Java program to sum values of an array. 17
14 Write a Java program to test if an array contains a specific value. 18
15 Write a Java program to find the index of an array element. 19-20
16 Write a Java program to reverse an array of integer values. 21
17 Write a Java Program to print all the elements of 2-D Array. 22
18 WAP to create a String and display the contents 23
19 WAP to find the length of a string, using the length() method of the String. 24
20 WAP to join the string. 25
21 WAP to compare the string 26
22 WAP to create a StringBuffer Object. 27
23 WAP to show difference between String and StringBuffer. 28-29
24 WAP for StringBuffer Object to: 30-32
 append()
 insert()
 reverse()
 replace()
 capacity()
25 Write a program to implement single inheritance 33
26 Write a program to implement multilevel inheritance 34
27 Write a program to implement hierarchical inheritance. 35
28 Write a program to implement hybrid inheritance. 36-37
29 WAP for Checked Exceptions. 38
30 WAP for Unchecked Exceptions. 39
31 WAP for Try-Catch Block 40
32 WAP to implement Java Thread by extending Thread class. 41
33 WAP to implement Java Thread by implementing runnable interface. 42
34 WAP to implement Java Thread Stop Method() 43-44
35 WAP to implement Java Thread Sleep Method() 45-46
36 WAP to implement synchronization in Threads. 47-48

1
Assignment 1
1. WAP to print message on the Command Prompt: "Welcome to JAVA".

public class Hello

public static void main (String[] args)

System.out.println("Welcome to Java");

OUTPUT:

2
Assignment 2
2. WAP to add two numbers.

public class add

public static void main(String[] args)

int a=10;

int b=20;

int c=a+b;

System.out.println(c);

OUTPUT:

3
Assignment 3
3. WAP using conditional operator to find:

a) Maximum of 2 numbers

b) Minimum of 3 numbers

a).Sol

public class Max

public static void main(String[] args)

int a=10;

int b=20;

int max=a>b?a:b;

System.out.println(max);

OUTPUT:

4
b).Sol

public class min

public static void main(String[] args)

int a=50;

int b=30;

int c=20;

int min;

if(a<=b && b<=c)

min=a;

}else if(b<=c && c<=a) {

min=b;

}else

min=c;

System.out.println(min);

} }

OUTPUT:

5
Assignment 4 [Loops]
4. Java Program to demonstrate the example of for loop which prints table of 1.
public class Table

public static void main(String[] args)

int a=10;

int i;

for(i=1;i<=10;i++)

int c;

c=a*i;

System.out.println(a+"*"+i+"="+c);

OUTPUT:

6
Assignment 5
5. WAP to find Sum of a digit of a number.
public class DigitSum

public static void main(String[] args)

int a=678,b,c=0;

while(a>0)

b=a%10;

c=c+b;

a=a/10;

System.out.println("Sum of all the digits will be:"+c);

OUTPUT:

7
Assignment 6
6. WAP to find reverse of a number.
public class Reverse {

public static void main(String[] args) {

int a=678,b,c=0;

while(a>0)

b=a%10;

c=c*10+b;

a=a/10;

System.out.println(+c);

OUTPUT:

8
Assignment 7
7. WAP to find whether given number is palindrome or not.
public class Palindrome {

public static void main(String[] args)

int a=676,b,c=0;

int d=a;

while(a>0)

b=a%10;

c=c*10+b;

a=a/10;

System.out.println(+c);

if(d==c)

System.out.println("It is a palindrome");

}else

System.out.println("It is not a palindrome");

OUTPUT:

9
Assignment 8 [Switch]
8. WAP to create a calculator(+, -, x, /) using switch.
import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

System.out.println("1.Add \n 2.Sub \n 3.Mul \n 4.Div \n");

try (Scanner sc = new Scanner(System.in)) {

System.out.println("Enter No:");

int n=sc.nextInt();

switch(n)

case 1: System.out.println("Enter two numbers;");

int a=sc.nextInt();

int b=sc.nextInt();

int c=a+b;

System.out.println(+c);

break;

case 2:System.out.println("Enter two numbers;");

int d=sc.nextInt();

int e=sc.nextInt();

int f=d-e;

System.out.println(+f);

break;

case 3:System.out.println("Enter two numbers;");

int i=sc.nextInt();

int j=sc.nextInt();

int k=i*j;

System.out.println(+k);

break;

case 4:System.out.println("Enter two numbers;");

int l=sc.nextInt();

int m=sc.nextInt();

int o=l/m;

10
System.out.println(+o);

break;

default: System.out.println("Please enter a correct number");

OUTPUT:

11
Assignment 9
9. WAP to calculate the grade of the student using switch.
import java.util.Scanner;

public class Grade {

public static void main(String[] args) {

try (Scanner sc = new Scanner(System.in)) {

System.out.println("Enter marks Obtained");

int n=sc.nextInt();

if(n>=50 && n<=60)

System.out.println("D Grade");

}else if(n>60 && n<=70)

System.out.println("C Grade");

}else if(n>70 && n<=80)

System.out.println("B Grade");

}else if(n>80 && n<=90)

System.out.println("A Grade");

}else if(n>90 && n<=100)

System.out.println("A+ Grade");

}else

System.out.println("Fail hai Bhai tu!!! \n Better Luck Next Time");

12
OUTPUT:

13
Assignment 10 [Constructors]
10. Java Program to create and call a default constructor .
public class Bike1{

Bike1(){System.out.println("Bike is created");}

public static void main(String args[]){

Bike1 b=new Bike1();

OUTPUT:

14
Assignment 11
11. Java Program to create and call a parameterized constructor.
public class Para {

int a;

int b;

static int sum=10;

int add(int a, int b)

sum=a+b;

return sum;

public static void main(String[] args) {

System.out.println("Before"+sum);

Para ob = new Para();

ob.add(30, 10);

System.out.println("After"+sum);

OUTPUT:

15
Assignment 12
12. Java Program to show the concept of Constructor Overloading.
public class Student {

int id;

String name;

Student(){

System.out.println("this a default constructor");

Student(int i, String n){

id = i;

name = n;

public static void main(String[] args) {

Student s = new Student();

System.out.println("\nDefault Constructor values: \n");

System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);

System.out.println("\nParameterized Constructor values: \n");

Student student = new Student(10, "David");

System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);

OUTPUT:

16
Assignment 13 [Arrays]
13. Write a Java program to sum values of an array.
public class SumArray {

public static void main(String[] args) {

int sum=0;

int i;

int []a= new int[]{1,2,3,4,5,6,7,8,9};

for(i=0;i<a.length;i++)

sum=sum+a[i];

System.out.println(+sum);

OUTPUT:

17
Assignment 14
14. Write a Java program to test if an array contains a specific value.
public class Givenelm {

public static void main(String args[]){

int[] myArray = {55, 45, 69, 44};

int num = 55;

for(int i = 0; i<myArray.length; i++){

if(num == myArray[i]){

System.out.println("Array contains the given element");

OUTPUT:

18
Assignment 15
15. Write a Java program to find the index of an array element.
import java.util.*;

public class index {

public static int findIndex(int arr[], int t)

if (arr == null) {

return -1;

int len = arr.length;

int i = 0;

while (i < len) {

if (arr[i] == t) {

return i;

else {

i = i + 1;

return -1;

public static void main(String[] args)

int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };

System.out.println("Index position of 5 is: "

+ findIndex(my_array, 5));

System.out.println("Index position of 7 is: "

+ findIndex(my_array, 7));

19
OUTPUT:

20
Assignment 16
16. Write a Java program to reverse an array of integer values.
import java.util.Arrays;

public class RevArray {

public static void main(String[] args){

int[] my_array1 = {

1789, 2035, 1899, 1456, 2013,

1458, 2458, 1254, 1472, 2365,

1456, 2165, 1457, 2456};

System.out.println("Original array : "+Arrays.toString(my_array1));

for(int i = 0; i < my_array1.length / 2; i++)

int temp = my_array1[i];

my_array1[i] = my_array1[my_array1.length - i - 1];

my_array1[my_array1.length - i - 1] = temp;

System.out.println("Reverse array : "+Arrays.toString(my_array1));

OUTPUT:

21
Assignment 17
17. Write a Java Program to print all the elements of 2-D Array.
public class Print2DArray {

public static void main(String[] args) {

final int[][] matrix = {

{ 1, 2, 3 },

{ 4, 5, 6 },

{ 7, 8, 9 }

};

for (int i = 0; i < matrix.length; i++) {

for (int j = 0; j < matrix[i].length; j++) {

System.out.print(matrix[i][j] + " ");

System.out.println();

OUTPUT:

22
Assignment 18 [Strings]
19. WAP to create a String and display the contents
public class DisString {

public static void main(String args[])

String s1="java";//creating string by java string literal

char ch[]={'s','t','r','i','n','g','s'};

String s2=new String(ch);//converting char array to string

String s3=new String("example");//creating java string by new keyword

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

OUTPUT:

23
Assignment 19
19. WAP to find the length of a string, using the length() method of the String.
public class StringLength {

public static void main(String args[]){

String s1="javatpoint";

String s2="python";

System.out.println("string length is: "+s1.length());

System.out.println("string length is: "+s2.length());

OUTPUT:

24
Assignment 20
20. WAP to join the string.
public class StringConcatenation {

public static void main(String args[]){

String s="Sachin"+" Tendulkar";

System.out.println(s);

OUTPUT:

25
Assignment 21
21. WAP to compare the string.
public class CompareStrings {

public static void main(String[] args) {

String style = new String("Bold");

String style2 = new String("Bold");

if(style == style2)

System.out.println("Equal");

else

System.out.println("Not Equal");

OUTPUT:

26
Assignment 22
22. WAP to create a StringBuffer Object.
import java.util.*;

public class Strbuf {

public static void main(String[] args)

StringBuffer sb=new StringBuffer("Welcome to my program");

System.out.println(sb);

OUTPUT:

27
Assignment 23
23. WAP to show difference between String and StringBuffer.
public class StringDiff {

public static String concatWithString() {

String t = "Java";

for (int i=0; i<10000; i++){

t = t + "Tpoint";

return t;

public static String concatWithStringBuffer(){

StringBuffer sb = new StringBuffer("Java");

for (int i=0; i<10000; i++){

sb.append("Tpoint");

return sb.toString();

public static void main(String[] args){

long startTime = System.currentTimeMillis();

concatWithString();

System.out.println("Time taken by Concating with String: "+(System.currentTimeMillis()-


startTime)+"ms");

startTime = System.currentTimeMillis();

concatWithStringBuffer();

System.out.println("Time taken by Concating with StringBuffer: "+(System.currentTimeMillis()-


startTime)+"ms");

28
OUTPUT:

29
Assignment 24
24. WAP for StringBuffer Object to:
 append
 insert
 reverse
 replace
 capacity
append()
public class Append {

public static void main(String args[]){

StringBuffer sb=new StringBuffer("Hello ");

sb.append("Java");

System.out.println(sb);

OUTPUT:

insert()
public class Insert {

public static void main(String args[]){

StringBuffer sb=new StringBuffer("Hello ");

sb.insert(1,"Java");

System.out.println(sb);

30
OUTPUT:

reverse()
public class RevEXP {

public static void main(String args[]){

StringBuffer sb=new StringBuffer("Hello");

sb.reverse();

System.out.println(sb);

OUTPUT:

replace()
public class Replace {

public static void main(String args[]){

StringBuffer sb=new StringBuffer("Hello");

sb.replace(1,3,"Java");

System.out.println(sb);//prints HJavalo

OUTPUT:

31
capacity()
public class Capacity {

public static void main(String args[]){

StringBuffer sb=new StringBuffer();

System.out.println(sb.capacity());//default 16

sb.append("Hello");

System.out.println(sb.capacity());//now 16

sb.append("java is my favourite language");

System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

OUTPUT:

32
Assignment 25 [Inheritence]
25. Write a program to implement single inheritance.
class Animal{

void eat(){System.out.println("eating...");}

class Dog extends Animal{

void bark(){System.out.println("barking...");}

public class Single {

public static void main(String args[]){

Dog d=new Dog();

d.bark();

d.eat();

OUTPUT:

33
Assignment 26
26. Write a program to implement multilevel inheritance.
class NewAnimal{

void eat(){System.out.println("eating...");}

class NewDog extends NewAnimal{

void bark(){System.out.println("barking...");}

class BabyDog extends NewDog{

void weep(){System.out.println("weeping...");}

class Multi{

public static void main(String args[]){

BabyDog d=new BabyDog();

d.weep();

d.bark();

d.eat();

OUTPUT:

34
Assignment 27
27. Write a program to implement hierarchical inheritance.
class Animal1{

void eat(){System.out.println("eating...");}

class Dog1 extends Animal1{

void bark(){System.out.println("barking...");}

class Cat extends Animal1{

void meow(){System.out.println("meowing...");}

class Hierarchical{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

OUTPUT:

35
Assignment 28
28. Write a program to implement hybrid inheritance.
class C
{

public void disp()

System.out.println("C");

class A extends C

public void disp()

System.out.println("A");

class B extends C

public void disp()

System.out.println("B");

class D extends A

public void disp()

System.out.println("D");

public class hybrid{

public static void main(String args[]){

36
D obj = new D();

obj.disp();

OUTPUT:

37
Assignment 29 [Exception Handling]
29. WAP for Checked Exceptions.
import java.io.*;

public class CheckedExceptions {

public static void main(String[] args) {

File f_ref = new File("C:\\Users\\User\\Desktop\\Today\\Sample.txt");

try {

FileReader fr = new FileReader(f_ref);

}catch(Exception e) {

System.out.println(e);

OUTPUT:

38
Assignment 30
30. WAP for Unchecked Exceptions.
public class UncheckedException

public static void main(String[] args)

String msg=null;

System.out.println(msg.length());

String name="abc";

int i=Integer.parseInt(name);

OUTPUT:

39
Assignment 31
31. WAP for Try-Catch Block
import java.util.Scanner;

public class TryCatch {

public static void main(String[] args) {

Scanner read = new Scanner(System.in);

System.out.println("Enter the a and b values: ");

try {

int a = read.nextInt();

int b = read.nextInt();

int c = a / b;

System.out.println(a + "/" + b +" = " + c);

catch(ArithmeticException ae) {

System.out.println("Problem info: Value of divisor can not be ZERO");

OUTPUT:

40
Assignment 32 [Multithreading]
32. WAP to implement Java Thread by extending Thread class.
class Multiple extends Thread{

public void run(){

System.out.println("thread is running...");

public static void main(String args[]){

Multiple t1=new Multiple();

t1.start();

OUTPUT:

41
Assignment 33
33. WAP to implement Java Thread by implementing runnable interface.
class Multi3 implements Runnable

public void run()

System.out.println("thread is running...");

public static void main(String args[])

Multi3 m1=new Multi3();

Thread t1 =new Thread(m1);

t1.start();

OUTPUT:

42
Assignment 34
34. WAP to implement Java Thread Stop Method().
public class JavaStopExp extends Thread

public void run()

for(int i=1; i<5; i++)

try

// thread to sleep for 500 milliseconds

sleep(500);

System.out.println(Thread.currentThread().getName());

}catch(InterruptedException e){System.out.println(e);}

System.out.println(i);

public static void main(String args[])

// creating three threads

JavaStopExp t1=new JavaStopExp ();

JavaStopExp t2=new JavaStopExp ();

JavaStopExp t3=new JavaStopExp ();

// call run() method

t1.start();

t2.start();

// stop t3 thread

t3.stop();

System.out.println("Thread t3 is stopped");

43
OUTPUT:

44
Assignment 35
35. WAP to implement Java Thread Sleep Method().
class TestSleepMethod1 extends Thread

public void run()

for(int i=1;i<=10;i++)

try

Thread.sleep(500);

catch(InterruptedException e)

System.out.println(e);

System.out.println(i);

public static void main(String args[])

TestSleepMethod1 t1=new TestSleepMethod1();

TestSleepMethod1 t2=new TestSleepMethod1();

t1.start();

45
OUTPUT:

46
Assignment 36
36. WAP to implement synchronization in Threads.
class Table1

synchronized void printTable(int n)

{//synchronized method

for(int i=1;i<=10;i++)

System.out.println(n*i);

try

Thread.sleep(400);

catch(Exception e)

System.out.println(e);

class MyThread1 extends Thread

Table1 t;

MyThread1(Table1 t)

this.t=t;

public void run()

t.printTable(5);

class MyThread2 extends Thread

47
{

Table1 t;

MyThread2(Table1 t)

this.t=t;

public void run()

t.printTable(2);

public class TestSynchronization2

public static void main(String args[])

Table1 obj = new Table1();//only one object

MyThread1 t1=new MyThread1(obj);

MyThread2 t2=new MyThread2(obj);

t1.start();

t2.start();

OUTPUT:

48

You might also like