Q1 -Wap that implements the concept of Encapsulation.
Coding
class Area {
// fields to calculate area
int length;
int breadth;
Area(int length, int breadth)
{
this.length = length;
this.breadth = breadth;
}
// method to calculate area
public void getArea()
{
int area = length * breadth;
System.out.println("Area: " + area);
}
}
class Main
{
public static void main(String[] args)
{
// create object of Area
// pass value of length and breadth
Area rectangle = new Area(5, 6);
rectangle.getArea();
}
BCA – III (Programming in Java)
1
}
Output-area = 30
BCA – III (Programming in Java)
2
Q2 – Wapto demonstrate concept of function overloading of
polymorphism .
Coding
class Shapes
{
public void area()
{
System.out.println("Find area ");
}
public void area(int r)
{
System.out.println("Circle area = "+3.14*r*r);
}
public void area(double b, double h)
{
System.out.println("Triangle area="+0.5*b*h);
}
public void area(int l, int b)
{
System.out.println("Rectangle area="+l*b);
}
}
class Main
{
public static void main(String[] args)
{
Shapes myShape = new Shapes(); // Create a Shapes object
myShape.area();
myShape.area(5);
myShape.area(6.0,1.2);
myShape.area(6,2);
}
BCA – III (Programming in Java)
3
}
Output
Output-find area
Circle area = 78.5
Triangle area = 3.5999999999999996
Rectangle area = 12
BCA – III (Programming in Java)
4
Q. 3 Write a program to demonstrate concept of construction
overloading of Polymorphism.
Program:
class ConsOverloading
{
Int age;
String name;
//creating two arg constructor
ConsOverloading(int i,String n)
{
id = i;
name = n;
}
//creating three arg constructor
ConsOverloading(int i,String n,int a)
{
id = i;
name
= n;
age=a;
}
void display()
{
System.out.println(id+" "+name+" "+age);
}
}public static void main(String args[])
{
ConsOverloading c1 = new ConsOverloading(001,"Divya");
ConsOverloading c2 = new ConsOverloading(002,"Karan",20);
System.out.println("First Constructer Overloaded: ");
c1.display();
System.out.println("Second Constructer Overloaded: ");
c2.display();
} }
BCA – III (Programming in Java)
5
Output:
BCA – III (Programming in Java)
6
Q. 4 Write a program the use Boolean data type and print the
prime number series up to 50.
Program:
class Prime
public static void main(String args[])
int num,i;
boolean prime=true;
System.out.println("Prime
Numbers upto 50 are:");
for(num=2;num<=50;num++)
{
prime=true; for(i=2;i<=num/2;i++)
if(num%i==0)
{
prime=false;
}
if(prime)
System.out.println(""+num);
}
}
}
BCA – III (Programming in Java)
7
Output:
BCA – III (Programming in Java) 8
Q5) Write a program to print first 10 number of the following Series using Do-
while Loops 0,1,1,2,3,5,8,11.
Code:-
class Fibonacci
{
public static void main(String []args)
{
int N=10,b=-1,c=1,sum,i=1;
do
{
sum=b+c;
System.out.print(" "+sum);
b=c;
c=sum;
i++;
}
while(i<=N);
System.out.println();
}
}
BCA – III (Programming in Java) 9
Output:-
BCA – III (Programming in Java) 10
Q6) Write a program to check the given number is Armstrong or not.
Code :-
class Armstrong
{
public static void main(String []args)
{ int n,sum=0,count=0;
Scanner input= new Scanner(System.in);
System.out.print("\nEnter a number to check Armstrong or not : ");
int number =input.nextInt();
int num=number;
while(num!=0)
{ num=num/10;
count++;
}
num=number;
while (num != 0)
{ n=num%10;
sum=sum+(int)Math.pow(n,count);
num=num/10;
}
if (number == sum )
System.out.println("\n"+number + " is an Armstrong number ");
else
System.out.println("\n"+number + " is not an Armstrong number ");
}
}
Output:-
BCA – III (Programming in Java) 11
7. WAP to find the factorial of any given number?
BCA – III (Programming in Java) 12
CODE :
import java.util.Scanner;
public class Factorial
{
public static void main(String args[])
{
int i, factorial=1, number;
System.out.println("Enter the number to which you need to find the factorial:" );
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
for(i = 1; i<=number; i++)
{
factorial = factorial * i;
}
System.out.println("Factorial of the given number is:: "+factorial); } }
OUTPUT
BCA – III (Programming in Java) 13
8. WAP to sort the element of One Dimensional Array in Ascending order?
BCA – III (Programming in Java) 14
CODE :
import java.util.Scanner;
public class Ascending _Order { public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:" );
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} }
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
} }
Output :
BCA – III (Programming in Java) 15
9. Write a program for matrix transposes using input/output stream class.
Program:
import java.util.Scanner;
public class Transpose
{
BCA – III (Programming in Java) 16
public static void main(String args[])
{
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i< row; i++)
{
for(j = 0; j < column; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i< row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is "); for(i = 0;
i< column; i++)
{
for(j = 0; j < row; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}
Output:
BCA – III (Programming in Java) 17
10. Write a program to add the elements of Vector as arguments of main
method (Run time) and rearrange them, and copy it into an Array.
BCA – III (Programming in Java) 18
Program:
import java.util.*;
class LanguageVector
{
public static void main(String args[])
{
Vector list = new Vector();
int length = args.length;
for(int i=0;i<length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt("COBOL",2);
int size=list.size();
String listArray[]=new String[size];
list.copyInto(listArray);
System.out.println("List of Languages");
for(int i=0;i<size;i++)
{
System.out.println(listArray[i]);
}
}
}
Output:
BCA – III (Programming in Java) 19
11. WAP to check given string is palindrome or not ?
import java.util.Scanner;
BCA – III (Programming in Java) 20
public class Ascending _Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:" );
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{ for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}}}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
} System.out.print(a[n - 1]);
} }
Output:-
BCA – III (Programming in Java) 21
12. WAP to sort the elements in alphabetic order ?
BCA – III (Programming in Java) 22
importjava.util.Arrays;
importjava.util.Scanner;
publicclassalphabetical {
publicstaticvoidmain(Stringargs[]) {
inttemp, size;
Scannersc = newScanner(System.in);
System.out.println("Enter a string value: ");
Stringstr = sc.nextLine();
charcharArray[] = str.toCharArray();
size = charArray.length;
for(inti = 0; i<size; i++ ) {
for(intj = i+1; j<size; j++) {
if(charArray[i]>charArray[j]) {
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = (char) temp;
}
}
}
System.out.println("Third largest element is:
"+Arrays.toString(charArray));
}
}
Output:-
BCA – III (Programming in Java) 23
13.WAP that use the multiple catch statements within the try-catch mechanism.
BCA – III (Programming in Java) 24
CODE:
public class MultipleCatchBlock1 {
public static void main(String[] args) {
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
OUTPUT:
BCA – III (Programming in Java) 25
14. WAP where user will create a Self-Exception using the “throw” keyword.
BCA – III (Programming in Java) 26
CODE:
import java.util.Scanner;
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
class User_Defined_Exception
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();
try
{
if(a<10)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
BCA – III (Programming in Java) 27
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
OUTPUT:
BCA – III (Programming in Java) 28
BCA – III (Programming in Java) 29
Q15. Write a Program for multithread using th isAlive(), join() and
synchronized() methods of thread class
Coding :
class NewThread implements Runnable
{
String name;
Thread t;
NewThread(String threadname)
{
name=threadname;
t=new Thread(this,name);
System.out.println("New thread = "+t);
t.start();
}
public void run()
{
try
{
for(int i=5; i>0; i--)
{
System.out.println(name+" : "+t);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println(name+" interrupted.");
}
BCA – III (Programming in Java) 30
System.out.println(name+" exiting.");
}
}
class DemoJoin
{
public static void main(String[] args)
{
NewThread obj1=new NewThread("One");
NewThread obj2=new NewThread("Two");
NewThread obj3=new NewThread("Three");
System.out.println("Thread One Is alive() : "+obj1.t.isAlive());
System.out.println("Thread One Is alive() : "+obj2.t.isAlive());
System.out.println("Thread One Is alive() : "+obj3.t.isAlive());
try
{
System.out.println("waiting for threads to finish ");
obj1.t.join();
obj2.t.join();
obj3.t.join();
}
catch(InterruptedException e)
{
System.out.println("main thread is interrupted.");
}
System.out.println("Thread One Is alive() : "+obj1.t.isAlive());
System.out.println("Thread One Is alive() : "+obj2.t.isAlive());
System.out.println("Thread One Is alive() : "+obj3.t.isAlive());
System.out.println("Main thread exiting.");
} }
BCA – III (Programming in Java) 31
Output :
BCA – III (Programming in Java) 32
Q16. Write a Program to create a package using command and one
package in import another package .
Coding :
package Test;
public class Classa
{
public void disp( int n)
{
int x, fact=1;
for( x=1; x<=n; x++)
{
fact=fact*x;
}
System.out.println("Factorial : "+fact);
}
}
Main Coding :
import Test.Classa;
class Trial
{
public static void main(String[] args)
{
Classa A=new Classa();
A.disp(5);
}
}
BCA – III (Programming in Java) 33
Output :
BCA – III (Programming in Java) 34
Q. 17 Write a program to perform multiplication of two matrices.
import java.io.*;
class matmul
{
public static void main(String arg[]) throws IOException
{
int r1,c1,r2,c2;
int a[][]=new int[10][10];
int b[][]=new int[10][10];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the row and column for 1st matrix");
r1=Integer.parseInt(br.readLine());
c1=Integer.parseInt(br.readLine());
System.out.println("enter the row and column for 2nd matrix");
r2=Integer.parseInt(br.readLine());
c2=Integer.parseInt(br.readLine());
if(c1==r2)
{
System.out.println("enter the number for 1st matrix");
for(int i=0;i<r1;i++)
for(int j=0;j<c1;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
BCA – III (Programming in Java) 35
System.out.println("enter the number for 2nd matrix");
for(int i=0;i<r2;i++)
for(int j=0;j<c2;j++)
{
b[i][j]=Integer.parseInt(br.readLine());
}
System.out.println("\n1st matrix is\n ");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("\n2nd matrix is \n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
System.out.println("\nmultiplication of matrix \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
BCA – III (Programming in Java) 36
int temp=0;
for(int r=0;r<3;r++)
temp=temp+a[i][r]*b[r][j];
System.out.print(temp+" ");
}
System.out.println();
}
}
else
System.out.println("not possible");
}
}
BCA – III (Programming in Java) 37
Output:
BCA – III (Programming in Java) 38
Q, 18 Write a program to sort a stream of Strings.
Source-Code:
import java.io.*;
import java.util.Scanner;
public class SortArray {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(stream);
String input = "";
Scanner in = new Scanner(System.in);
//ask number of ppl and set up array
int TotalNames;
System.out.print("Enter the total number of names: ");
TotalNames = Integer.parseInt(buffer.readLine());
System.out.println();
final int TOTNAMES = TotalNames;
String[] names = new String[TOTNAMES];
//input names
for (int i = 0; i < names.length; i++) {
System.out.print("Enter a name for person #" + (i) + ": ");
input = in.nextLine();
names[i] = input;
while (true) {
for (int i = 0; i < names.length - 1; i++) {
for (int j = i + 1; j < names.length; j++) {
if (names[i].compareTo(names[j]) > 0) {
String temp = names[i];
BCA – III (Programming in Java) 39
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.println();
System.out.println("The names sorted:");
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
int Changes;
System.out.println();
System.out.print("Would you like to change any of the names? 1-Yes 0-No: ");
Changes = Integer.parseInt(buffer.readLine());
if (Changes == 1) {
System.out.println();
for (int i = 0; i < names.length; i++) {
System.out.println("#" + i + " " + names[i]);
} else {
System.out.println();
System.out.println("PROGRAM TERMINATED");
return;
}int TheChange;
System.out.print("What is the number of the name would you like to change?: ");
TheChange = Integer.parseInt(buffer.readLine());
System.out.print("Enter a NEW name for person #" + (TheChange)
+ " previously named " + names[TheChange] + ": ");
input = in.nextLine();
names[TheChange] = input;
System.out.println(); } }
BCA – III (Programming in Java) 40
BCA – III (Programming in Java) 41
Q. 19 :-Write a program to add two integer numbers using command line
argument.
PROGRAM:
class add
{
public static void main(String str[])
{
int a,b,c;
a=Integer.parseInt(str[0]);
b=Integer.parseInt(str[1]);
c=a+b;
System.out.println("Sum Is =- " + c);
}
BCA – III (Programming in Java) 42
OUTPUT:
BCA – III (Programming in Java) 43
Q. 20 Write a program to reverse a 5digit number and calculate sum of its digit
using for loop.
PROGRAM:
class rev
{
public static void main(String str[])
{
int a,b,sum=0,rev=0;
a=Integer.parseInt(str[0]);
for(;a>0;)
{
b=a%10;
rev=rev*10+b;
sum=sum+b;
a=a/10;
}
System.out.println("revers is "+rev);
System.out.println("sum of the digit "+sum);
}
}
BCA – III (Programming in Java) 44
OUTPUT:
BCA – III (Programming in Java) 45
Q. 21 Write a program to generate a pattern using 2D array.
0
12
345
6789
PROGRAM:
class pattern
{public static void main(String str[])
{int toD[][]=new int[4][];
toD[0]=new int[1];
toD[1]=new int[2];
toD[2]=new int[3];
toD[3]=new int[4];
int i,j,k=0;
for(i=0;i<4;i++)
for(j=0;j<i+1;j++)
{
toD[i][j]=k;
k++;
}
for(i=0;i<4;i++)
{
for(j=0;j<i+1;j++)
System.out.print(toD[i][j]+" ");
System.out.println();
}}}
BCA – III (Programming in Java) 46
OUTPUT:
BCA – III (Programming in Java) 47
Q. 22 Write a program to demonstrate the fundamental of inner class.
PROGRAM:
class innerclass
{
public static void main(String str[])
{
outer obj=new outer();
obj.test();
}
}
class outer
{
int w=25;
void test()
{
inner in=new inner();
in.disp();
}
class inner
{
void disp()
{
System.out.println("outer class data is"+w);
}
}
}
BCA – III (Programming in Java) 48
OUTPUT:
BCA – III (Programming in Java) 49
Q. 23 Write a program to inherit a property of a base class in derived class using
super statement
PROGRAM:
class item
{
int itemno;
String disc;
float rate;
int qoh;
void disp()
{
System.out.println("item class");
System.out.println("item no is"+itemno);
System.out.println("disc is"+disc);
System.out.println("reate is"+rate);
System.out.println("quantity is"+qoh);
}
item()
{
itemno=0;
disc=" ";
rate=0;
qoh=0;
}
item(int n,String s,float r,int q)
{
itemno=n;
disc=s;
BCA – III (Programming in Java) 50
rate=r;
qoh=q;
}
}
// creating printer class
class printer extends item
String type;
int ppm;
printer()
super();
type=" ";
ppm=0;
printer(int a,String b,float c,int d,String e,int f)
super(a,b,c,d);
type=e;
ppm=f;
void disp()
super.disp();
System.out.println("printer class");
System.out.println("type is "+type);
System.out.println("ppm is "+ppm);
BCA – III (Programming in Java) 51
}
public static void main(String str[])
printer p1=new printer();
p1.disp();
printer p2=new printer(10,"husain",4.5f,99,"khan",88);
p2.disp();
BCA – III (Programming in Java) 52
OUTPUT:
BCA – III (Programming in Java) 53
Q. 24 - Write a program to display IP address of a system.
PROGRAM:
import java.net.*;
public class ipadd
{
public static void main(String args[])
{
try
{
InetAddress i=InetAddress.getLocalHost( );
String s=i.toString( );
System.out.println(s.substring(s.indexOf(“/”)+1));
}
catch(Exception e)
{
System.out.println(“Exception : “+e);
}
}
}
BCA – III (Programming in Java) 54
OUTPUT:
BCA – III (Programming in Java) 55
Q.25 - Write a program to display protocol, host name , port and filename of a
system.
PROGRAM:
import java.net.*;
public class host{ public static void main(String args[])
{ try
InetAddress i=InetAddress.getLocalHost( );
String s=i.toString( );
System.out.println(s.substring(0,(s.indexOf("/"))));
catch(Exception e)
System.out.println("Exception : "+e);
BCA – III (Programming in Java) 56
OUTPUT:
BCA – III (Programming in Java) 57