0% found this document useful (0 votes)
8 views45 pages

Java Lab File 2023 24

The document outlines the Java Lab curriculum for the B. Tech IV Semester at Geetanjali Institute of Technical Studies for the academic year 2023-2024. It includes a list of experiments focusing on various Java programming concepts such as finding the greatest and smallest numbers, creating a mini calculator, and implementing classes and interfaces. Each experiment contains an aim, code, and expected output, providing a comprehensive guide for students to enhance their programming skills.

Uploaded by

udaicool2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views45 pages

Java Lab File 2023 24

The document outlines the Java Lab curriculum for the B. Tech IV Semester at Geetanjali Institute of Technical Studies for the academic year 2023-2024. It includes a list of experiments focusing on various Java programming concepts such as finding the greatest and smallest numbers, creating a mini calculator, and implementing classes and interfaces. Each experiment contains an aim, code, and expected output, providing a comprehensive guide for students to enhance their programming skills.

Uploaded by

udaicool2027
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Geetanjali Institute of Technical Studies

(Approved by AICTE, New Delhi and Affiliated to Rajasthan Technical University Kota (Raj.))

DABOK, UDAIPUR, RAJASTHAN 313022

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


B. Tech - IV SEMESTER

ACADEMIC YEAR – 2023-2024

JAVA LAB
4CS4-25

Submitted To: Submitted By:


Dr. Mayank Patel (Student Name)
Mr. Sandeep Bordia (Roll No)
(Section)
INDEX

Date of Date of Teacher's


S. No. Experiment
Perform Submission Signature
1. Write a Java program to find the greatest
and smallest of 3 numbers
2. Write a program in Java to find the average
and percentage of marks obtained by a
student in five papers.
3. Write a menu driven program to design a
mini calculator with all arithmetic
operations
4. Write a program to check whether a given
number is a prime number or not.
5. Write a program to print all Armstrong
numbers between 1 to 1000
6. Write a program in Java with class
Rectangle with the data fields width,
length, area and color. The length, width
and area are of double type and color is of
string type. The methods are set_ length ( )
, set_width ( ), set_ color( ), and find_ area
( ). Create two object of Rectangle and
compare their area and color. If area and
color both are same for the objects then
display “Matching Rectangles” otherwise
display “Non matching Rectangle”.
7. Write a program in Java to create a Player
class. Classes Cricket _Player, Football
_Player and Hockey_ Player inherit Player
class. The basic players details (Name,
address , … etc) are mentioned in Player
class and their sport details and
achievements are mention in respective
classes. Display all the details of those
players who score century in cricket and
score more than 10 goals in hockey.
8. Consider trunk calls of a telephone
exchange. A trunk call can be ordinary,
urgent or lightning call. The charges
depend on the duration and the type of the
call. Write a program-using concept of
polymorphism in Java to calculate the
charges
9. Write a program to create interface named
Shape. In this interface the member
function is square and circle. Implement

Department of CSE (Student Name, Roll No) Page


|2
this interface in Area class. Create one new
class called Menstruation, in this class use
the object of arithmetic class to calculate
area of square and circle.
10. Write a program in java which implement
interface Student which has two methods
Display_Grade and Atrendance for
PG_Students and UG_Students(
PG_Students and UG_Students are two
different classes for Post Graduate and
Under Graduate students respectively).
11. Write a program to make a package
Balance in which has Account class with
Display_Balance method in it. Import
Balance package in another program to
access Display_Balance method of
Account class.
12. Write program in Java for String handling
which perform followings
i) Read the mail-id and check
whether it is a perfect format or
not.
ii) Read the password and check
whether is follows the rules or not.
(Rule :- A password at least be of
6 chars, at least 1 small alphabet,
at least 1 Caps alphabet, at least 1
digit, at least 1 symbol
from(*,_,@).
13. Write a program in Java to display name
and roll number of students. Initialize
respective array variables for 10 students.
Handle ArrayIndexOutOfBoundsExeption,
so that any such problem doesn’t cause
illegal termination of program.

14. Write a program to throw an exception


when customer tries to debit rupees from
his bank account if the available balance is
lower than 1000 rupees.
15. Write a java program to create five threads
with different priorities. Send two threads
of highest priority in sleep state. Check the
aliveness of the threads and mark which
dread is long listing
16. Write a program to create two threads. In
this class we have inherit one Interface
which is used to start the thread and run it.

Department of CSE (Student Name, Roll No) Page


|3
And display all the states of those two
threads.
17. Write a java program to read the file
content and find the total number of
characters, words and Line in that file.
18. Develop an applet that displays “Geetanjali
Institute of technical Studies”
19. Write a java program that Develop an
applet that receives an integer in one text
field, and checks whether is prime number
or not and returns yes or no it in another
text field, when the button named “Submit”
is clicked.
Experiments Beyond Syllabus
20. Write Java program to implement stack
using Java.
21. Write a java program to create a linked list
with all the operations such as insert at
front, insert at position, insert at last, delete
at first, delete at position, delete at last,
traverse, count etc.
22. Design an Applet for Student Record
Management.

Department of CSE (Student Name, Roll No) Page


|4
Experiment – 1
AIM: Write a Java program to find the greatest and smallest of 3 numbers.
CODE:
public class greatest_of_three
{
public static void main(int a,int b,int c)
{
int d=0;
int e=0;
if (a>b && a>c)
d=a;
if (b>a && b>c)
d=b;
if (c>a && c>b)
d=c;
if (a<b && a<c)
e=a;
if (b<a && b<c)
e=b;
if (c<a && c<b)
e=c;
[Link]("Among the inputs done i.e. "+a+","+b+","+c);
[Link]("The greatest number = "+d);
[Link]("The smallest number = "+e);
}
}

Input :

a=3
b=2
c=1

OUTPUT :

Among the inputs done i.e. 3,2,1


The greatest number = 3
The smallest number = 1

Department of CSE (Student Name, Roll No) Page | 5


Experiment – 2
AIM: Write a program in Java to find the average and percentage of marks obtained by a student in five papers.
CODE:
import [Link];

public class Totalof5Papers {


private static Scanner sc;
public static void main(String[] args)
{
int totalPapers, i;
float Marks, total = 0, Percentage, Average;
sc = new Scanner([Link]);

[Link](" Please Enter the Total Number of Papers : ");


totalPapers = [Link]();

[Link](" Please Enter the Papers Marks : ");


for(i = 0; i < totalPapers; i++)
{
Marks = [Link]();
total = total + Marks;
}

Average = total / totalPapers;


Percentage = (total / (totalPapers * 100)) * 100;
[Link](" Total Marks = " + total);
[Link](" Average Marks = " + Average);
[Link](" Marks Percentage = " + Percentage);
}
}

OUTPUT:

Please Enter the Total Number of Papers: 5


Please Enter the 5 Papers marks: 70 85 90 86 84
Total Marks= 415
Average Marks= 83.00
Marks Percentage=83.00

Department of CSE (Student Name, Roll No) Page | 6


Experiment – 3
AIM: Write a menu driven program to design a mini calculator with all arithmetic operations
CODE:
import [Link].*;

class calculator
{
public static void main(String a[])
{
int num1=0,num2=0,option,ex;
do
{
Scanner sc = new Scanner([Link]);
[Link]("Enter your choice from the following menu:");
[Link]("[Link] [Link] [Link] [Link] [Link]");
option = [Link]();
if(option!=5){
[Link]("Enter the first number");
num1=[Link]();
[Link]("Enter the second number");
num2=[Link]();
}
else
break;
switch(option)
{
case 1:[Link]("Addition of "+num1+" and "+num2+" is "+(num1+num2));
break;
case 2:[Link]("Subtraction of "+num1+" and "+num2+" is "+(num1-num2));
break;
case 3:[Link]("Addition of "+num1+" and "+num2+" is "+(num1+num2));
break;
case 4: if(num2==0)
[Link]("Error!!! In Division denominator cannot be 0!");
else{
[Link]("In division of "+num1+" by "+num2+" quotient is "+(num1/num2)+" and remainder is
"+(num1%num2));}
break;
case 5: break;
default: [Link]("Invalid choice");
}
[Link]("Do you want to continue?[Link] [Link]");
ex=[Link]();
}while(ex==1);
}
}

Department of CSE (Student Name, Roll No) Page | 7


OUTPUT:
Enter your choice from the following menu:
1. Addition 2. Subtraction 3. Multiplication 4. Division [Link]
1
Enter the first number
5
Enter the second number
9
Addition of 5 and 9 is 14
Do you want to continue? [Link] [Link]
2

Department of CSE (Student Name, Roll No) Page | 8


Experiment – 4
AIM: Write a program to check whether a given number is a prime number or not.

CODE:
public class Prime {

public static void main(String[] args) {

int num = 29;


boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}

if (!flag)
[Link](num + " is a prime number.");
else
[Link](num + " is not a prime number.");
}
}

OUTPUT:
29 is a prime number.

Department of CSE (Student Name, Roll No) Page | 9


Experiment – 5
AIM: Write a program to print all Armstrong numbers between 1 to 1000.
CODE:
public class ArmstrongNumberDemo
{
public static void main(String[] args)
{
int number, temp, total = 0;
[Link]("Armstrong number between 1 to 1000 : ");
for(int a = 1; a <= 1000; a++)
{
number = a;
while(number > 0)
{
temp = number % 10;
total = total + (temp * temp * temp);
number = number / 10;
}
if(total == a)
{
[Link](a + " ");
}
total = 0;
}
}
}

OUTPUT:
Armstrong number between 1 to 1000 : 1 153 370 371 407

Department of CSE (Student Name, Roll No) Page | 10


Experiment – 6
AIM: Write a program in Java with class Rectangle with the data fields width, length, area and [Link] length,
width and area are of double type and color is of string type. The methods are set_ length ( ) , set_width ( ), set_
color( ), and find_ area ( ). Create two object of Rectangle and compare their area and color. If area and color
both are same for the objects then display “Matching Rectangles” otherwise display “Non matching Rectangle”.
CODE:
import [Link].*;
class rect
{
int width,length;
String color;
void set_length(int a)
{ length=a; }
void set_width(int a)
{ width=a; }
void set_color(String a)
{ color=a; }
int area()
{ return(width*length); }
String getcolor()
{ return(color); }
}
class s03_01
{
public static void main(String arg[])throws Exception
{
String s=null;
DataInputStream in=new DataInputStream([Link]);
rect a=new rect();
[Link]("Enter the length for first Rectangle=");
s=[Link]();
a.set_length([Link](s));
[Link]("Enter the width for first Rectangle=");
s=[Link]();
a.set_width([Link](s));
[Link]("Enter the Color for first Rectangle=");
a.set_color([Link]());
rect b=new rect();
[Link]("Enter the length for second Rectangle=");
s=[Link]();
b.set_length([Link](s));
[Link]("Enter the width for second Rectangle=");
s=[Link]();
b.set_width([Link](s));
[Link]("Enter the Color for second Rectangle=");
b.set_color([Link]());
if([Link]()==[Link]() && [Link]().equals([Link]()))
[Link]("Matching Rectangle ");
else
[Link]("Non Matching Rectangle ");
}
}

Department of CSE (Student Name, Roll No) Page | 11


OUTPUT:

Enter the length for first Rectangle= 13

Enter the width for first Rectangle= 12

Enter the Color for first Rectangle= Red

Enter the length for second Rectangle= 13

Enter the width for second Rectangle= 12

Enter the Color for second Rectangle= Red

Matching Rectangle

Department of CSE (Student Name, Roll No) Page | 12


Experiment – 7
AIM: Write a program in Java to create a Player class. Classes Cricket _Player, Football _Player and Hockey_
Player inherit Player class. The basic players details (Name, address , … etc) are mentioned in Player class and
their sport details and achievements are mention in respective classes. Display all the details of those players
who score century in cricket and score more than 10 goals in hockey.

CODE:
import [Link].*;
import [Link].*;
class player {
String name;
int age;
String address;
}
class cricket_player extends player {
int total_runs;
int century;
public void set()
{
Scanner s=new Scanner([Link]);
[Link]("Enter cricketer details:-");
[Link]("Enter name:-");
name=[Link]();
[Link]("Enter address:-");
address=[Link]();
[Link]("Enter age:-");
age=[Link]();
[Link]("Enter total runs:-");
total_runs=[Link]();
[Link]("Enter century:-");
century=[Link]();
}
}
class football_player extends player
{
int total_goals;
public void set()
{
Scanner s=new Scanner([Link]);
[Link]("Enter football details:-");
[Link]("Enter name:-");
name=[Link]();
[Link]("Enter address:-");
address=[Link]();
[Link]("Enter age:-");
age=[Link]();
[Link]("Enter total goals:-");
total_goals=[Link]();
}
}
class hockey_player extends player {
int total_goals;
public void set()
{
Scanner s=new Scanner([Link]);
[Link]("Enter hockey details:-");

Department of CSE (Student Name, Roll No) Page | 13


[Link]("Enter name:-");
name=[Link]();
[Link]("Enter address:-");
address=[Link]();
[Link]("Enter age:-");
age=[Link]();
[Link]("Enter total goals:-");
total_goals=[Link]();
}
}
class display
{
void pr(cricket_player x, hockey_player y)
{
if([Link]>1 && y.total_goals>10)
{
[Link]([Link] +" "+ [Link] +" "+ [Link] +" "+ [Link]);
[Link]([Link] +" "+ [Link] +" "+ [Link] +" "+ y.total_goals);
}
}
}

class player_score{
public static void main(String args[] )
{
cricket_player c = new cricket_player();
football_player f = new football_player();
hockey_player h = new hockey_player();
display d =new display();
[Link]();
[Link]();
[Link]();
[Link](c,h);
}
}

OUTPUT:

Enter cricketer details:-


Enter name:- Dr. Mayank Patel
Enter address:-Pratap Nagar
Enter age:-34
Enter total runs:- 5000
Enter century:- 10
Enter football details:-
Enter name:- xyz
Enter address:- jaipur
Enter age:- 25
Enter total goals:- 223
Enter hockey details:-
Enter name:- Raj
Enter address:- udaipur
Enter age:- 20
Enter total goals:- 250

Dr. Mayank PatelPratap Nagar34 Raj udaipur 20 250

Department of CSE (Student Name, Roll No) Page | 14


Experiment – 8
AIM: Consider trunk calls of a telephone exchange. A trunk call can be ordinary, urgent or lightning call. The
charges depend on the duration and the type of the call. Write a program-using concept of polymorphism in Java
to calculate the charges.

CODE:
import [Link].*;
class Calls{
float dur;
String type;
float rate(){
if([Link]("urgent"))
return4.5f;
else
if(type=="lightning")
return3.5f;
else
return 3f;
}
}

class Bill extends Calls{


float amount;
void read(){
Scanner input=new Scanner([Link]);
[Link]("Enter Call Type(urgent,lightning,ordinary):
"); type=[Link]();
[Link]("Enter Call duration:");
dur=[Link]();
}
void calculate(){
if(dur<=1.5){
amount=rate()*dur+1.5f;
}
else if(dur<=3){
amount=rate()*dur+2.5f;
}
else if(dur<=5){
amount=rate()*dur+4.5f;
}
else{
amount=rate()*dur+5f;
}
}
void print(){
[Link](" Call Type : "+type);
[Link](" Duration : "+dur);
[Link](" Charge: "+amount);
}
}

Department of CSE (Student Name, Roll No) Page | 15


class TelephoneExchange{
public static void main(String
arg[]){ Bill b=new Bill();
[Link]();
[Link]();
[Link]();
}
}

OUTPUT:

Enter Call Type(urgent,lightning,ordinary): ordinary


Enter Call duration:23
Call Type : ordinary
Duration : 23.0
Charge: 74.0
Experiment – 9
AIM: Write a program to create interface named Shape. In this interface the member function is square and
circle. Implement this interface in Area class. Create one new class called Mensuration, in this class use the
object of arithmetic class to calculate area of square and circle.

CODE:
import [Link];
interface shape{
void square();
void circle();
}

class area implements shape


{
int side, r;
double pi = 3.14,area;
Scanner s = new Scanner([Link]);
public void circle()
{
[Link]("Enter radius of circle:");
r = [Link]();
area = pi * r *r;
[Link]("Area of circle:"+area);
}
public voidsquare()
{
[Link]("Enter side ofsquare:");
side =[Link]();
area = side * side;
[Link]("Perimeter of rectangle:"+area);
}

public static void main(String[] args)


{
area obj new area();
[Link]();
[Link]();
}
}

OUTPUT:

Enter radius of circle:23


Area of circle:1661.06
Enter side of square:12
Area of square:144.0
Experiment – 10
AIM: Write a program in java which implement interface Student which has two methods Display_Grade
and Attendance for PG_Students and UG_Students( PG_Students and UG_Students are two different
classes for Post Graduate and Under Graduate students respectively).
CODE:
interface Student
{
void Display_Grade();
void Display_Atten();
}
class PG_Student implements Student
{
String name, grade;
int m1, m2, m3, attendence, total;
PG_Student(String name, int m1, int m2, int m3, int attendence)
{
[Link] = name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
[Link] = attendence;
}
void Display()
{
[Link]("Name is " + name);
[Link]("Marks are " + m1 + " " + m2 + " " + m3);
}
public void Display_Atten()
{
[Link]("The attendence is " + attendence);
}
public void Display_Grade()
{
total = m1 + m2 + m3;
if (total > 250)
{
grade = "A";
}
else if (total < 250)
{
grade = "B";
} else if (total < 200)
{
grade = "C";
}
else
{
grade = "D";
}
[Link]("The Grade is " + grade);
}
}
class UG_Student implements Student
{
String name, grade;
int m1, m2, m3, attendence, total;
UG_Student(String name, int m1, int m2, int m3, int attendence)
{
[Link] = name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
[Link] = attendence;
}
void Display()
{
[Link]("Name is " + name);
[Link]("Marks are " + m1 + " " + m2 + " " + m3);
}
public void Display_Atten()
{
[Link]("The attendence is " + attendence);
}
public void Display_Grade()
{
total = m1 + m2 + m3;
if (total > 300)
{
grade = "S";
}
else if (total > 250)
{
grade = "A";
}
else if (total < 250)
{
grade = "B";
}
else if (total < 200)
{
grade = "C";
}
else
{
grade = "D";
}
[Link]("The Grade is " + grade);
}
}
class StudentDemo {
public static void main(String[] args) {
PG_Student pg = new PG_Student("Alex", 45, 68, 47, 35);
[Link]();
pg.Display_Atten();
pg.Display_Grade();
UG_Student ug = new UG_Student("Aman", 95, 88, 77, 25);
[Link]();
ug.Display_Atten();
ug.Display_Grade();
}
}

OUTPUT:
Name is Alex
Marks are 45 68 47
The attendence is 35
The Grade is B
Name is Aman
Marks are 95 88 77
The attendence is 25
The Grade is A
Experiment – 11
AIM: Write a program to make a package Balance in which has Account class with Display_Balance
method in it. Import Balance package in another program to access Display_Balance method of Account
class.

CODE:
class s05_01
{
public static void main(String ar[])
{
try
{
[Link] a=new [Link]();
[Link]();
[Link]();
}
catch(Exception e)
{ [Link](e); }
}
}
package balance;
import [Link].*;
public class account
{
long acc,bal;
String name;
public void read()throws Exception
{
DataInputStream in=new DataInputStream([Link]);
[Link]("Enter the name :");
name=[Link]();
[Link]("Enter the account number :");
acc=[Link]([Link]());
[Link]("Enter the account balance :");
bal=[Link]([Link]());
}
public void disp()
{
[Link]("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
[Link]("--- Account Details ---");
[Link]("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
[Link]("Name :"+name);
[Link]("Account number :"+acc);
[Link]("Balance :"+bal);
}
}
OUTPUT:
Enter the name: Aryan
Enter the account number: 94568123
Enter the account balance: 5000
---Account Details-----
Name: Aryan
Account number : 94568123
Balance :5000
Experiment – 12
AIM: Write program in Java for String handling which perform followings
i) Read the mail-id and check whether it is a perfect format or not.
i) Read the password and check whether is follows the rules or not. (Rule :- A password at least be of 6
chars, at least 1 small alphabet, at least 1 Caps alphabet, at least 1 digit, at least 1 symbol from(*,_,@).
CODE:
check whether it is a perfect format or not
import [Link];
import [Link];

class Test
{
public static boolean isValid(String email)
{
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
"[a-zA-Z0-9_+&*-]+)*@" +
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
"A-Z]{2,7}$";

Pattern pat = [Link](emailRegex);


if (email == null)
return false;
return [Link](email).matches();
}

/* driver function to check */


public static void main(String[] args)
{
String email = "contribute@[Link]";
if (isValid(email))
[Link]("Yes");

else
[Link]("No");
}
}

OUTPUT:
Email id Verified
Yes
Read the password and check whether is follows the rules or not.

import [Link];
import [Link].*;

public class PasswordValidatorTest {

private PasswordValidator passwordValidator;

@BeforeClass
public void initData(){
passwordValidator = new PasswordValidator();
}

@DataProvider
public Object[][] ValidPasswordProvider() {
return new Object[][]{
{new String[] {
" Char2@ ", "mkYOn12$",
}}
};
}

@DataProvider
public Object[][] InvalidPasswordProvider() {
return new Object[][]{
{new String[] {
"HELLO1", “ myc@”;
}}
};
}

@Test(dataProvider = "ValidPasswordProvider")
public void ValidPasswordTest(String[] password) {

for(String temp : password){


boolean valid = [Link](temp);
[Link]("Password is valid : " + temp + " , " + valid);
[Link](true, valid);
}

@Test(dataProvider = "InvalidPasswordProvider",
dependsOnMethods="ValidPasswordTest")
public void InValidPasswordTest(String[] password) {

for(String temp : password){


boolean valid = [Link](temp);
[Link]("Password is valid : " + temp + " , " + valid);
[Link](false, valid);
}
}
}
OUTPUT:
Password is valid : “Char2@” , true
Password is valid : “ mkYOn12$", true
Password is invalid : myc@
Password is invalid : HELLO1
Experiment – 13
AIM: Write a program in Java to display name and roll number of students. Initialize respective array
variables for 10 students. Handle ArrayIndexOutOfBoundsExeption, so that any such problem doesn’t cause
illegal termination of program.

CODE:
import [Link].*;
class student
{
String name,grade; int reg,m1,m2,m3;
void read()throws Exception
{
DataInputStream in= new DataInputStream([Link]);
[Link]("enter the register no : ");
reg=[Link]([Link]());
[Link]("enter the name : ");
name=[Link]();
[Link]("enter mark1 : ");

m1=[Link]([Link]());
[Link]("enter mark2 : ");
m2=[Link]([Link]());
[Link]("enter mark3 : ");
m3=[Link]([Link]());
}
public void disp_grade()
{
int tt=m1+m2+m3;
if(tt>=250) grade="A";
else if(tt>=200) grade="B";
else if(tt>=150) grade="C";
else if(tt>=100) grade="D";
else grade="E";
[Link]("Grade :"+grade);
}
void disp()
{
[Link]("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
[Link](" MARK LIST OF STUDENTS ");
[Link]("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
[Link]("Register No :"+reg);
[Link]("Name :"+name);
[Link]("Mark1 :"+m1);
[Link]("Mark2 :"+m2);
[Link]("Mark3 :"+m3);
disp_grade();
}
}
class s06_01
{
public static void main(String ar[])
{
int no=0;
student s=new student(); try
{
DataInputStream in= new DataInputStream([Link]); [Link]("enter the number of students :
"); no=[Link]([Link]());
for(int i=0;i<no;i++); [Link]();
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("the maximum students should be ten\n"); no=10;
}
catch(Exception e)
{ [Link](e);
}
for(int i=0;i<no;i++);
[Link]();
}
}

OUTPUT:
enter the number of students :
11
enter the roll no :
12
enter the name :
nbv
enter the roll no :
12
enter the name :
2bnb
enter the roll no :
312n
Exception in thread "main" [Link]: For input string: "312n"
Experiment – 14

AIM: Write a program to throw an exception when customer tries to debit rupees from his bank account if
the available balance is lower than 1000 rupees.

CODE:
class myException2 extends Exception
{
int acc_bal=0; //current account balance

public String toString()


{
return "Cannot debit. Low Account Balance.";
}
public static void main(String... ar)
{
myException2 ob= new myException2();
try
{
[Link](100);
}
catch(myException2 e)
{
[Link]("Exception handled - "+ e);
}
}

public void withdraw(int debitMoney) throws myException2


{
if(acc_bal>1000) //if account balance is greater than 1000 then you can dedit money
{
acc_bal = acc_bal - debitMoney;
}
else // if account balance is less than 1000 then debiting throws our user defined exception.
throw new myException2();
}
}

Output:
Exception handled - Cannot debit. Low Account Balance
Experiment – 15
AIM: Write a java program to create five threads with different priorities. Send two threads of highest
priority in sleep state. Check the aliveness of the threads and mark which thread is long listing.

CODE:
public class Five Threads implements Runnable
{
public static void main (String args[]) throws Interrupted Exception
{
Thread T1=new Thread ();
Thread T2=new Thread ();
Thread T3=new Thread ();
Thread T4=new Thread ();
Thread T5=new Thread ();
T1. set Priority (7);
[Link](2);
[Link](10);
[Link](5);
[Link](8);
[Link](1500);
if ([Link]())
[Link]("Thread 1 is alive");
else
[Link]("Thread 1 is not alive");
[Link]();
if ([Link]())
[Link]("Thread 2 is alive");
else
[Link]("Thread 2 is not alive");
[Link](1000);
if ([Link]())
[Link]("Thread 3 is alive");
else
[Link]("Thread 3 is not alive");
[Link]();
if ([Link]())
[Link]("Thread 4 is alive");
else
[Link]("Thread 4 is not alive");
[Link]();
if ([Link]())
[Link]("Thread 5 is alive");
else
[Link]("Thread 5 is not alive");
}
}
OUTPUT:

C:\jdk-12.0.2\bin>java Multi3
Thread 1 is alive
Exception in thread "main" [Link]
at [Link]/[Link]([Link])
at [Link]([Link])
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
4
4
4
4
4
Experiment – 16
AIM: Write a program to create two threads. In this class we have inherit one Interface which is used to
start the thread and run it. And display all the states of those two threads.
CODE:
import [Link].*; import [Link].*;

interface Runnable{ public voidrun();


}
class de implements Runnable
{
public voidrun()
{
[Link]("MyRunnable running");
}

public static void main(String args[])


{
de t1 = new de();
de t2 = new de(); [Link]();
[Link]();
}
}

OUTPUT:
MyRunnable running
MyRunnable running
Experiment – 17
AIM: Write a java program to copy a file into another file. During copy the file content total number of
alphabet, numeric digits, special symbols and word has to display.
CODE:
import [Link];
import [Link];
import [Link];

public class WordCountInFile


{
public static void main(String[] args)
{
BufferedReader reader = null;

//Initializing charCount, wordCount and lineCount to 0

int charCount = 0;

int wordCount = 0;

int lineCount = 0;

try
{
//Creating BufferedReader object

reader = new BufferedReader(new FileReader("C:\[Link]"));

//Reading the first line into currentLine

String currentLine = [Link]();

while (currentLine != null)


{
//Updating the lineCount

lineCount++;

//Getting number of words in currentLine

String[] words = [Link](" ");

//Updating the wordCount

wordCount = wordCount + [Link];

//Iterating each word

for (String word : words)


{
//Updating the charCount
charCount = charCount + [Link]();
}

//Reading next line into currentLine

currentLine = [Link]();
}

//Printing charCount, wordCount and lineCount

[Link]("Number Of Chars In A File : "+charCount);

[Link]("Number Of Words In A File : "+wordCount);

[Link]("Number Of Lines In A File : "+lineCount);


}
catch (IOException e)
{
[Link]();
}
finally
{
try
{
[Link](); //Closing the reader
}
catch (IOException e)
{
[Link]();
}
}
}
}

Output :
Experiment – 18
AIM: Develop an applet that displays “Geetanjali Institute of technical Studies”
CODE:
package Applet;

import [Link].*;
import [Link].*;

public class WelcomeToApplet extends Applet


{
// create paint class to print on the screen.
public void paint (Graphics g)
{
// Enter message with co-ordinates to make it in centre.
[Link] ("Geetanjali Institute of Technical Studies", 25, 50);

}
}

OUTPUT:
Experiment – 19
AIM: Write a java program that Develop an applet that receives an integer in one text field, and checks
whether is prime number or not and returns yes or no it in another text field, when the button named
“Submit” is clicked.

CODE:
/*<APPLET CODE=[Link] WIDTH=300 HEIGHT=300></APPLET>*/
import [Link].*;
import [Link].*;
import [Link].*;
public class prime extends Applet implements ActionListener
{
TextField t1,t2;
Button b1;
Label l1,l2
GridLayout g1;
public void init()
{
t1 = new TextField();
t2 = new TextField();
b1 = new Button("Submit");
l1 = new Label("Enter Number");
l2 = new Label("Result is");
g1 = ne GridLayout(3,2);
add(l1);add(t1);
add(l2);add(t2);
add(b1);
setBackground([Link]);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
int n = [Link]([Link]());
int f=0,i;
for(i=2;i<n;i++)
{
if(n%2==0)
{
f++;break;
}
}
if(f==0)
{
[Link]("It is Prime Number");
}
else
{
[Link]("It is Not Prime Number");
}
}
}
OUTPUT:
EXPERIMENTS BEYOND RTU SYLLABUS

Experiment – 20
AIM: Write Java program to implement stack using Java.

CODE:
import [Link].*;
import [Link].*;

class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
[Link](i);
}
}

// Popping element from the top of the stack


static void stack_pop(Stack<Integer> stack)
{
[Link]("Pop Operation:");

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


{
Integer y = (Integer) [Link]();
[Link](y);
}
}

// Displaying element on the top of the stack


static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) [Link]();
[Link]("Element on stack top: " + element);
}

// Searching element in the stack


static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) [Link](element);

if(pos == -1)
[Link]("Element not found");
else
[Link]("Element is found at position: " + pos);
}
public static void main (String[] args)
{
Stack<Integer> stack = new Stack<Integer>();

stack_push(stack);
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
Element is found at position: 3
Element not found
Experiment – 21
AIM: Write Java program to implement Linked List using Java.

CODE:
public class SinglyLinkedList {
//Represent a node of the singly linked list
class Node{
int data;
Node next;

public Node(int data) {


[Link] = data;
[Link] = null;
}
}

//Represent the head and tail of the singly linked list


public Node head = null;
public Node tail = null;

//addNode() will add a new node to the list


public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);

//Checks if the list is empty


if(head == null) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
[Link] = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


public void display() {
//Node current will point to head
Node current = head;

if(head == null) {
[Link]("List is empty");
return;
}
[Link]("Nodes of singly linked list: ");
while(current != null) {
//Prints each node by incrementing pointer
[Link]([Link] + " ");
current = [Link];
}
[Link]();
}

public static void main(String[] args) {

SinglyLinkedList sList = new SinglyLinkedList();

//Add nodes to the list


[Link](11);
[Link](22);
[Link](33);
[Link](44);

//Displays the nodes present in the list


[Link]();
}
}

Output:

11 22 33 44
Experiment – 22
AIM: Design an Applet for Student Record Management.
CODE:

import [Link].*;
import [Link].*;
import [Link];
//import [Link].*;
public class cardlayout1 extends Frame implements ActionListener
{
Panel mainp,p1,p2,p3,btp;
GridLayout gi,gv,gd;
CardLayout cl1;
Label l1,l2,l3,l4 ,l5,l6,l7,l8;
TextField t1,t2,t3,t4,t5,t6,t7;
Button b1,b2,b3,b4,b5,b6,b7;

//Construtor initilzation of awt components


cardlayout1()
{
//--------LABEl------------------
//Label for insert panel
l1 = new Label("Enter Roll No. : ");
l2 = new Label("Enter Name :");
l3 = new Label("Enter Branch :");
l4 = new Label("Insert your Entry");
//Label for view panel
l5= new Label("Enter Roll No.");
l6= new Label("Name :");
l7 = new Label("Branch :");
//Label for delete panel
l8 = new Label("Enter Roll No. to delete");

//-----------TEXTFIELD----------------
//TextFeld for insert panel
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
//TextFeldfor view panel
t4 = new TextField(10);
t5 = new TextField(10);
t6 = new TextField(10);
//TextFeldfor delete panel
t7 = new TextField(10);

//----------BUTTON-------------------
b1 = new Button("Reset");
b2 = new Button("Submit");
b3 = new Button("View");
b4 = new Button("Delete");

b5 = new Button("INSERT");
b6 = new Button("SHOW");
b7 = new Button("DELETE");

[Link](this);
[Link](this);
[Link](this);
}

// Show Method
public void show1()
{
//main Pannel
mainp = new Panel();
cl1= new CardLayout();
[Link](cl1);

//insert pannel
gi = new GridLayout(5,2,10,10); // for insert
p1 = new Panel(gi);
[Link]([Link]);
[Link](l1); [Link](t1);
[Link](l2); [Link](t2);
[Link](l3); [Link](t3);
[Link](b1); [Link](b2);
[Link](l4);

//view Pannel

gv = new GridLayout(4,2,10,10); // for view


p2 = new Panel(gv);
[Link]([Link]);
[Link](l5); [Link](t4);
[Link](l6); [Link](t5);
[Link](l7); [Link](t6);
[Link](b3);

//delete Pannel
gd = new GridLayout(2,2,10,10); //for delete
p3 = new Panel(gd);
[Link]([Link]);
[Link](l8); [Link](t7);
[Link](b4);

//button Pannel
btp = new Panel();
[Link](b5);
[Link](b6);
[Link](b7);

// Main panel comprising of Insert,view and delete pannel


[Link](p1,"I");
[Link](p2,"V");
[Link](p3,"D");

//adding Main pannel and Button pannel in BorderLayout


setLayout( new BorderLayout());
add(btp,[Link]);
add(mainp,[Link]);

//Frame Methods
setTitle("Students Record Managment");
setSize(500,500);
setVisible(true);

//For closing the frame //#### Anonymous Class####


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
[Link](0);
}
}
);
}
//Event Handling Method for displaying Panel
public void actionPerformed(ActionEvent a1)
{
if([Link]()== b5)
{ [Link](mainp,"I");
}
if([Link]()== b6)
{ [Link](mainp,"V");
}
if([Link]()== b7)
{ [Link](mainp,"D");
}
}
public static void main(String[] args)
{ cardlayout1 c1 = new cardlayout1();
c1.show1();
}
}

Output:
RUBRICS EVALUATION

Performance Scale 1 Scale 2 Scale 3 Scale 4 Score


Criteria (0-25%) (26-50%) (51-75%) (76-100%) (Numerical
)

Understandability Unable to Able to Able to Able to understand


understand understand understand the problem
Ability to analyse the the problem the problem completely
Problem and Identify problem. partially and completely but and able to
solution unable to unable to provide
identify the identify the alternative
solution solution solution too.
Logic Program Program logic is Program logic Program logic is
logic is on the right is mostly correct, with no
Ability to specify incorrect track but has correct, but may known boundary
Conditions & control several errors contain errors, and no
flow that are appropriate an occasional redundant or
for the boundary error contradictory
or redundant or conditions.
problem domain.
contradictory
condition.
Debugging Unable to Unable to debug Able to execute Able to execute
Ability to execute execute several errors. program with program
/debug program several completely
warnings.
Correctness Program Program Program Program produces
Ability to code formulae does not approaches produces correct answers or
and algorithms that produce correct answers correct answers appropriate results
reliably produce correct correct or appropriate or appropriate for all inputs
answers or appropriate answers or results for most results for most tested.
results. appropriate inputs, but can inputs.
results for contain
most inputs. miscalculations
in some cases.
Completeness Unable to Unable to Able to explain Able to explain
Ability to demonstrate explain the explain the code code and the code and the
and deliver on time. [Link] and the code program was program was
the code was submission was delivered within delivered on time.
overdue. late. the due date.
TOTAL

You might also like