Java File
Java File
Java Programming
Submitted to Submitted by
Mr. Ashish Kumar Nayyar Deepanshi Mendiratta
Enrollment No: 05413702022
Index
Page Date of Faculty
Sr. No. Problem Statement
No. completion Sign.
1. Write a program to compute Simple Interest.
2|Page
05413702022 Deepanshi Mendiratta
3|Page
05413702022 Deepanshi Mendiratta
4|Page
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 1
class Simpleint
int p,r,t;
float a;
p=Integer.parseInt(in1.readLine());
r=Integer.parseInt(in1.readLine());
t=Integer.parseInt(in1.readLine());
a=(float)p*r*t/100;
System.out.println("average= "+(a));}}
OUTPUT:
5|Page
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 2
OUTPUT:
6|Page
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 3
class Avgo7
{int lb,ub,i,sum=0;
int ctr=0;
float avg;
lb=Integer.parseInt(in1.readLine());
ub=Integer.parseInt(in1.readLine());
for(i=lb;i<=ub;i++)
{if(i%7==0)
{ctr++;
sum=sum+i;}}
avg=(float)sum/ctr;
System.out.println("average= "+(avg));}}
OUTPUT:
7|Page
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 4
i.) ii.)
1 2 3 4 5 @
6 7 8 9 @ @
10 11 12 @ @ @
13 14 @ @ @ @
15 @ @ @ @ @
i) public class NumberPattern
int rows = 5;
int count = 1;
8|Page
05413702022 Deepanshi Mendiratta
count++;
System.out.println();
} } }
int rows = 5;
printPattern(rows); }
System.out.print(" ");
if (k == i)
System.out.print("@");
9|Page
05413702022 Deepanshi Mendiratta
else {
System.out.print("@ "); } }
System.out.println(); } }}
PRACTICAL NO – 5
if (isPrime (i))
System.out.println (i);
int count = 0;
if (n < 2)
return false;
{ if (n % i == 0)
return false; }
return true;
10 | P a g e
05413702022 Deepanshi Mendiratta
}}
OUTPUT:
PRACTICAL NO – 6
int n1=0;
n1=Integer.parseInt(args[0]);
System.out.println("05413702022");
System.out.println("Deepanshi Mendiratta");
}}
OUTPUT:
11 | P a g e
05413702022 Deepanshi Mendiratta
12 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 7
13 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 8
int originalNumber = n;
int sum = 0;
while (n > 0) {
n /= 10; }
if (isArmstrong(num)) {
else {
System.out.println("Deepanshi"); System.out.println("05413702022"); }}
14 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 9
int n1,n2,n3,n4;
n1=Integer.parseInt(args[0]);
n2=Integer.parseInt(args[1]);
n3=Integer.parseInt(args[2]);
n4=Integer.parseInt(args[3]);
System.out.println("05413702022");
System.out.println("Deepanshi ");
System.out.println(myArray[i]);
15 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 10
Ques 10) Write a program to initialize and print the elements of an array.
class java10
System.out.println("05413702022");
System.out.println("Deepanshi");
System.out.println(myArray[i]);
}}
16 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 11
import java.util.Collections;
class ArraySort {
Integer[] arr = { 5, 2, 1, 8, 10 };
Arrays.sort(arr, Collections.reverseOrder());
System.out.println("05413702022");
System.out.println("Deepanshi");
System.out.print("descending order-");
System.out.println("");
Arrays.sort(arr);
System.out.print("ascending order-");
} } }
17 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 12
for(int i=0;i<arr.length;i++){
if(arr[i] == key){
return i;
return -1;
System.out.println("05413702022");
System.out.println("Deepanshi");
} }
18 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 13
public static void J13(int arr[], int first, int last, int key){
first = mid + 1;
break;
}else{
last = mid - 1; }
System.out.println("05413702022");
System.out.println("Deepanshi");
int last=arr.length-1;
J13(arr,0,last,key); }}
19 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 14
if(arr[i] <min)
min = arr[i];
System.out.println("05413702022");
System.out.println("Deepanshi");
20 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 15
Ques 15) WAP to display difference between smallest and largest elements
of an Array of 5 integers.
import java.util.Arrays;
max_val = array_nums[i];
min = array_nums[i]; }
System.out.println("05413702022");
System.out.println("Deepanshi");
21 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 16
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
System.out.println("Deepanshi") ;
System.out.println("05413702022") ;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" "); }
System.out.println();}
}}
ii.) Multiplication
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
22 | P a g e
05413702022 Deepanshi Mendiratta
System.out.println("Deepanshi");
System.out.println("05413702022");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{ c[i][j]+=a[i][k]*b[k][j]; }
System.out.print(c[i][j]+" "); }
System.out.println(); }
}}
23 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 17
Ques 17) Write a program to demonstrate the working of any Ten String
class functions.
import java.io.*;
System.out.println("Deepanshi");System.out.println("05413702022");
System.out.println("Enter a string:");
if (length > 7)
if (length > 7)
24 | P a g e
05413702022 Deepanshi Mendiratta
25 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 18
try {
reader.close();
catch (IOException e) {
e.printStackTrace();
26 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 19
Ques 19) Write a program to create a class called StringDemo and declare
three strings and use functions like length, charAt and equals on them.
import java.io.*;
System.out.println("Deepanshi");
System.out.println("05413702022");
if (str1.equals(str2)) {
} else {
27 | P a g e
05413702022 Deepanshi Mendiratta
if (str1.equals(str3)) {
} else {
reader.close();
}}
28 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 20
Ques 20) Write a program to create a class student add enrollment number
name and percentage as data fields. Add constructors and other necessary
functions to the class.
import java.io.*;
int enrollNo;
String name;
double per;
public Student(){}
this.enrollNo=enrollNo;
this.name=name;
this.per=per;
return enrollNo;
return name;
return per;
this.enrollNo=enrollNo;
29 | P a g e
05413702022 Deepanshi Mendiratta
this.name=name;
this.per=per;
System.out.println("Name: "+name);
System.out.println("Percentage: "+per+"%");
System.out.println("-------STUDENT DETAILS-------");
s1.display();
30 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 21
Ques 21) Write a program to create a class student add enrollment number
name and percentage as data fields. Add constructors and other necessary
functions to the class. Add function to perform name based linear search
on an array of 5 objects of student class.
import java.io.*;
int enrollNo;
String name;
double per;
public Student(){}
this.enrollNo=enrollNo;
this.name=name;
this.per=per; }
return enrollNo; }
return name; }
return per; }
this.enrollNo=enrollNo; }
this.name=name; }
this.per=per; }
31 | P a g e
05413702022 Deepanshi Mendiratta
System.out.println("Name: "+name);
System.out.println("Percentage: "+per+"%"); }
System.out.println();
System.out.println("-------STUDENT DETAILS-------");
for(Student stud:students){
stud.displayStudentInfo(); }}
for(Student stud:students){
if(stud.getName().equals(key)){
return stud; }}
return null; }
double per;
Student result;
enroll=Integer.parseInt(rd.readLine());
System.out.print("Name: ");
name=rd.readLine();
System.out.print("Percentage: ");
32 | P a g e
05413702022 Deepanshi Mendiratta
per=Double.parseDouble(rd.readLine());
displayStudentDetails(studentArr);
target=rd.readLine();
result=searchStudent(studentArr, target);
if(result!=null)
{ System.out.println();
System.out.println("Student found:");
result.displayStudentInfo(); }
else{
System.out.println("\nDeepanshi");
System.out.println("05413702022"); }}
33 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 22
Ques 22) Write a program to create a class called Use Static and declare
some static variables, a static method that will print those variables and
static block which will perform some operation on that variable.
import java.io.*;
class UseStatic
static
{ num2=num1*2;
num1++;
{ display();
System.out.println("Deepanshi");
System.out.println("05413702022");
34 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 23
Ques 23) Write a program to create a class Add that will add to numbers
by using a default constructor.
import java.io.*;
class Add
num1=Integer.parseInt(rd.readLine());
num2=Integer.parseInt(rd.readLine());
sum=num1+num2;
System.out.println("Sum: "+sum);
System.out.println("Deepanshi");
System.out.println("05413702022");
35 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 24
Ques 24) Write a program to create a class called triangle with base and
height as the attributes and calculate its area using parameterized
constructor.
import java.io.*;
this.base = base;
this.height = height; }
System.out.println("Deepanshi");System.out.println("05413702022");
reader.close();}}
36 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 25
Ques 25) WAP to create a class called Rectangle and calculate its area
using this keyword.
import java.io.*;
this.length = length;
this.width = width; }
System.out.println("Deepanshi");System.out.println("05413702022");
reader.close(); }}
37 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 26
Ques 26) Write a program to create a class called Box with instance
variables width, height and depth. Create another class BoxWeight which
inherits class Box with an additional instance variable weight. Compute the
volume of the box using inheritance.
import java.io.*;
class Box
Box()
{ width = 0;
height = 0;
depth = 0; }
{ width = w;
height = h;
depth = d;
}}
float weight;
BoxWeight()
super();
weight = 0; }
{ super(w,h,d);
weight = wt;}
void volume()
38 | P a g e
05413702022 Deepanshi Mendiratta
}}
class Demonstration
width = Float.parseFloat(input.readLine());
height = Float.parseFloat(input.readLine());
depth = Float.parseFloat(input.readLine());
box.volume(); } }
39 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 27
class Box
Box()
width = 0;
height = 0;
depth = 0;
width = w;
height = h;
depth = d;
}}
float weight;
BoxWeight()
{ super();
weight = 0;
{ super(w,h,d);
40 | P a g e
05413702022 Deepanshi Mendiratta
weight = wt; }
void volume()
}}
class Demonstration1
width = Float.parseFloat(input.readLine());
height = Float.parseFloat(input.readLine());
depth = Float.parseFloat(input.readLine());
box.volume(); } }
41 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 28
Ques 28) Create a class Figure and its 2 subclasses Rectangle and Triangle
and calculate its area using dynamic method dispatch.
abstract class Figure
double length;
double width;
this.length = length;
this.width = width;
double calculateArea(){
return length*width;
}}
double base;
double height;
this.base = base;
this.height = height;
42 | P a g e
05413702022 Deepanshi Mendiratta
double calculateArea(){
return 0.5*(base*height);
}}
class Demo
}}
43 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 29
Ques 29) Implement the above program using abstract classes by making
Figure class abstract.
abstract class Figure
double length;
double width;
this.length = length;
this.width = width;
double calculateArea(){
return length*width;
}}
double base;
double height;
this.base = base;
this.height = height;
44 | P a g e
05413702022 Deepanshi Mendiratta
double calculateArea(){
return 0.5*(base*height);
}}
class Demo
}}
45 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 30
Ques 30) Write relevant programs to show the usage of final keyword with
class, variables and method in java.
final class FinalClass {
}}
void display(){
}}
class J30 {
finalObject.MAX_VALUE = 30;
46 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 31
Ques 31) Write a relevant program to show usage of package and access
protection in java, also show the concept of importing a package.
Demonstration.java
import mypckg.DemoPckg;
class Demonstration
demo.add(10, 30);
demo.var = 40;}}
DemoPckg.java
package mypckg;
sum = a+b;
47 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 32
Ques 32) Write a relevant program to show usage of package and access
protection in java, also show the concept of importing a package.
interface Stack
int pop();
void display();}
MyStack()
top = -1; }
{ if(top == MAX_SIZE-1)
{ System.out.println("Stack Overflow");
return;}
stack[++top] = item;}
{ if(top == -1)
{ System.out.println("Stack Underflow");
return -1;}
return stack[top--];
{ if(top == -1)
48 | P a g e
05413702022 Deepanshi Mendiratta
{ System.out.println("Stack is empty");}
else
System.out.println("Stack: ");
System.out.print(stack[i]+" | "); }
System.out.println("");
} }}
class Demonstration {
s1.display();
s1.push(10);
s1.push(13);
s1.push(16);
s1.display();
System.out.println("pop(): "+s1.pop());
System.out.println("pop(): "+s1.pop());
s1.display();
}}
49 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 33
void drive();
interface Engine {
void start();
void stop(); }}
class Demonstration
car.start();
car.drive();
car.stop();
}}
50 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 34
Ques 34) Write a program that throws user defined exceptions if age of
voter is less than 18.
class AgeException extends Exception
AgeException(){
}}
class ElectionCommission
ElectionCommission(){
age = 18;
try{
if(age<18)
throw(new AgeException());
this.name = name;
this.age = age;
catch(AgeException e)
51 | P a g e
05413702022 Deepanshi Mendiratta
}}
Void display()
System.out.println("Voter: "+name);
System.out.println("Age: "+age);
}}
class J34
v1.display();
v2.display();
}}
52 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 35
Ques 35) Write a program that throws user defined exceptions if stack is
full or is empty.
class StackOverflowException extends Exception {
StackOverflowException(String message) {
super(message); }}
StackUnderflowException(String message) {
super(message);}}
class Stack {
int stack[];
int top;
top = -1; }
} else {
stack[++top] = item;
display(); }
} catch (StackOverflowException e) {
System.out.println(e.getMessage());} }
} else {
top--;
display();
53 | P a g e
05413702022 Deepanshi Mendiratta
} catch (StackUnderflowException e) {
System.out.println(e.getMessage());}}
System.out.println("Stack is empty");
System.out.println(""); }}
s1.display(); s1.push(10);
s1.push(20); s1.push(30);
s1.push(40); s1.push(50);
s1.push(60); s1.display();
54 | P a g e
05413702022 Deepanshi Mendiratta
PRACTICAL NO – 36
import java.io.IOException;
import java.io.InputStreamReader;
super(message);
}}
super(message);
}}
class SavingsAccount {
this.accountNumber = accountNumber;
this.balance = initialBalance;
balance += amount;
55 | P a g e
05413702022 Deepanshi Mendiratta
}balance -= amount;
return balance;
}}
try {
account.depositAmount(depositAmount);
56 | P a g e
05413702022 Deepanshi Mendiratta
account.withdrawAmount(withdrawAmount);
} catch (IOException e) {
} catch (NumberFormatException e) {
} finally {
try {
if (reader != null) {
reader.close();
} } catch (IOException e) {
}}
if (account != null) {
} }}
PRACTICAL NO – 37
57 | P a g e
05413702022 Deepanshi Mendiratta
Ques 37) Write relevant programs to show the concept of creating Thread
through thread class and Runnable interface. WAP to print multiples of
2,3,4 simultaneously.
58 | P a g e