0% found this document useful (0 votes)
2 views

Practicle java core

The document contains a series of Java programming assignments completed by Vaishnavi Suresh Salunkhe, covering basic concepts such as printing statements, data types, arithmetic operations, control statements, classes, and methods. Each assignment includes source code, expected output, and explanations of concepts like type casting, shift operators, and method overloading. The assignments demonstrate practical applications of Java programming skills through various coding exercises.

Uploaded by

ganeshmane5368
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practicle java core

The document contains a series of Java programming assignments completed by Vaishnavi Suresh Salunkhe, covering basic concepts such as printing statements, data types, arithmetic operations, control statements, classes, and methods. Each assignment includes source code, expected output, and explanations of concepts like type casting, shift operators, and method overloading. The assignments demonstrate practical applications of Java programming skills through various coding exercises.

Uploaded by

ganeshmane5368
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3

ROLL NO-30 BCS-TY

Assignment 1 – Basic of Java


1.1 Write a program to print “Hello World” Statement.
Source Code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
Hello World

1.2 Write a program to demonstrate different type of Data Types.


Source Code:
class Datatype
{
public static void main(String args[])
{
int a=10;
String b="VGSP";
boolean c=true;
double x=123.11;
float y=11.12f;
System.out.println("int="+a);
System.out.println("String="+b);
System.out.println("boolean="+c);S
System.out.println("double="+x);
System.out.println("float="+y);
}
}

[1]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Output:
int=10
String=VGSP
boolean=true
double=123.11
float=11.12

Q3- Write a program to calculate sum and average of 3 integer value.


Source Code:
import java.util.*;
class sum
{
public static void main(String args[])
{
int a,b,e;
System.out.println("Enter Three Numbers:");
Scanner X=new Scanner(System.in);
a=X.nextInt();
b=X.nextInt();
e=X.nextInt();
int c=a+b+e;
a=c/3;
System.out.println("Addition="+c);
System.out.println("Average="+a);
}
}
Output:
30 31 32
Addition=93

[2]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Average=31

Q4- Write a program to demonstrate implicit type casting.


Source Code:
class Implicit
{
public static void main(String args[])
{
int a=100;
float b=a;
double c=a;
long x=a;
System.out.println("Implicit type casting:");
System.out.println("int-float"+b);
System.out.println("int-double"+c);
System.out.println("int-long"+x);
}
}
Output:
Implicit type casting:
int-float100.0
int-double100.0
int-long100

Q5- Write a program to demonstrate explicit type casting.


Source Code:
class Explicit
{
public static void main(String args[])

[3]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

{
double a=111.11;
float b=(float)a;
long c=(long)b;
int x=(int)c;
System.out.println("Explicit:");
System.out.println("double-float:"+b);
System.out.println("float-long:"+c);
System.out.println("long-int:"+x);
}
}
Output:
Explicit:
double-float:111.11
float-long:111
long-int:111

Q6- Write a program to demonstrate use of shift operator.


Source Code:
class Shift
{
public static void main(String args[])
{
int a=100;
int b=a<<2;
int c=a>>>2;
int x=a>>2;
System.out.println("Left Shift:"+b);
System.out.println("Right Shift:"+c);

[4]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

System.out.println("unsigned:"+x);
}
}
Output:
Left Shift:400
Right Shift:25
unsigned:25

[5]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 2 – Control Statements


2.1 Write a program to take three numbers from the user and print the greatest number.
Source Code:
package ASS2;
import java.util.*;
public class Max
{
public static void main(String[] args)
{
int a,b,c;
Scanner x=new Scanner(System.in);{
System.out.println("Enter Three Numbers:");
a=x.nextInt();
b=x.nextInt();
c=x.nextInt();
if(a>b)
{
if(a>c)
System.out.println(a+"is maximum");
else
System.out.println(a+"is maximum");
}
else{
if(b>c)
System.out.println(b+"is maximum");
else{
System.out.println(c+"is maximum");
}
}

[6]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
x.close();
}
}
Output:
Enter Three Numbers:
24 30 22
30is maximum

2.2 Write a program to calculate factorial of a given no.


Source Code:
package ASS2;
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int i,fact=1;
System.out.println("Enter to calculate Factorial=");
int number=sc.nextInt();
for(i=1;i<=number;i++)
{
fact=fact*i;
}
System.out.println("Factorial of"+ number +" is:"+fact);
sc.close();
}
}

[7]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Output:
Enter to calculate Factorial=
5
Factorial of5 is:120

2.3 Write a program to print reverse number of given number.


Source Code:
package ASS2;
public class Reverse {
public static void main(String[] args)
{
int number=375684,reverse=0;
while(number != 0)
{
int remainder=number % 10;
reverse=reverse*10+remainder;
number=number/10;
}
{
System.out.println("The reverse of the given number is:"+reverse);
}
}
}
Output:
The reverse of the given number is:486573

2.4 Write a program to print multiplication table from 1 to 10.


Source Code:
package ASS2;

[8]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

import java.util.Scanner;
public class Multiplication {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number:");
int num=sc.nextInt();
for(int i=1;i<10;i++)
{
System.out.println(num+"*"+i+"="+num*i);
}
}
}
Output:
Enter number:
10
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90

2.5 Write a program to print given number is leap year or not.


Source Code:
package ASS2;

[9]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

import java.util.Scanner;
public class Leapyear {
public static void main(String[] args)
{
int year;
System.out.println("Enter on year::");
Scanner sc=new Scanner(System.in);
year=sc.nextInt();
if(((year % 4==0)&&(year % 100!=0))||(year % 400==0)){
System.out.println("Specified year is leap year");
}
else
{
System.out.println("Specified year not is leap year");
}
}
}
Output:
Enter on year::
2003
Specified year not is leap year

[10]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 3 – Classes, Objects & Methods


3.1 Write a program to Display Student information using Student_info & Marks Class & by
adding calculate method.
Source Code:
package ASS3;
class Marks
{
void calculate()
{
int m1 = 55;
int m2 = 70;
int m3 = 90;
int sum = m1 + m2 + m3;
double avg = sum/3;
System.out.println("Total Marks are = " + sum);
System.out.println("Average of Marks = " + avg);
}
}
public class Q1
{
public static void main(String[] args)
{
Marks obj = new Marks();
obj.calculate();
}
}
Output:
Total Marks are = 215
Average of Marks = 71.0

[11]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

3.2 Write a program to display prime numbers in given range using “isPrime” method.
Source Code:
package ASS3;
public class Q2
{
public static void main(String[] args)
{
int min = 2;
int max = 100;
for(int n=min;n<=max;n++)
{
if(isPrime(n))
{
System.out.println(n);
}
}
}
public static boolean isPrime(int num)
{
for(int i = 2; i <= num/i; ++i)
{
if(num % i == 0)
{
return false;
}
}
return true;
}

[12]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
Output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

[13]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

3.3 Write a program to demonstrate finalize() method.


Source Code:
package ASS3;
public class Q3
{
public static void main(String[] args)
{
Q3 obj = new Q3();
System.out.println(obj.hashCode());
obj = null;
// calling garbage collector
System.gc();
System.out.println("end of garbage collection");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
Output:
end of garbage collection
finalize method called

3.4 Write a program to demonstrate use of “this” keyword.


Source Code:
package ASS3;
class Q4
{

[14]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

int a;int b;
Q4 (int a, int b)
{
this.a = a;
this.b = b;
}
void display(){
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Q4 object = new Q4(10, 20);
object.display();
}
}
Output:
a = 10 b = 20

3.5 Write a program to demonstrate String class methods.


Source Code:
package ASS3;
public class Q5{
public static void main(String args[])
{
String s1="java";
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);
String s3=new String("example");

[15]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

System.out.println("Assigning String when Declaring like Variable - " + s1);


System.out.println("Creating Object of String Class & Passing character Array - " + s2);
System.out.println("Creating Object of String class & Pass String Directly - " + s3);
String s=" KasturabaiWalchand college ";
System.out.println("Original String - " + s);
System.out.println("Length of String - " + s.length());
System.out.println("UPPERCASE String - " + s.toUpperCase());
System.out.println("lowercase string - " + s.toLowerCase());
System.out.println("Removing Extra Spaces - " + s.trim());
System.out.println(s.startsWith("Ka"));
System.out.println(s.endsWith("e"));
System.out.println(s.concat(", Sangli"));
String replaceString = s.replace("college", "College");
System.out.println(replaceString);
System.out.println("Sub String - " + s.substring(17));
System.out.println("Sub String - " + s.substring(10, 15));
System.out.println("Connected String - " + String.join("-", "Java" , "Program"));
}
}
Output:
Assigning String when Declaring like Variable - java
Creating Object of String Class & Passing character Array - strings
Creating Object of String class & Pass String Directly - example
Original String - KasturabaiWalchand college
Length of String - 32
UPPERCASE String - KASTURABAIWALCHAND COLLEGE
lowercase string - kasturabaiwalchand college
Removing Extra Spaces - KasturabaiWalchand college
false

[16]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

false
KasturabaiWalchand college , Sangli
KasturabaiWalchand College
Sub String - lchand college
Sub String - rabai
Connected String - Java-Program

3.6 Write a program to accept a String and whether the String is Palindrome or not.
Source Code:
package ASS3;
import java.util.Scanner;
public class Q6 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str , rev = "";
System.out.println("Enter a String = ");
str = sc.next();
int length = str.length();
for(int i = length - 1;i >= 0;i--) {
rev = rev + str.charAt(i);
}
if(str.equals(rev))
{
System.out.println("String is Palindrome");
}
else
{
System.out.println("String is not Palindrome");

[17]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
sc.close();
}
}
Output:
Enter a String =
30
String is not Palindrome

[18]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 4 - Method Overloading & Constructor Overloading


4.1 Write a program to overload method by passing different number of parameters.
Source Code:
package ASS4;
public class Q1 {
public void display(int c)
{
System.out.println("method with 1 parameter="+c);
}
public void display(int c,int num)
{
System.out.println("method with 2 parameter="+c+" "+num);
}
public void display(int c,int num,int f)
{
System.out.println("method with 3 parameter="+c+" "+num+" "+f);
}}
class P1
{
public static void main(String[] args)
{
Q1 obj=new Q1();
obj.display(10);
obj.display(20 ,30);
obj.display(40,50,60);
}
}
Output:
method with 1 parameter=10

[19]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

method with 2 parameter=20 30


method with 3 parameter=40 50 60

4.2 Write a program to calculate area of triangle, rectangle & circle using method
overloading.
Source Code:
package ASS4;
public class Q2 {
public static void main(String[] args)
{
CalculateArea ob=new CalculateArea();
ob.area(4);
ob.area(10,12);
ob.area(5.5);
}
}
class CalculateArea
{
void area(float x)
{
System.out.println("The area of the square is "+Math.pow(x,2)+" sq.units");
}
void area(float x,float y)
{
System.out.println("The area of rectangle is "+x*y+" sq.units");
}
void area(double x)
{
double z=3.14*x*x;
System.out.println("The area of the circle is "+z+" sq.units" );

[20]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
}
Output:
The area of the square is 16.0 sq.units
The area of rectangle is 120.0 sq.units
The area of the circle is 94.985 sq.units

4.3 Write a program to perform addition operation various number of arguments to


demonstrate use of default, parameterized & overloaded constructor.
Source Code:
package ASS4;
class Democlass
{
int val1;int val2;
Democlass()
{
val1=10;
val2=20;
System.out.println("Q3:: No argument Constructor " );
}
Democlass(int num1)
{
val1=num1;
val2=num1;
System.out.println("Q3:: Overloaded Constructor with" );
}
Democlass(int num1,int num2)
{
val1=num1;
val2=num2;

[21]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

System.out.println("Q3:: Overloaded Constructor with" );


}
public void display()
{
System.out.println("val1 === "+val1+" val2 === "+val2);
}
}
class Q3
{
public static void main(String[] args)
{
Democlass d1 = new Democlass();
d1.display();
Democlass d2 = new Democlass(10);
d2.display();
Democlass d3 = new Democlass(20,40);
d3.display();
}
}
Output:
Q3:: No argument Constructor
val1 === 10 val2 === 20
Q3:: Overloaded Constructor with
val1 === 10 val2 === 10
Q3:: Overloaded Constructor with
val1 === 20 val2 === 40

[22]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 5 – Array
5.1 Write a program to display upper, lower, triangular and diagonal matrix.
Source Code:
package ASS5;
import java.util.*;
public class Q1
{
public static void printMatrix(int[][] arr)
{
int m = arr.length;
int n = arr[0].length;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void lowerTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;

[23]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
else
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i < j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Lower Triangular Matrix is : ");
printMatrix(arr);
}
}
public static void upperTriangularMatrix(int arr[][])
{
int m = arr.length;
int n = arr[0].length;
if (m != n)
{
System.out.println("Matrix entered should be a Square Matrix");
System.out.println("Try Again..");
return;
}
else
{

[24]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

for (int i = 0; i < m; i++)


{
for (int j = 0; j < n; j++)
{
if (i > j)
{
arr[i][j] = 0;
}
}
}
System.out.println( "Upper Triangular Matrix is : ");
printMatrix(arr);
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m,n;
System.out.println("Enter the number of rows: ");
m=sc.nextInt();
System.out.println("Enter the number of columns: ");
n=sc.nextInt();
System.out.println("Enter the Matrix Elements: ");
int arr[][] = new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();

[25]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
}
int arr1[][] = new int[m][n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr1[i][j] = arr[i][j];
}
}
System.out.println( "Original Matrix is : ");
printMatrix(arr);
lowerTriangularMatrix(arr);
System.out.println( "Original Matrix is : ");
printMatrix(arr1);
upperTriangularMatrix(arr1);
sc.close();
}
Output:
Enter the number of rows:
3
Enter the number of columns:
3
Enter the Matrix Elements:
356
457
689
Original Matrix is :
356

[26]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

457
689
Lower Triangular Matrix is :
300
450
689
Original Matrix is :
356
457
689
Upper Triangular Matrix is :
356
057
009

5.2 Write a program to know that whether an array is arranged in decreasing order or not.
Source Code:
package ASS5;
public class Q2
{
public static void main(String[] args)
{
int []arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}

[27]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

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


{
for (int j = i+1; j < arr.length; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
System.out.println("Elements of array sorted in descending order: ");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
Output:
Elements of original array:
52871
Elements of array sorted in descending order:
87521

5.3 Write a program to pass array to method.


Source Code:
package ASS5;

[28]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

public class Q3
{
static void display(int arr[])
{
int sum = 0;
System.out.println("Array elements are - ");
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i] + " ");
sum += arr[i];
}
System.out.println("Sum of array elements - " + sum);
}
public static void main(String[] args)
{
int a[]={33,13,44,25};
display(a);
}
}
Output:
Array elements are -
33
13
44
25
Sum of array elements - 115

5.4 Write a Java program to remove a specific element from an array.


Source Code:

[29]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

package ASS5;
import java.util.Scanner;
public class Q4
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number of elements :");
n=sc.nextInt();
Integer arr[]=new Integer[n];
System.out.println("Enter the elements of the array :");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Display Array elements :");
for(int i=0;i<n;i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("Enter the element you want to remove ");
int elem = sc.nextInt();
for(int i = 0; i < arr.length; i++)
{
if(arr[i] == elem)
{
for(int j = i; j < arr.length - 1; j++)

[30]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

{
arr[j] = arr[j+1];
}
break;
}
}
System.out.println("Elements after deletion " );
for(int i = 0; i < arr.length-1; i++)
{
System.out.print(arr[i]+ " ");
}
sc.close();
}
}
Output:
Enter the number of elements :
2
Enter the elements of the array :
23
Display Array elements :
23
Enter the element you want to remove
2
Elements after deletion
3

[31]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 6 – Collection
6.1 Write a program to append the specified element to the end of a linked list.
Source Code:
package ASS6;
import java.util.LinkedList;
public class Q1
{
public static void main(String[] args)
{
LinkedList<String>l_list=new LinkedList<String>();
l_list.add("Red");
l_list.add("Green");
l_list.add("Black");
l_list.add("White");
l_list.add("Pink");
l_list.add("Yellow");
System.out.println("The Linked list:"+l_list);
}
}
Output:
The Linked list:[Red, Green, Black, White, Pink, Yellow]

6.2 Write a Program to Demonstrate Working of Vector Class.


Source Code:
package ASS6;
import java.util.*;
public class Q2 {
public static void main(String[] args)
{

[32]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

int n=5;
Vector<Integer> v= new Vector<Integer>(n);
for(int i=1;i<=n;i++)
v.add(i);
System.out.println("Vector Elements-"+v);
v.remove(3);
System.out.println("Vector elements after removing specified elemenr-");
System.out.println(v);
System.out.println("Display vector elements using for loop-");
for(int i=0;i<v.size();i++)
System.out.print(v.get(i)+" ");
}
}
Output:
Vector Elements-[1, 2, 3, 4, 5]
Vector elements after removing specified elemenr-
[1, 2, 3, 5]
Display vector elements using for loop-
1235

6.3 Write a Program to demonstrate stack implementation.


Source Code:
package ASS6;
import java.util.*;
public class Q3 {
static void stack_push(Stack<Integer> stack)
{
for(int i=0;i<5;i++)
{

[33]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

stack.push(i);
}
}
static void stack_pop(Stack<Integer> stack)
{
System.out.println("Pop Operation:");
for(int i=0;i<5;i++)
{
Integer y=(Integer) stack.pop();
System.out.println(y);
}
}
static void stack_peek(Stack<Integer>stack)
{
Integer element =(Integer) stack.peek();
System.out.println("Element on stack top:"+element);
}
static void stack_search(Stack<Integer>stack,int element)
{
Integer pos=(Integer) stack.search(element);
if(pos==-1)
System.out.println("Element" + element + "not found");
else
System.out.println("Element" + element + "is found at position:"+pos);
}
public static void main(String[] args)
{
Stack<Integer>stack=new Stack<Integer>();
stack_push(stack);

[34]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack,6);
}
}
Output:
Pop Operation:
4
3
2
1
0
Element on stack top:4
Element2is found at position:3
Element6not found

[35]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 7 – Inheritance
7.1 Write a program to display Employee information whose salary is more than 20000.
Source Code:
import java.util.Scanner;
class employee {
int emp_id;
String empname;
}
class salary extends employee
{
double monthly_salary;
String designation;
salary(double monthly_salary,String designation,int emp_id,String empname) {
this.emp_id=emp_id;
this.empname=empname;
this.designation=designation;
this.monthly_salary=monthly_salary;
}
void condition()
{
System.out.println("monthly salary is:"+monthly_salary);
if(monthly_salary>20000)
{
System.out.println("name of employee is::"+empname);
System.out.println("id of employee is::"+emp_id);
System.out.println("monthly salary is::"+monthly_salary);
System.out.println("designation of employee is::"+designation);
}
else

[36]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

{
System.out.println("Cannot display details....");
}
}
}
public class p1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int id,i;
String name,desig;
double sal;
for(i=0;i<3;i++)
{
System.out.println("enter id of employee..");
id=s.nextInt();
System.out.println("enter name of employee..");
name=s.next();
System.out.println("enter designation of employee...");
desig=s.next();
System.out.println("enter salary of employee");
sal=s.nextDouble();
salary x=new salary(sal,desig,id,name);
x.condition();
}
s.close();
}
}

[37]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Output:
enter id of employee..
1
enter name of employee..
VGSP
enter designation of employee...
Developer
enter salary of employee
20000
monthly salary is:20000.0
Cannot display details....

7.2 Create an abstract class 'Bank' with an abstract method 'getBalance'.


Create three subclasses to deposit money & each having a method named
'getBalance'. Call this method by creating an object of each of the three classes.
Source Code –
abstract class Bank
{
abstract int getBalance();
}
class BankA extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
@Override
int getBalance()
{

[38]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

return balance;
}
}
class BankB extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
@Override
int getBalance()
{
return balance;
}
}
class BankC extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
@Override
int getBalance()
{
return balance;
}
}

[39]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

public class p2
{
public static void main(String[] args)
{
BankA bankA = new BankA();
BankB bankB = new BankB();
BankC bankC = new BankC();
bankA.deposit(100000);
bankB.deposit(150000);
bankC.deposit(20000);
System.out.println("Balance of Bank A = Rs."+bankA.getBalance());
System.out.println("Balance of Bank B = Rs."+bankB.getBalance());
System.out.println("Balance of Bank C = Rs."+bankC.getBalance());
}
}
Output:
Balance of Bank A = Rs.100000
Balance of Bank B = Rs.150000
Balance of Bank C = Rs.20000

7.3 Create an abstract class “Marks” with abstract method “getPercentage”.


Create two subclasses to calculate percentage by entering different subject marks
& add final internal subject mark.
Source Code:
public class p3 {
public static void main(String[] args) {
A a = new A(70, 50, 100);
System.out.println("Percentage of Class A - " + a.getPercentage());
B b = new B(90, 75, 64, 86);
System.out.println("Percentage of Class B - " + b.getPercentage());

[40]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
}
abstract class Marks {
public abstract float getPercentage();
final int internal = 35;
}
class A extends Marks {
int marks1, marks2, marks3;
A(int m1, int m2, int m3) {
marks1 = m1;
marks2 = m2;
marks3 = m3;
}
public float getPercentage() {
float total = ((internal + marks1 + marks2 + marks3) / (float) 300) * 100;
return total;
}
}
class B extends Marks {
int marks1, marks2, marks3, marks4;
B(int m1, int m2, int m3, int m4) {
marks1 = m1;
marks2 = m2;
marks3 = m3;
marks4 = m4;
}
public float getPercentage() {
float total = ((internal + marks1 + marks2 + marks3 + marks4) / (float) 400100;
return total;

[41]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
}
Output:
Percentage of Class A - 85.0
Percentage of Class B - 87.5

7.4 Write a program to show use of Super Keyword.


Source Code:
class Vehicle {
int maxSpeed = 120;
}
class Car extends Vehicle {
int maxSpeed = 180;
void display() {
System.out.println("Maximum Speed of Base Class: " + super.maxSpeed);
System.out.println("Maximum Speed: " + maxSpeed);
}
}
class p4 {
public static void main(String[] args) {
Car small = new Car();
small.display();
}
}
Output:
Maximum Speed of Base Class: 120
Maximum Speed: 180

[42]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 8 – Package & Interface


8.1 Write a program to create a package that access the member of external class as well
as same package.
Source Code:
Demo-
package controlstructure;
public class demo{
public void display()
{
System.out.println("This is control structure package class.");
}
}
//package controlstructure;
import controlstructure.*;
public class p1
{
public static void main(String[] args)
{
System.out.println("This is Assignment Package");
demo d=new demo();
d.display();
}
}
Output:
This is Assignment Package
This is control structure package class.

8.2 Write a program to show implementation of interface.


Source Code:

[43]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

interface Printable
{
void print();
}
interface Showable
{
void show();
}
class p2 implements Printable,Showable
{
public void print()
{
System.out.println("This method is declared in Printable interface");
}
public void show()
{
System.out.println("This method is declared in Showable interface");
}
public static void main(String[] args)
{
p2 obj=new p2();
obj.print();
obj.show();
}
}
Output:
This method is declared in Printable interface
This method is declared in Showable interface

[44]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

8.3 Write a program to calculate area of Circle, Square & Tringle using interface.
Source Code:
interface area
{
double pi=3.14;
double calc(double x,double y);
}
class rect implements area{
public double calc(double x,double y)
{
return(x*y);
}
}
class cir implements area{
public double calc(double x,double y)
{
return(pi*x*x);
}
}
class sqr implements area{
public double calc(double x,double y)
{
return(x*x);
}
}
class p3
{
public static void main(String[] args)
{

[45]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

rect r=new rect();


cir c=new cir();
sqr s=new sqr();
area a;
a=r;
System.out.println("\nArea of rectangle:"+a.calc(10,20));
a=c;
System.out.println("\nArea of circle:"+a.calc(15,15));
a=s;
System.out.println("\nArea of Square:"+a.calc(10,10));
}
}
Output:
Area of rectangle:200.0

Area of circle:706.5

Area of Square:100.0

[46]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 9 – Exception Handling


9.1 Write a program to handle the Exception using try & multiple catch block.
Source Code:
public class p1
{
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("Reset of the code");
}
}
Output:
Arithmetic Exception occurs

[47]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Reset of the code

9.2 Write a program to handle the user defined Exception using throw keyword.
Source code:
import java.util.Scanner;
class NegativeAmtException extends Exception{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
public class p2
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Amount:");
int a=s.nextInt();
try
{
if(a<0)
{
throw new NegativeAmtException("Invalid Number");
}

[48]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

System.out.println("Amount deposited");
}
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
Output:
Enter Amount:
100
Amount deposited

[49]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Assignment 10 – Multithreading
10.1 Write a program to implement Runnable Interface
Source Code –
public class p1 implements Runnable
{
@Override
public void run()
{
System.out.println("Thread has ended");
}
public static void main(String[] args)
{
p1 ex = new p1();
Thread t1= new Thread(ex);
t1.start();
System.out.println("Hi");
}
}
Output:
Hi
Thread has ended

10.2 Write a Program for Demonstrating Thread States.


Source Code –
// ABC class implements the interface Runnable
class ABC implements Runnable
{
public void run()
{

[50]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

try
{

Thread.sleep(100);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
System.out.println("The state of thread t1 while it invoked the method join() on thread t2
-"+ ThreadState.t1.getState());
try
{
Thread.sleep(200);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
}
}
public class ThreadState implements Runnable
{
public static Thread t1;
public static ThreadState obj;
// main method
public static void main(String argvs[])
{
obj = new ThreadState();

[51]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

t1 = new Thread(obj);
System.out.println("The state of thread t1 after spawning it - " + t1.getState());
t1.start();
System.out.println("The state of thread t1 after invoking the method start() on it - " +
t1.getState());
}
public void run()
{
ABC myObj = new ABC();
Thread t2 = new Thread(myObj);
System.out.println("The state of thread t2 after spawning it - "+ t2.getState());
t2.start();
System.out.println("the state of thread t2 after calling the method start() on it - " +
t2.getState());
try
{
Thread.sleep(200);
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
System.out.println("The state of thread t2 after invoking the method sleep() on it - "+
t2.getState() );
try
{
t2.join();
}
catch (InterruptedException ie)
{

[52]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

ie.printStackTrace();
}
System.out.println("The state of thread t2 when it has completed it's execution - " +
t2.getState());
}
}
Output:
The state of thread t1 after spawning it - NEW
The state of thread t1 after invoking the method start() on it - RUNNABLE
The state of thread t2 after spawning it - NEW
the state of thread t2 after calling the method start() on it - RUNNABLE
The state of thread t1 while it invoked the method join() on thread t2 -TIMED_WAITING
The state of thread t2 after invoking the method sleep() on it - TIMED_WAITING
The state of thread t2 when it has completed it's execution - TERMINATED

10.3 Write a Program for Synchronization block.


Source Code –
class Table {
void printTable(int n) {
synchronized (this) {
// synchronized block
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}

[53]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
class MyThread2 extends Thread {
Table t
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}
public class p18 {
public static void main(String args[]) {
Table obj = new Table();// only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}

[54]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

Output:
5
10
15
20
25
100
200
300
400
500

[55]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY

[56]

You might also like