Professional Documents
Culture Documents
OF
PROGRAMMING IN
JAVA
(Practical BVSD-45)
1
Assignment 1
1. WAP to print message on the Command Prompt: "Welcome to JAVA".
System.out.println("Welcome to Java");
OUTPUT:
2
Assignment 2
2. WAP to add two numbers.
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
int a=10;
int b=20;
int max=a>b?a:b;
System.out.println(max);
OUTPUT:
4
b).Sol
int a=50;
int b=30;
int c=20;
int min;
min=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
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
int a=678,b,c=0;
while(a>0)
b=a%10;
c=c+b;
a=a/10;
OUTPUT:
7
Assignment 6
6. WAP to find reverse of a number.
public class Reverse {
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 {
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
OUTPUT:
9
Assignment 8 [Switch]
8. WAP to create a calculator(+, -, x, /) using switch.
import java.util.Scanner;
System.out.println("Enter No:");
int n=sc.nextInt();
switch(n)
int a=sc.nextInt();
int b=sc.nextInt();
int c=a+b;
System.out.println(+c);
break;
int d=sc.nextInt();
int e=sc.nextInt();
int f=d-e;
System.out.println(+f);
break;
int i=sc.nextInt();
int j=sc.nextInt();
int k=i*j;
System.out.println(+k);
break;
int l=sc.nextInt();
int m=sc.nextInt();
int o=l/m;
10
System.out.println(+o);
break;
OUTPUT:
11
Assignment 9
9. WAP to calculate the grade of the student using switch.
import java.util.Scanner;
int n=sc.nextInt();
System.out.println("D Grade");
System.out.println("C Grade");
System.out.println("B Grade");
System.out.println("A Grade");
System.out.println("A+ Grade");
}else
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");}
OUTPUT:
14
Assignment 11
11. Java Program to create and call a parameterized constructor.
public class Para {
int a;
int b;
sum=a+b;
return sum;
System.out.println("Before"+sum);
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(){
id = i;
name = n;
OUTPUT:
16
Assignment 13 [Arrays]
13. Write a Java program to sum values of an array.
public class SumArray {
int sum=0;
int i;
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 {
if(num == myArray[i]){
OUTPUT:
18
Assignment 15
15. Write a Java program to find the index of an array element.
import java.util.*;
if (arr == null) {
return -1;
int i = 0;
if (arr[i] == t) {
return i;
else {
i = i + 1;
return -1;
int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
+ findIndex(my_array, 5));
+ 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;
int[] my_array1 = {
my_array1[my_array1.length - i - 1] = temp;
OUTPUT:
21
Assignment 17
17. Write a Java Program to print all the elements of 2-D Array.
public class Print2DArray {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
System.out.println();
OUTPUT:
22
Assignment 18 [Strings]
19. WAP to create a String and display the contents
public class DisString {
char ch[]={'s','t','r','i','n','g','s'};
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 {
String s1="javatpoint";
String s2="python";
OUTPUT:
24
Assignment 20
20. WAP to join the string.
public class StringConcatenation {
System.out.println(s);
OUTPUT:
25
Assignment 21
21. WAP to compare the string.
public class CompareStrings {
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.*;
System.out.println(sb);
OUTPUT:
27
Assignment 23
23. WAP to show difference between String and StringBuffer.
public class StringDiff {
String t = "Java";
t = t + "Tpoint";
return t;
sb.append("Tpoint");
return sb.toString();
concatWithString();
startTime = System.currentTimeMillis();
concatWithStringBuffer();
28
OUTPUT:
29
Assignment 24
24. WAP for StringBuffer Object to:
append
insert
reverse
replace
capacity
append()
public class Append {
sb.append("Java");
System.out.println(sb);
OUTPUT:
insert()
public class Insert {
sb.insert(1,"Java");
System.out.println(sb);
30
OUTPUT:
reverse()
public class RevEXP {
sb.reverse();
System.out.println(sb);
OUTPUT:
replace()
public class Replace {
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
OUTPUT:
31
capacity()
public class Capacity {
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
OUTPUT:
32
Assignment 25 [Inheritence]
25. Write a program to implement single inheritance.
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
d.bark();
d.eat();
OUTPUT:
33
Assignment 26
26. Write a program to implement multilevel inheritance.
class NewAnimal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
void weep(){System.out.println("weeping...");}
class Multi{
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...");}
void bark(){System.out.println("barking...");}
void meow(){System.out.println("meowing...");}
class Hierarchical{
c.meow();
c.eat();
OUTPUT:
35
Assignment 28
28. Write a program to implement hybrid inheritance.
class C
{
System.out.println("C");
class A extends C
System.out.println("A");
class B extends C
System.out.println("B");
class D extends A
System.out.println("D");
36
D obj = new D();
obj.disp();
OUTPUT:
37
Assignment 29 [Exception Handling]
29. WAP for Checked Exceptions.
import java.io.*;
try {
}catch(Exception e) {
System.out.println(e);
OUTPUT:
38
Assignment 30
30. WAP for Unchecked Exceptions.
public class UncheckedException
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;
try {
int a = read.nextInt();
int b = read.nextInt();
int c = a / b;
catch(ArithmeticException ae) {
OUTPUT:
40
Assignment 32 [Multithreading]
32. WAP to implement Java Thread by extending Thread class.
class Multiple extends Thread{
System.out.println("thread is running...");
t1.start();
OUTPUT:
41
Assignment 33
33. WAP to implement Java Thread by implementing runnable interface.
class Multi3 implements Runnable
System.out.println("thread is running...");
t1.start();
OUTPUT:
42
Assignment 34
34. WAP to implement Java Thread Stop Method().
public class JavaStopExp extends Thread
try
sleep(500);
System.out.println(Thread.currentThread().getName());
}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
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
for(int i=1;i<=10;i++)
try
Thread.sleep(500);
catch(InterruptedException e)
System.out.println(e);
System.out.println(i);
t1.start();
45
OUTPUT:
46
Assignment 36
36. WAP to implement synchronization in Threads.
class Table1
{//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);
Table1 t;
MyThread1(Table1 t)
this.t=t;
t.printTable(5);
47
{
Table1 t;
MyThread2(Table1 t)
this.t=t;
t.printTable(2);
t1.start();
t2.start();
OUTPUT:
48