New 2
New 2
New 2
/**************************OUTPUT***********************
Purva
Jani
*******************************************************/
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***********************
*******************************************************/
d2.add1();
}
}
/**************************OUTPUT***********************
*******************************************************/
/**************************OUTPUT***********************
*******************************************************/
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());
/**************************OUTPUT***********************
Integer Number=5
Float Number=3.7
*******************************************************/
/**************************OUTPUT***********************
*******************************************************/
System.out.println();
}
}
}
/**************************OUTPUT***********************
*******************************************************/
/**************************OUTPUT***********************
*******************************************************/
/**************************OUTPUT***********************
Addition is 50
Subtraction is 10
Multiplication is 600
Division is 1
Modulus is 10
Increment is 30
Decrement is 31
*******************************************************/
/**************************OUTPUT***********************
*******************************************************/
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***********************
*******************************************************/
*******************************************************/
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:
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***********************
*******************************************************/
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
*******************************************************
/**************************OUTPUT***********************
Id is 1
Name is Purva
*******************************************************/
/**************************OUTPUT***********************
The Ans is
30 The Ans
is 60
*******************************************************/
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
*******************************************************/
/**************************OUTPUT***********************
*******************************************************/
/**************************OUTPUT***********************
Name 1 is base
Name 2 is derived
*******************************************************/
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();
}
}
/**************************OUTPUT***********************
java multilevel21 3 4 5
A=3
B=4
C=5
Addition=12
*******************************************************/
/**************************OUTPUT***********************
java overriding22
Super X=100
Super Y=200
*******************************************************/
System.out.println("Enter Name=");
String s2=sc.nextLine();
System.out.println("Enter Address=");
String s3=sc.nextLine();
a1.scan(s1,s2,s3);
a1.display1();
/**************************OUTPUT***********************
java abstract23
Enter Roll Number=
68
Enter Name=
Purva Jani
Enter Address=
Navsari
Roll No.=68
Name=Purva Jani
Address=Navsari
*******************************************************/
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
*******************************************************/
interface In1
{
final int a = 10;
void display();
}
/**************************OUTPUT***********************
java TestClass
Java Programming
10
*******************************************************/
/**************************OUTPUT***********************
java interface26
A=500
B=600
C=700
*******************************************************/
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
*******************************************************/
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;
/**************************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
*******************************************************/
System.out.println(a.toLowerCase());
System.out.println(a.toUpperCase());
System.out.println(b.concat(a));
System.out.println(b.length());
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
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
*******************************************************/
/**************************OUTPUT***********************
list of Language
c++
java
COBOL
python
.net
oops
*******************************************************/
{
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
==============================
*******************************************************/
/**************************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
*******************************************************/
/**************************OUTPUT***********************
java exception34
Divide by Zero is not Allowed.
*******************************************************/
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***********************
*******************************************************/
/**************************OUTPUT***********************
java nestedtry36
java.lang.ArrayIndexOutOfBoundsException: 3
Reset of the code. . .I will try always print
*******************************************************/
/**************************OUTPUT***********************
java nestedtry37
Arithmetic Exception Occurs
*******************************************************/
main(String args[]){try {
/**************************OUTPUT***********************
java TestFinallyBlock2
Inside try block
Exception handled
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...
*******************************************************/
}
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
*******************************************************/
/**************************OUTPUT***********************
java Examplethrows
Caught the exceptionjava.lang.ArithmeticException: / by zero
*******************************************************/
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
*******************************************************/
/**************************OUTPUT***********************
Value of a:6
Value of b:3
Subtraction:3
******************************************************/
HTML:-
<html>
<head>
<title>My Applet</title>
</head>
<body>
<Applet code="myapplet.class" width=300 height=300></Applet>
</body>
</html>
/**************************OUTPUT***********************
*******************************************************/
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
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>
/**************************OUTPUT***********************
*******************************************************/
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
*******************************************************/
/**************************OUTPUT***********************
java RunnableTest
End of main Thread
ThreadX1
ThreadX2
ThreadX3
ThreadX4
ThreadX5
ThreadX6
ThreadX7
ThreadX8
ThreadX9
ThreadX10
End of ThreadX
*******************************************************/
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;
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***********************
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.
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.
*******************************************************/
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();
newNode.data=data;
newNode.next=last.next;
last.next=newNode;
last=newNode;
return last;
}
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;
}
}
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");
}
}
/**************************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
*******************************************************/