Practicle java core
Practicle java core
[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
[2]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
Average=31
[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
[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
[6]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
}
x.close();
}
}
Output:
Enter Three Numbers:
24 30 22
30is maximum
[7]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
Output:
Enter to calculate Factorial=
5
Factorial of5 is:120
[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
[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
[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
[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
[15]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
[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
[19]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
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
[21]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
[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
[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
[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
[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]
[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
[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....
[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
[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
[42]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
[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
Area of circle:706.5
Area of Square:100.0
[46]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
[47]
NAME-VAISHNAVI SURESH SALUNKHE BATCH NO-B3
ROLL NO-30 BCS-TY
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
[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
[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]