New 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 77

JAVA PROGRAMMING

Q1. Write a Program to Print Your Name and Address.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a Program to Print Your Name and Address.
Class: SYBCA
Div: 2
Date: 6-12-22
*********************************************************/
class info
{
public static void main(String args[])
{
System.out.println("Name: " + args[0]);
System.out.println("Address: " + args[1]);
}
}

/**************************OUTPUT***********************

Purva
Jani

*******************************************************/

Purva Jani [68] SYBCA Sem 4 1


JAVA PROGRAMMING

Q2. Write a Program to Calculate Net Salary from Given Basic Salary.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a Program to Calculate Net Salary from Given Basic Salary.
Class: SYBCA
Div: 2
Date: 6-12-22
*********************************************************/
import java.util.Scanner;
public class salary
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter Your basic salary");
int basicsalary=scan.nextInt();
float HRA=(10/100)* basicsalary;
float DA=(73/100)* basicsalary;
float GS=basicsalary+DA+HRA;
float incometax=(30/100)* GS;
float netsalary=GS-incometax;
System.out.println("Net Salary is " + netsalary);
}
}

/**************************OUTPUT***********************

Enter Your basic salary


25000
Net Salary is 25000.0

*******************************************************/

Purva Jani [68] SYBCA Sem 4 2


JAVA PROGRAMMING

Q3. Write a program for use multiple class.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use multiple class.
Class: SYBCA
Div: 2
Date: 6-12-22
*********************************************************/
import java.util.Scanner;
class demo
{
int a,b;
void scan(int n,int m)
{
a=n;
b=m;
}
void show()
{
System.out.println("A="+a);
System.out.println("B="+b);
}
}
class demo1
{
int p=50;
int q=100;
void add1()
{
System.out.println("P+Q="+(p+q));
}
}
class demo2
{
public static void main(String args[])
{
demo d1=new demo();
System.out.println("Display First Object
Details.");

Scanner sc=new Scanner(System.in);


System.out.print("Enter First Number=");
Integer p=sc.nextInt();
System.out.print("Enter Second Number=");
Integer q=sc.nextInt();
d1.scan(p,q);
d1.show();
demo1 d2=new demo1();

Purva Jani [68] SYBCA Sem 4 3


JAVA PROGRAMMING

Syst em.out.println("Display Second Object Details.");

Purva Jani [68] SYBCA Sem 4 4


JAVA PROGRAMMING

d2.add1();
}
}

/**************************OUTPUT***********************

Display First Object Details.


Enter First Number=3
Enter Second Number=7
A=3
B=7
Display Second Object Details.
P+Q=150

*******************************************************/

Purva Jani [68] SYBCA Sem 4 5


JAVA PROGRAMMING

Q4. Write a program for use of command line arguments.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use of command line arguments.Class:
SYBCA
Div: 2
Date: 9-12-22
*********************************************************/
public class CommandLine
{
public static void main(String args[])
{
System.out.println("Your Name is: " + args[0]);
}
}

/**************************OUTPUT***********************

Your Name is: Purva

*******************************************************/

Purva Jani [68] SYBCA Sem 4 6


JAVA PROGRAMMING

Q5. Write a program for read data from keyboard using data input string.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for read data from keyboard using data input string.
Class: SYBCA
Div: 2
Date: 9-12-22
*********************************************************/
import java.io.DataInputStream;
class read1
{
public static void main(String args[])
{
DataInputStream d1=new
DataInputStream(System.in); int number=0;
float floatnumber=0.0f;
try
{
System.out.print("Enter an Integer Number=");
number=Integer.parseInt(d1.readLine());

System.out.print("Enter Float Number=");


floatnumber=Float.valueOf(d1.readLine()).floatValue();
}
catch(Exception e){}
System.out.println(" ");
System.out.println("Integer Number="+number);
System.out.println("Float Number="+floatnumber);
}
}

/**************************OUTPUT***********************

Enter an Integer Number=5


Enter Float Number=3.7

Integer Number=5
Float Number=3.7

*******************************************************/

Purva Jani [68] SYBCA Sem 4 7


JAVA PROGRAMMING

Q6. Write a program for casting of variables.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for casting of variables.
Class: SYBCA
Div: 2
Date: 12-12-22
*********************************************************/
public class TypeCasting
{
public static void main(String[] args)
{
System.out.println("Widening Type Casting");
int m=5;
System.out.println("The int value is " + m);
double n=m;
System.out.println("The double value is " + n);
System.out.println("Narrowing Type Casting");
double a=2;
System.out.println("The double value is " + a);
int b=(int)a;
System.out.println("The int value is " + b);
}
}

/**************************OUTPUT***********************

Widening Type Casting


The int value is 5
The double value is 5.0

Narrowing Type Casting


The double value is 2.0
The int value is 2

*******************************************************/

Purva Jani [68] SYBCA Sem 4 8


JAVA PROGRAMMING

Q7. Write a program to display following patterns.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to display following patterns.
Class: SYBCA
Div: 2
Date: 12-12-22
*********************************************************/
public class pattern
{
public static void main(String[] args)
{
System.out.println("The First Pattern :-");
int k=1;
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j< i + 1; j++)
{
System.out.print( k++ + " ");
}
System.out.println();
}

System.out.println("The Second Pattern :-");


for (int m= 5; m>= 1; m--)
{
for (int n=5; n>m;n--)
{
System.out.print(" ");
}
for (int p=1;p<=m;p++)
{
System.out.print("$");
}
System.out.println("");
}

System.out.println("The Third Pattern :-");


for (int a = 1; a<= 5; a++)
{
for (int b= 1; b <= a; b++)
{
if(b==1 || b== a)
System.out.print("1");
else
System.out.print("0");
}

Purva Jani [68] SYBCA Sem 4 9


JAVA PROGRAMMING

System.out.println();
}
}
}

/**************************OUTPUT***********************

The First Pattern :-


1
23
456
7 8 9 10

The Second Pattern :-


$$$$$
$$$$
$$$
$$
$

The Third Pattern :-


1
11
101
1001
10001

*******************************************************/

Purva Jani [68] SYBCA Sem 4 10


JAVA PROGRAMMING

Q8. Write a program sum of following series.


1+1/2+1/3+1/4...................+1/n
Ans.
/*********************************************************
Name: Purva Jani
Program: Write a program sum of following series. 1+1/2+1/3+1/4...................+1/n
Class: SYBCA
Div: 2
Date: 15-12-22
*********************************************************/
import java.util.Scanner;
public class sum
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter The value of n");
int n=scan.nextInt();
double i,s=0.00;
for(i=1;i<=n;i++)
{
s+=1/i;
}
System.out.println("Sum is:" + (s));
}
}

/**************************OUTPUT***********************

Enter The value of n


5
Sum is: 2.283333333333333

*******************************************************/

Purva Jani [68] SYBCA Sem 4 11


JAVA PROGRAMMING

Q9. Write a program for check arithmetic operators.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for check arithmetic operators.
Class: SYBCA
Div: 2
Date: 15-12-22
*********************************************************/
public class Arithmetic
{
public static void main(String[] args)
{
int a=30, b=20;
System.out.println("Addition is " +( a+b));
System.out.println("Subtraction is " + (a-
b));
System.out.println("Multiplication is " +(a*b));
System.out.println("Division is " + (a/b));
System.out.println("Modulus is " + (a%b));
System.out.println("Increment is " +(a++));
System.out.println("Decrement is " + (a--));
}
}

/**************************OUTPUT***********************

Addition is 50
Subtraction is 10
Multiplication is 600
Division is 1
Modulus is 10
Increment is 30
Decrement is 31

*******************************************************/

Purva Jani [68] SYBCA Sem 4 12


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 13


JAVA PROGRAMMING

Q10. Write a program for relational operators.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for relational operators.
Class: SYBCA
Div: 2
Date: 15-12-22
*********************************************************/
public class relational
{
public static void main(String[] args)
{
System.out.println("the relational operator are as follows:- ");
int a=17, b=25;
System.out.println("The value of a is " + a + " and the value of b is " +b );
System.out.println("Greater than: " + (a>b));
System.out.println("Less than: " + (a<b));
System.out.println("Greater than or equal to:" + (a>=b));
System.out.println("Less than or equal to: " +(a<=b));
System.out.println("Equal to: " + (a==b));
System.out.println("Not Equal to:" + (a!=b));
}
}

/**************************OUTPUT***********************

the relational operator are as follows :-


The value of a is 17 and the value of b is 25

Greater than: false


Less than: true
Greater than or equal to: false
Less than or equal to: true
Equal to: false
Not Equal to: true

*******************************************************/

Purva Jani [68] SYBCA Sem 4 14


JAVA PROGRAMMING

Q11. Write a program to check maximum number from given three number only use
from conditional operator
Ans.
/*********************************************************
Name: Purva Jani
Program: Write a program to check maximum number from given three number only use from
conditional operator.
Class: SYBCA
Div: 2
Date: 15-12-22
*********************************************************/
import java.util.Scanner;
class turnery
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter first number:");
a = sc.nextInt();
System.out.println("Enter Second number:");
b= sc.nextInt();
System.out.println("Enter Third number:");
c= sc.nextInt();
int temp,largest;
temp = a>b?a:b;
largest = temp>c?temp:c;
System.out.println("Largest="+largest);
}
}

/**************************OUTPUT***********************

Enter first number:


30
Enter Second number:
50
Enter Third number:
20
Largest=50

*******************************************************/

Purva Jani [68] SYBCA Sem 4 15


JAVA PROGRAMMING

Q12. Write a program for use of ten mathematical function.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use of ten mathematical
function. Class: SYBCA
Div: 2
Date: 17-12-22
*********************************************************/
import java.util.Scanner;
public class math
{
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=scan.nextInt();
System.out.println("Enter the value of m");
int m=scan.nextInt();
System.out.println("Absolute value of " + n + " is " + Math.abs(n));
System.out.println("Min value of " + n + " is " + Math.min(m,n));
System.out.println("Max value of " + n + " is " + Math.max(m,n));
System.out.println("Power value of " + n + " is " + Math.pow (n,m));
System.out.println("Square root value of " + n + " is " + Math.sqrt(n));
System.out.println("Sin value of " + n + " is " + Math.sin(n));
System.out.println("Cos value of " + n + " is " +Math.cos(n) );
System.out.println("Tan value of " + n + " is " +Math.tan(n) );
System.out.println("Log value of " + n + " is " +Math.log(n) );
System.out.println("Exponential value of " + n + " is " +Math.exp(n)
);
}
}
/**************************OUTPUT***********************

Enter the value of n


12
Enter the value of m
15
Absolute value of 12 is 12
Min value of 12 is 12
Max value of 12 is 15
Power value of 12 is 1.5407021574586368E16
Square root value of 12 is 3.4641016151377544
Sin value of 12 is -0.5365729180004349
Cos value of 12 is 0.8438539587324921
Tan value of 12 is -0.6358599286615808
Log value of 12 is 2.4849066497880004
Exponential value of 12 is 162754.79141900392

Purva Jani [68] SYBCA Sem 4 16


JAVA PROGRAMMING

*******************************************************/

Purva Jani [68] SYBCA Sem 4 17


JAVA PROGRAMMING

Q13. Write a program for find month name from given number using switch case
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for find month name from given number using switch
case Class: SYBCA
Div: 2
Date: 17-12-22
*********************************************************/
import java.util.Scanner;
public class month
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the month number:-");
int number=scan.nextInt();
switch(number)
{
case 1: break; case 9:

break; break;
case
2:

break;
case
3:

break;
case
4:

break;
case
5:

break;
case
6:

break;
case
7:

break;
case
8:

Purva Jani [68] SYBCA Sem 4 18


JAVA PROGRAMMING

System.out.println("The month is April");


System.out.println("The

System.out.println("The month is May");


month is January");

System.out.println("The month is June");


System.out.println("The

System.out.println("The month is July");


month is February");

System.out.println("The month is August");


System.out.println("The

System.out.println("The month is September");


month is March");
case 10:
System.out.println("The month is October");
break;

Purva Jani [68] SYBCA Sem 4 19


JAVA PROGRAMMING

case 11:
System.out.println("The month is November");
break;
case 12:
System.out.println("The month is December");
break;
default:
System.out.println("Invalid month no");
break;
}
}
}

/**************************OUTPUT***********************

Enter the month number :-


8
The month is August

*******************************************************/

Purva Jani [68] SYBCA Sem 4 20


JAVA PROGRAMMING

Q14. Write a program to print table of inputted number only use of do…. while loop.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to print table of inputted number only use of do…. while loop.
Class: SYBCA
Div: 2
Date: 17-12-22
*********************************************************/
class table
{
public static void main(String[] args)
{
int a=Integer.parseInt(args[0]);
int i=1;
do
{
System.out.println(a+ " * " +i+ " = " + (a*i));
i++;
}
while(i<=10);
}
}

/**************************OUTPUT***********************

7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

*******************************************************

Purva Jani [68] SYBCA Sem 4 21


JAVA PROGRAMMING

Q15. Write a program for application of classes and objects


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for application of classes and
objects Class: SYBCA
Div: 2
Date: 20-12-22
*********************************************************/
class info{
int id;
String name;
public void
putdata(){ System.out.println("Id is "+
this.id); System.out.println("Name is "
+ this.name);
}
}
public class Student{
public static void main(String[]
args){info i1=new info();
i1.id=1;
i1.name="Purva";
i1.putdata();
}
}

/**************************OUTPUT***********************

Id is 1
Name is Purva

*******************************************************/

Purva Jani [68] SYBCA Sem 4 22


JAVA PROGRAMMING

Q16. Write a program for use of different types of constructors.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use of different types of constructors
Class: SYBCA
Div: 2
Date: 20-12-22
*********************************************************/
class pqr
{
int a,b;
pqr()
{
a=20;
b=40;
}
pqr(int a,int b)
{
this.a=a;
this.b=b;
}
void display()
{
System.out.println("A="+a);
System.out.println("B="+b);
}
}
class constructor16
{
public static void main(String args[])
{
System.out.println("Default Constructor:");
pqr p1=new pqr();
p1.display();
System.out.println(" ");
System.out.println("Perameterized Constructor:");
pqr p2=new pqr(10,20);
p2.display();
}
}
/**************************OUTPUT***********************
Default Constructor:
A=20
B=40
Perameterized Constructor:
A=10
B=20
*******************************************************/

Purva Jani [68] SYBCA Sem 4 23


JAVA PROGRAMMING

Q17. Write a program for method overloading.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for method overloading.
Class: SYBCA
Div: 2
Date: 23-12-22
*********************************************************/
public class Method
{
public int sum(int x, int y)
{
return (x + y);
}
public int sum(int x, int y, int z)
{
return (x + y + z);
}
public static void main(String args[])
{
Method s = new Method();
System.out.println("The Ans is " + s.sum(10,
20));
System.out.println("The Ans is " +s.sum(10, 20, 30));
}
}

/**************************OUTPUT***********************

The Ans is
30 The Ans
is 60

*******************************************************/

Purva Jani [68] SYBCA Sem 4 24


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 25


JAVA PROGRAMMING

Q18. Write a program for use of static data and static method.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use of static data and static method.
Class: SYBCA
Div: 2
Date: 23-12-22
*********************************************************/
public class Stat
{
static int a =
10; int b = 25;
void simpleDisplay()
{
System.out.println(a);
System.out.println(b);
}
static void staticDisplay()
{
System.out.println( a);
}
public static void main(String[] args)
{
Stat s1 = new Stat();
s1.simpleDisplay();
staticDisplay();
}
}

/**************************OUTPUT***********************

10
25
10

*******************************************************/

Purva Jani [68] SYBCA Sem 4 26


JAVA PROGRAMMING

Q19. Write a program for use of nesting method.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for use of nesting method.
Class: SYBCA
Div: 2
Date: 27-12-22
*********************************************************/
import java.util.Scanner;
public class NestingMethods
{
int perimeter(int l, int b)
{
int pr = 2 * (l +
b); return pr;
}
int area(int l, int b)
{
int pr = perimeter(l, b);
System.out.println("Perimeter: "+pr);
int ar = 6 * l * b;
return ar;
}
int volume(int l, int b, int h)
{
int ar = area(l, b);
System.out.println("Area: "+ar);
int vol ;
vol = l * b * h;
return vol;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter Length of Rectangle: ");
int l = s.nextInt();
System.out.print("Enter Breadth of Rectangle: ");
int b = s.nextInt();
System.out.print("Enter Height of Rectangle: ");
int h = s.nextInt();
NestingMethods n1 = new NestingMethods();
int vol = n1.volume(l, b, h);
System.out.println("Volume: "+vol);
}
}

Purva Jani [68] SYBCA Sem 4 27


JAVA PROGRAMMING

/**************************OUTPUT***********************

Enter Length of Rectangle: 5


Enter Breadth of Rectangle: 7
Enter Height of Rectangle: 12
Perimeter: 48
Area: 378
Volume: 441

*******************************************************/

Purva Jani [68] SYBCA Sem 4 28


JAVA PROGRAMMING

Q20. Write a program for example of single inheritance.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for example of single
inheritance. Class: SYBCA
Div: 2
Date: 27-12-22
*********************************************************/
class Base
{
String name1="base";
}
class Derived extends Base
{
String name2="derived";
}
public class Student
{
public static void main(String args[])
{
Derived d1 = new Derived();
System.out.println("name 1 is “ +d1.name1 + "Name 2 is “+ d1.name2);
}
}

/**************************OUTPUT***********************

Name 1 is base
Name 2 is derived

*******************************************************/

Purva Jani [68] SYBCA Sem 4 29


JAVA PROGRAMMING

Q21. Write a program for example of single inheritance.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for example of multilevel inheritance.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class abc
{
int a,b,c;
}
class pqr extends abc
{
void scan(int p,int q,int r)
{
a=p;
b=q;
c=r;
}

void show()
{
System.out.println("A="+a);
System.out.println("B="+b);
System.out.println("C="+c);
}
}
class xyz extends pqr
{
void sum()
{
show(); System.out.println("Addition="+
(a+b+c));
}
}
class multilevel21
{
public static void main(String args[])
{
xyz x1=new xyz();
int p=Integer.parseInt(args[0]);
int q=Integer.parseInt(args[1]);
int r=Integer.parseInt(args[2]);
x1.scan(p,q,r);
x1.sum();
}

Purva Jani [68] SYBCA Sem 4 30


JAVA PROGRAMMING

}
/**************************OUTPUT***********************

java multilevel21 3 4 5
A=3
B=4
C=5
Addition=12

*******************************************************/

Purva Jani [68] SYBCA Sem 4 31


JAVA PROGRAMMING

Q22. Write a program for method overriding.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for method overloading.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class super1
{
int x;
super1(int x)
{
this.x=x;
}
void display()
{
System.out.println("Super X"+x);
}
}
class sub extends super1
{
int y;
sub(int x,int y)
{
super(x);
this.y=y;
}
void display()
{
System.out.println("Super X="+x);
System.out.println("Super Y="+y);
}
}
class overriding22
{
public static void main(String args[])
{
sub s1=new sub(100,200);
s1.display();
}
}

/**************************OUTPUT***********************
java overriding22
Super X=100
Super Y=200
*******************************************************/

Purva Jani [68] SYBCA Sem 4 32


JAVA PROGRAMMING

Q23. Write a program for example of abstract classed and methods.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for example of abstract classed and methods.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
abstract class abstract1
{
int rollno;
String name;
String address;
abstract void scan(int s1,String s2,String
s3); abstract void display1();
}
class abstract2 extends abstract1
{
void scan(int s1,String s2,String s3)
{
rollno=s1;
name=s2;
address=s3;
}
void display1()
{
System.out.println("Roll No.="+rollno);
System.out.println("Name="+name);
System.out.println("Address="+address);
}
}
class abstract23
{
public static void main(String args[])
{
abstract2 a1=new abstract2();
Scanner sc=new
Scanner(System.in);

System.out.println("Enter Roll Number=");


int s1=sc.nextInt();

System.out.println("Enter Name=");
String s2=sc.nextLine();

System.out.println("Enter Address=");
String s3=sc.nextLine();
a1.scan(s1,s2,s3);

Purva Jani [68] SYBCA Sem 4 33


JAVA PROGRAMMING

a1.display1();

Purva Jani [68] SYBCA Sem 4 34


JAVA PROGRAMMING

/**************************OUTPUT***********************

java abstract23
Enter Roll Number=
68
Enter Name=
Purva Jani
Enter Address=
Navsari
Roll No.=68
Name=Purva Jani
Address=Navsari

*******************************************************/

Purva Jani [68] SYBCA Sem 4 35


JAVA PROGRAMMING

Q24. Write a program for use of final variable and final methods.
/*********************************************************
Ans. Name: Purva Jani
Program: Write a program for use of final variable and final methods.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class final1
{
int a,b;
final void display()
{
System.out.println("A="+a);
System.out.println("B="+b);
}
}
class final2 extends final1
{
int c,d;
void scan()
{
c=60;
d=70;
}
void display()
{
System.out.println("This is not access because final keyword write in above class.");
System.out.println("C="+c);
System.out.println("D="+d);
}
}
class final24
{
public static void main(String args[])
{
final2 f1=new final2();
f1.scan();
f1.display();
}
}

/**************************OUTPUT***********************
javac final24.java
final24.java:18: error: display() in final2 cannot override display() in final1
void display()
overridden method is final
1 error
*******************************************************/

Purva Jani [68] SYBCA Sem 4 36


JAVA PROGRAMMING

Q25. Write a program for create and use of interface.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for create and use of interface.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.io.*;

interface In1
{
final int a = 10;
void display();
}

class TestClass implements In1


{
public void display()
{
System.out.println("Java Programming");
}

public static void main(String[] args)


{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}

/**************************OUTPUT***********************

java TestClass
Java Programming
10

*******************************************************/

Purva Jani [68] SYBCA Sem 4 37


JAVA PROGRAMMING

Q26. Write a program for implementing multiple interface.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for implementing multiple
interface. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
interface t1
{
static final int a=500;
}
interface t2
{
static final int b=600;
}
interface t3 extends t1,t2
{
static final int c=700;
public void display();
}
class interface1 implements t3
{
public void display()
{
System.out.println("A="+a);
System.out.println("B="+b);
System.out.println("C="+c);
}
}
class interface26 extends interface1
{
public static void main(String args[])
{
interface1 i1=new interface1();
i1.display();
}
}

/**************************OUTPUT***********************

java interface26
A=500
B=600
C=700

*******************************************************/

Purva Jani [68] SYBCA Sem 4 38


JAVA PROGRAMMING

Q27. Write a program for create single dimensional array with 10 element And print that
data in sorted list.
Ans.
/*********************************************************
Name: Purva Jani
Program: Write a program for create single dimensional array with 10 element And
print that data in sorted list.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class onedimentional27
{
public static void main(String args[])
{
int number[]={101,91,262,93,24};
int n=number.length;
for(int i=0;i<n;i++){
System.out.println(" "+number[i]);
}
System.out.println("\n");
for(int i=0;i<n;i++){
for(int
j=i+1;j<n;j++){ if(numb
er[i]>number[j])
{
int temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
System.out.println(" "+number[i]);
}
System.out.println(" ");
}
}

/**************************OUTPUT***********************
java onedimentional27
101 24
91 91
262 93
93 101
24 262
*******************************************************/

Purva Jani [68] SYBCA Sem 4 33


JAVA PROGRAMMING

Q28. Write a program for to create two dimensional matrix and multiply that matrix.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program for to create two dimensional matrix and multiply that matrix
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.util.Scanner;

public class MatrixMultiplication


{ public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows for matrix A: ");
int rowsA = sc.nextInt();
System.out.println("Enter the number of columns for matrix A:
"); int columnsA = sc.nextInt();
System.out.println("Enter the number of rows for matrix B: ");
int rowsB = sc.nextInt();
System.out.println("Enter the number of columns for matrix B: ");
int columnsB = sc.nextInt();
if (columnsA != rowsB) {
System.out.println("The matrices can't be multiplied");
return;
}
int[][] matrixA = new int[rowsA][columnsA];
int[][] matrixB = new int[rowsB][columnsB];
System.out.println("Enter the elements of matrix A: ");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < columnsA; j++)
{matrixA[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of matrix B: ");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < columnsB; j++)
{matrixB[i][j] = sc.nextInt();
}
}
int[][] resultMatrix = new int[rowsA][columnsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < columnsB; j++) {
for (int k = 0; k < columnsA; k++) { resultMatrix[i][j]
+= matrixA[i][k] * matrixB[k][j];
}
}
}
System.out.println("The result matrix is: ");
Purva Jani [68] SYBCA Sem 4 34
JAVA PROGRAMMING

for (int i = 0; i < rowsA; i++) {


for (int j = 0; j < columnsB; j++)
{ System.out.print(resultMatrix[i][j] + "
");
}
System.out.println();
}
}
}

/**************************OUTPUT***********************

java MatrixMultiplication
Enter the number of rows for matrix A:
3
Enter the number of columns for matrix A:
3
Enter the number of rows for matrix B:
3
Enter the number of columns for matrix B:
3
Enter the elements of matrix A:
123
456
789
Enter the elements of matrix B:
987
654
321
The result matrix is:
30 24 18
84 69 54
138 114 90

*******************************************************/

Purva Jani [68] SYBCA Sem 4 35


JAVA PROGRAMMING

Q29. Write a program to use string method in java.[minimum 10 method].


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to use string method in java.[minimum 10 method].
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class stringmethod
{
public static void
main(String args[]){String
a="JANI"; String
b="PURVA";

System.out.println(a.toLowerCase());
System.out.println(a.toUpperCase());
System.out.println(b.concat(a));
System.out.println(b.length());

String c=" india ";


String d=" ";

System.out.println(c.trim());
System.out.println(d.isEmpty());
System.out.println(c);
System.out.println(b.charAt(2));
System.out.println(a.indexOf('k'));
System.out.println(b.equals(a));
System.out.println(b.replace('d','h'));
}
}

/**************************OUTPUT***********************

java stringmethod
jani
JANI
PURVAJANI
6
india
false
india
K
-1
false
PURVA

*******************************************************/
Purva Jani [68] SYBCA Sem 4 36
JAVA PROGRAMMING

Q30. Write a program to use of string bufferclass method.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to use of string bufferclass
method. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class helloworld30
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Object
Oriented"); System.out.println("Original
String="+str); System.out.println("Length of
String="+str.length());

str.append(" Language");
System.out.println("Updated String="+str);

str.insert(0,"Java is ");
System.out.println("Inserted String="+str);

str.replace(8,12,"Secure ");
System.out.println("Replaced String="+str);

str.delete(1,5);
System.out.println("Deleted String="+str);

str.reverse();
System.out.println("Reverse String="+str);
}
}

/**************************OUTPUT***********************

java helloworld30
Original String=Object Oriented
Length of String=15
Updated String=Object Oriented Language
Inserted String=Java is Object Oriented
Language
Replaced String=Java is Secure ct Oriented Language
Deleted String=Jis Secure ct Oriented Language
Reverse String=egaugnaL detneirO tc eruceS siJ

*******************************************************/

Purva Jani [68] SYBCA Sem 4 37


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 38


JAVA PROGRAMMING

Q31. Write a program to use of vectors method.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to use of vectors method.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.util.*;
class vector31
{
public static void main(String args[])
{
Vector list=new Vector();
int length=args.length;
for(int i=0;i<length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt("COBOL",2);
int size=list.size();
String listarray[]=new String[size];
list.copyInto(listarray);
System.out.println("list of Language");
for(int i=0;i<size;i++)
{
System.out.println(listarray[i]);
}
}
}

/**************************OUTPUT***********************

list of Language
c++
java
COBOL
python
.net
oops

*******************************************************/

Purva Jani [68] SYBCA Sem 4 39


JAVA PROGRAMMING

Q32. Write a program to use of wrapper class method.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to use of wrapper class method.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.io.*;
class invest
{
public static void main(String args[])
{
Float principalAmount=new Float(0);
Float interestRate=new Float(0);
int numYears=0;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Principal Amount:");
System.out.flush();
String principalString=in.readLine();
principalAmount=Float.valueOf(principalString);
System.out.print("Enter interest Rate: ");
System.out.flush();
String interestString=in.readLine();
interestRate=Float.valueOf(interestString);
System.out.print("ENter number of years : ");
System.out.flush();
String yearsString=in.readLine();
numYears=Integer.parseInt(yearsString);
}
catch(IOException e)
{
System.out.println("I/O Error");
System.exit(1);
}
float value = loan(principalAmount.floatValue(),
interestRate.floatValue(),numYears);
printline();
System.out.println("final value = " + value);
printline();
}
static float loan (float p,float r,int n)
{
int year=1;
float sum=p;
while (year <=n)

Purva Jani [68] SYBCA Sem 4 40


JAVA PROGRAMMING

{
sum = sum *
(1+r); year = year
+ 1;
}
return sum;
}
static void printline()
{
for (int i=1;i <=30; i++)
{
System.out.print("=");
}
System.out.println(" ");
}
}

/**************************OUTPUT***********************

java invest
Enter Principal
Amount:20000 Enter interest
Rate: 3.4
ENter number of years : 5
==============================
final value = 3.2983246E7
==============================

*******************************************************/

Purva Jani [68] SYBCA Sem 4 41


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 42


JAVA PROGRAMMING

Q33. Write a program to implement Enum data.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to implement Enum data.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
public class workingdays
{
enum Days
{
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday
}
public static void main(String args[])
{
for(Days d: Days.values())
{
weekend (d);
}
}
private static void weekend (Days d)
{
if(d.equals(Days.sunday))
System.out.println("value="+ d + "is a
holiday"); else
System.out.println("value="+ d + "is a working day");
}
}

/**************************OUTPUT***********************

java workingdays
value=sundayis a holiday
value=mondayis a working day
value=tuesdayis a working day
value=wednesdayis a working day
value=thursdayis a working day
value=fridayis a working day
value=saturdayis a working day

*******************************************************/

Purva Jani [68] SYBCA Sem 4 43


JAVA PROGRAMMING

Q34. Write a program to use of Exception Handling.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to use of Exception Handling..
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class exception34
{
public static void main(String args[])
{
try
{
int data=100/0;
//System.out.println(e);
}
catch(Exception e)
{
System.out.println("Divide by Zero is not Allowed.");
}
}
}

/**************************OUTPUT***********************

java exception34
Divide by Zero is not Allowed.

*******************************************************/

Purva Jani [68] SYBCA Sem 4 44


JAVA PROGRAMMING

Q35. Write a program to input command line argument and find out how many
integer arguments there[use try..catch block].
Ans.
/*********************************************************
Name: Purva Jani
Program: Write a program to input command line argument and find out how
many integer arguments there[use try..catch block]
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class CLineInput
{
public static void main(String args[])
{
int invalid =0;
int number,count=0;
for(int i=0;i<args.length;i++)
{
try
{
number = Integer.parseInt(args[i]);
}
catch(NumberFormatException e)
{
invalid=invalid+1;
System.out.println("Invalid Number:"+args[i]);
continue;
}
count=count+1;
}
System.out.println("valid number ="+count);
System.out.println("invalid number ="+invalid);
}
}

/**************************OUTPUT***********************

java CLineInput 2 10 5.8


Invalid Number:5.8
valid number =2
invalid number =1

*******************************************************/

Purva Jani [68] SYBCA Sem 4 45


JAVA PROGRAMMING

Q36. Write a program to implement nested try statement


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to implement nested try statement
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
public class nestedtry36
{
public static void main(String arg[])
{
try
{
int data;
int p[]=new int[2];
p[3]=33;
try
{
data=100/0;
System.out.println("The Value of Data is:"+data);
}
catch(ArithmeticException a)
{
System.out.println("Divided by Zero is Not Allowed:");
}
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Reset of the code. . .I will try always print.");
}
}

/**************************OUTPUT***********************

java nestedtry36
java.lang.ArrayIndexOutOfBoundsException: 3
Reset of the code. . .I will try always print

*******************************************************/

Purva Jani [68] SYBCA Sem 4 46


JAVA PROGRAMMING

Q37. Write a program to implement Multiple catch statements.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to implement Multiple catch
statements. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class nestedtry37
{
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.");
}
}
}

/**************************OUTPUT***********************

java nestedtry37
Arithmetic Exception Occurs

*******************************************************/

Purva Jani [68] SYBCA Sem 4 47


JAVA PROGRAMMING

Q38. Write a program to implement Finally block.


/*********************************************************
Ans. Name: Purva Jani
Program: Write a program to implement Finally block.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
public class TestFinallyBlock2{
public static void

main(String args[]){try {

System.out.println("Inside try block");

//below code throws divide by zero exception


int data=25/0;
System.out.println(data);
}

//handles the Arithmetic Exception / Divide by zero


exception catch(ArithmeticException
e){ System.out.println("Exception handled");
System.out.println(e);
}

//executes regardless of exception occured or not


finally {
System.out.println("finally block is always executed");
}

System.out.println("rest of the code...");


}
}

/**************************OUTPUT***********************

java TestFinallyBlock2
Inside try block
Exception handled
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...

*******************************************************/

Purva Jani [68] SYBCA Sem 4 48


JAVA PROGRAMMING

Q39. Write a program to implement Throwing your own exception.


/*********************************************************
Ans. Name: Purva Jani
Program: Write a program to implement Throwing your own exception.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.lang.Exception;
class myexception extends Exception
{
myexception(String message)
{
super(message);
}

}
class testmyexception
{
public static void main(String args[])
{
int x = 5,y =
1000; try{
float z = (float)x/(float) y;
if (z < 0.01)
{
throw new myexception ("number is too small");
}
}
catch (myexception e)
{
System.out.println("caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("i am always here");
}
}
}

/**************************OUTPUT***********************

java testmyexception
caught my exception
number is too small
i am always here

*******************************************************/

Purva Jani [68] SYBCA Sem 4 49


JAVA PROGRAMMING

Q40. Write a program to implement Throws keyword.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to implement Throws
keyword. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class Examplethrows
{
static void divide_m() throws ArithmeticException
{
int x=22,y=0,z;
z=x/y;
}
public static void main(String args[])
{
try
{
divide_m();
}
catch(ArithmeticException e)
{
System.out.println("Caught the exception"+e);
}
}
}

/**************************OUTPUT***********************

java Examplethrows
Caught the exceptionjava.lang.ArithmeticException: / by zero

*******************************************************/

Purva Jani [68] SYBCA Sem 4 50


JAVA PROGRAMMING

Q41. Write a program to create your own package and use that package in other class.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create your own package and use that package in other.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
package package1;
public class ClassA
{
public void displayA()
{
System.out.println("Class A");
}
}
package package2;
public class ClassB
{
protected int m = 10;
public void displayB()
{
System.out.println("Class B");
System.out.println("m = " +
m);
}
}
import package1.ClassA;
import package2.ClassB;
class PackageTest2
{
public static void main(String args[])
{
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();
objectA.displayA();
objectB.displayB();
}
}

/**************************OUTPUT***********************

java PackageTest2
Class A
Class B
m = 10

*******************************************************/

Purva Jani [68] SYBCA Sem 4 51


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 52


JAVA PROGRAMMING

Q42. Write a program to create sub package.


/*********************************************************
Ans. Name: Purva Jani
Program: Write a program to create sub package.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
package mypackage.subtract;
public class sub
{
public void disp()
{
int a=6,b=3;
System.out.println("Value of a:"+a);
System.out.println("Value of b:"+b);
System.out.println("Subtraction:"+(a-b));
}
}
import mypackage.subtract.sub;
import java.util.Scanner;
class dem2
{
public static void main(String args[])
{
sub s=new sub();
s.disp();
}
}

/**************************OUTPUT***********************

Value of a:6
Value of b:3
Subtraction:3

******************************************************/

Purva Jani [68] SYBCA Sem 4 53


JAVA PROGRAMMING

Q43. Write a program to create applet that print your detail.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create applet that print your detail.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.awt.*;
import java.applet.*;
public class myapplet extends applet
{
public void paint(Graphics g)
{
Font f1;
f1=new Font("Times New
Roman",Font.BOLD,15); g.setFont(f1);
g.drawString("Name:Nikhil",20,20);
g.drawString("Surname:Solanki",40,40);
g.drawString("Class:SyBCA Div-3",60,60);
g.drawString("Roll No204",80,80);
}
}

HTML:-

<html>
<head>
<title>My Applet</title>
</head>
<body>
<Applet code="myapplet.class" width=300 height=300></Applet>
</body>
</html>

/**************************OUTPUT***********************

*******************************************************/

Purva Jani [68] SYBCA Sem 4 54


JAVA PROGRAMMING

Q44. Write a program to create applet that uses any seven graphics class Methods.
Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create applet that uses any seven graphics class Methods.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
public class seven extends Applet
{
public void paint(Graphics
g){ g.setColor
(Color.green);
g.drawString ("Welcome", 70, 70);
g.drawLine (30, 40, 30, 400);
g.drawRect (80, 150, 40, 40);
g.fillRect (190, 120, 40, 40);
g.drawOval (90, 180, 30,
30); g.setColor (Color.blue);
g.fillOval (170, 200, 30, 30);
g.drawString("Purva Jani",100,200);
g.drawRect(80,180,100,30);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(200,110,60,30,5,5);
}
}

HTML:-
<html>
<head>
<title>Seven</title>
</head>
<body>
<Applet code="seven.class" height="500" width="500"></Applet>
</body>
</html>

/**************************OUTPUT***********************

*******************************************************/
Purva Jani [68] SYBCA Sem 4 52
JAVA PROGRAMMING

Q45. Write a program to create a threads using thread class.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create a throw using thread
class. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.awt.*;
import java.applet.*;
public class paramtag extends Applet
{
String str,nm,snm;
public void init()
{
str=getParameter("String");
nm=getParameter("name");
snm=getParameter("surname");
if(str==null)
{
str="Java";
}else{
str=str+" Applet";
}
}

public void paint(Graphics


g){ g.drawString(str,10,200);
g.drawString(nm,10,100);
g.drawString(snm,60,180);
}
}

HTML:-

<html>
<head>
<title>Using Param Tag</title>
</head>
<body>
<Applet code="paramtag.class" height="500" width="500">
<param name="String" value="Programming"></param>
<param name="name" value="Purva"></param>
<param name="surname" value="Jani"></param>
</Applet>
</body>
</html>

Purva Jani [68] SYBCA Sem 4 53


JAVA PROGRAMMING

Purva Jani [68] SYBCA Sem 4 54


JAVA PROGRAMMING

/**************************OUTPUT***********************

*******************************************************/

Purva Jani [68] SYBCA Sem 4 55


JAVA PROGRAMMING

Q46. Write a program to create a threads using thread class.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create a throw using thread
class. Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class A extends Thread
{
public void run ()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t From Thread A : i = " +i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\t From Thread B : j = " +j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t From Thread C : k = " +k);
}
System.out.println("Exit from C");
}
}
class ThreadTest
{
public static void main(String args[])
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();

Purva Jani [68] SYBCA Sem 4 56


JAVA PROGRAMMING

ThreadA.start();
ThreadB.start();
ThreadC.start();
}
}

/**************************OUTPUT***********************

java ThreadTest
From Thread A : i = 1
From Thread C : k = 1
From Thread B : j = 1
From Thread C : k = 2
From Thread A : i = 2
From Thread C : k = 3
From Thread B : j = 2
From Thread C : k = 4
From Thread A : i = 3
From Thread C : k = 5
From Thread B : j = 3
Exit from C
From Thread A : i = 4
From Thread B : j = 4
From Thread A : i = 5
From Thread B : j = 5
Exit from A
Exit from B

*******************************************************/

Purva Jani [68] SYBCA Sem 4 57


JAVA PROGRAMMING

Q47. Write a program to create a thread using runnable interface.


Ans. /*********************************************************
Name: Purva Jani
Program: Write a program to create a thread using runnable interface.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
class X implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("\tThreadX" +i);
}
System.out.println("End of ThreadX");
}
}
class RunnableTest
{
public static void main(String args[])
{
X runnable=new X();
Thread threadX=new Thread(runnable);
threadX.start();
System.out.println("End of main Thread");
}
}

/**************************OUTPUT***********************

java RunnableTest
End of main Thread
ThreadX1
ThreadX2
ThreadX3
ThreadX4
ThreadX5
ThreadX6
ThreadX7
ThreadX8
ThreadX9
ThreadX10
End of ThreadX

*******************************************************/

Purva Jani [68] SYBCA Sem 4 58


JAVA PROGRAMMING

Q48. Write a Program to Create Singly Link List with its Function
Ans. /*********************************************************
Name: Purva Jani
Program: Write a Program to Create Singly Link List with its Function
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
import java.lang.*;
import java.io.DataInputStream;
class Node
{
int value;
Node next;
public Node(int value)
{
this.value=value;
this.next=null;
}
}
class SinglyLinkList
{
Node start;
public void insert(int value)
{
//Node start=new Node(value);
Node current = start;
while(current.next != null)
{
current = current.next;
}
Node newNode = new Node(value);
current.next=newNode;
}
public void print()
{
Node current = start;
while(current.next != null){
System.out.print(current.value+" ");
current=current.next;
}
System.out.println(current.value);
}
public void delete(int value)
{
if(value == start.value)
{
start=start.next;

Purva Jani [68] SYBCA Sem 4 59


JAVA PROGRAMMING

System.out.println(start.value+" is deleted successfully


from your node list.");
return;
}
Node current = start;
try{
while(current.next.value != value)
{
current=current.next;
}
System.out.println(current.next.value+" is deleted
successfully from your node list.");
current.next=current.next.next;
}
catch(NullPointerException e)
{
System.out.println("please enter valid node value.");
}
}
public void search(int value)
{
int position=1;
Node current=start;
while(current.next != null)
{
if(current.value == value)
{
System.out.println("value is found in your link list.");
System.out.println("At "+position+" position "+value+" is
there in your node list.");
return;
}
current=current.next;
position++;
}
}
public static void main(String arg[])
{
SinglyLinkList obj = new SinglyLinkList();
DataInputStream obj2 = new DataInputStream(System.in);
int ch=0,value=0,i=1;
do{
System.out.println("1 - Insertion operation.");
System.out.println("2 - Deletion operation.");
System.out.println("3 - Display operation.");
System.out.println("4 - Searching operation.");
System.out.println("5 - Exit.");
try{
System.out.print("enter your choice = ")

Purva Jani [68] SYBCA Sem 4 60


JAVA PROGRAMMING

ch=Integer.parseInt(obj2.readLine());
}
catch(Exception e){}
switch(ch){
case 1: try
{
System.out.print("enter your value = ");
value=Integer.parseInt(obj2.readLine());
}
catch(Exception e){}
if(i==1)
{
obj.start=new Node(value);
i++;
}
else
{
obj.insert(value);
}
break;
case 2: try
{
System.out.print("enter your value to delete = ");
value=Integer.parseInt(obj2.readLine());
}
catch(Exception e){}
obj.delete(value);
break;
case 3: obj.print();
break;
case 4: try{
System.out.print("search = ");
value=Integer.parseInt(obj2.readLine());
}
catch(Exception e){}
obj.search(value);
break;
}
}
while(ch>=1&&ch<=4);
System.out.print("program is end.");
}
}

/**************************OUTPUT***********************

Purva Jani [68] SYBCA Sem 4 61


JAVA PROGRAMMING

1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation
5 - Exit.
enter your choice = 1
enter your value = 1
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 1
enter your value = 2
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 1
enter your value = 3
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 1
enter your value = 4
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 1
enter your value = 5
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 3
12345
1 - Insertion operation.
2 - Deletion operation3 - Display operation.
4 - Searching operation.
5 - Exit.

Purva Jani [68] SYBCA Sem 4 62


JAVA PROGRAMMING

enter your choice = 2


enter your value to delete = 3
3 is deleted successfully from your node list.

Purva Jani [68] SYBCA Sem 4 63


JAVA PROGRAMMING

4 - Searching operation.
5 - Exit.
enter your choice = 3
1245
1 - Insertion operation.
2 - Deletion operation.
3 - Display operation.
4 - Searching operation.
5 - Exit.
enter your choice = 4
search = 2
value is found in your link list.
At 2 position 2 is there in your node list.
1 - Insertion operation.
2 - Deletion
operation. 3 - Display
operation.
4 - Searching operation.
5 - Exit.
enter your choice = 5
program is end.

*******************************************************/

Purva Jani [68] SYBCA Sem 4 64


JAVA PROGRAMMING

Q49. Write a Program to Create Circular Singly Link List with its Function
Ans. /*********************************************************
Name: Purva Jani
Program: Write a Program to Create Circular Singly Link List with its Function.
Class: SYBCA
Div: 2
Date: 20-1-23
*********************************************************/
//Program: Java code to perform circular linked list operations
class circularlinklist {
static class Node {
int data;
Node next;
};
// add node to empty
static Node addtoempty(Node last, int data)
{if (last != null)
return last;
// allocate memory to the new node
Node newNode = new Node();
// assign data to the new node
newNode.data = data;
// assign last to newNode
last = newNode;
// create link to iteself
newNode.next = last;
return last;
}
// add node to the front
static Node addatfront(Node last, int
data){if (last == null)
return addtoempty(last, data);
// allocate memory to the new node
Node newNode = new Node();
// add data to the node
newNode.data = data;
// store the address of the current first node in the newNode
newNode.next = last.next;
// make newNode as head
last.next = newNode;
return last;
}
// add node at the end
static Node addatend(Node last, int
data){if (last == null)
return addtoempty(last, data);
// allocate memory to the new node
Node newNode = new Node();

Purva Jani [68] SYBCA Sem 4 65


JAVA PROGRAMMING

newNode.data=data;
newNode.next=last.next;
last.next=newNode;
last=newNode;
return last;
}

//add after some code


static Node addafter(Node last,int data,int
item){if(last==null) {
return null;
}
Node newNode,p;
p=last.next;

do{
if(p.data==item){
newNode=new Node();
newNode.data=data;
newNode.next=p.next;
p.next=newNode;
if(p==last)
last=newNode;
return last;
}
p=p.next;
}
while(p!=last.next);
System.out.println(item+"The given node is not present in the list");
return last;
}

//delete Node
static Node deleteNode(Node last,int
key){if(last==null)
return null;

if(last.data==key &&
last.next==last){last=null;
return last;
}

Node temp=last, d=new Node();


if(last.data==key){
while(temp.next!=last){
temp=temp.next;
}
temp.next=last.next;
last=temp.next;

Purva Jani [68] SYBCA Sem 4 66


JAVA PROGRAMMING

}
while(temp.next!=last && temp.next.data !=
key){temp=temp.next;
}
if(temp.next.data ==
key){d=temp.next;
temp.next=d.next;
}
return last;
}

//traverse Node
static void traverse(Node
last){Node p;
if(last==null){
System.out.println("List is empty");
return;
}
p=last.next;
do{
System.out.print(p.data+ " ,");
p=p.next;
}while(p!=last.next);
System.out.println();
}
public static void main(String
args[]){Node last=null;
last=addtoempty(last,1);
traverse(last);
System.out.println("1 added to empty");

last=addatend(last,2);
traverse(last);
System.out.println("2 added to end");

last=addatfront(last,3);
traverse(last);
System.out.println("3 added to front");

last=addafter(last,5,3);
traverse(last);
System.out.println("5 added after 3");

deleteNode(last,5);
traverse(last);
System.out.println("Delete Node 5");
}
}

Purva Jani [68] SYBCA Sem 4 67


JAVA PROGRAMMING

/**************************OUTPUT***********************

java circularlinklist
1,
1 added to empty
1 ,2 ,
2 added to end
3 ,1 ,2 ,
3 added to front
3 ,5 ,1 ,2 ,
5 added after 3
3 ,1 ,2 ,
Delete Node 5

*******************************************************/

Purva Jani [68] SYBCA Sem 4 68

You might also like