You are on page 1of 54

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 1

ROLL NO: 82

1.Addition of two values passing by command line argument. CODE


:-
class Addition
{
public static void main(String arg[])
{
int a = Integer.parseInt(arg[0]);
int b = Integer.parseInt(arg[1]);
int sum = a+b;
System.out.println("THE VALUE OF A IS: "+a);
System.out.println("THE VALUE OF B IS: "+b);
System.out.println("THE SUM OF A AND B IS:
"+sum);
}
}
OUTPUT :-

Make CALC class with two data members. Perform addition, subtraction, multiplication
and division operation of given two values by user. Initialize both data members with
zero. (use menu-driven)
CODE :-
import java.util.Scanner;
class Calc
{
int res;
int add(int no1,int no2)
{
res=no1+no
2; return
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 2
ROLL NO: 82
res;
}
int mul(int no1,int no2)
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 3
ROLL NO: 82
{
res=no1*no
2; return res;
}
int div(int no1,int no2)
{
res=no1/no2;
return res;
}
int sub(int no1,int no2)
{
res=no1-no2;
return res;
}
}
public class Calc_c
{
public static void main(String args[])
{
Scanner sc = new
Scanner(System.in); int no1=0;
int no2=0;
System.out.print("\t\nEnter no 1 :-"); no1
= sc.nextInt();
System.out.print("\t\nEnter no 2 :-"); no2
= sc.nextInt();
Calc ob = new Calc();
System.out.println();
int m;
do
{
System.out.println();
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 4
ROLL NO: 82
System.out.print("\n
0.Exit\n1.Addition\n2.Multipication\n3.Division\n4.Substraction")
;
System.out.println();
System.out.print("\nEnter choice :-"); m
= sc.nextInt();
switch(m)
{
case 0: break;
case 1:
{
System.out.print("Two
"+ob.add(no1,no2)); number sum is:-

break;
}
case 2:
{
System.out.print("Two
"+ob.mul(no1,no2)); number mul is:-

break;
}
case 3:
{
System.out.print("Two
"+ob.div(no1,no2)); number div is:-

break;
}
case 4:
{
System.out.print("Two
"+ob.sub(no1,no2)); number sub is:-
5

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


break; }
default:
{
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 6
ROLL NO: 82
System.out.print("
choice"); You enter wrong

}
}
}while(m!=0);
}
}
OUTPUT :-

Create class EMPLOYEE with i͚ d, name and salary͛ private data-members. Create 5
objects dynamically through user input. Create two methods which display data salary
wise and name wise.
CODE :-
import java.util.Scanner;
class EMP
{
int eno; int
salary;
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 7
ROLL NO: 82
String name;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print(" Enter The Employee Number : ");
eno = sc.nextInt();
System.out.println();
System.out.print(" Enter THE Eployee's Salary : ");
salary = sc.nextInt();
System.out.println();
Scanner sc1 = new Scanner(System.in);
System.out.print(" Enter The Employee's Name : ");
name = sc1.nextLine();
System.out.println(" ");
System.out.println(" ");
}
void display()
{
System.out.println(" "+eno+" \t "+name+" \t "+salary);
}

}
class Demo
{
public static void main(String arg[])
{
final int size = 5;
EMP e[] = new
EMP[size]; for(int i =
0;i<size;i++)
{
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 8
ROLL NO: 82
e[i] = new EMP();
e[i].input();
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 9
ROLL NO: 82
}
System.out.println("\n ENO \t NAME \t SALARY");
for(int i = 0;i<size;i++)
{
e[i].display();
}
int ch;
do
{
System.out.println("\n 0.Exit");
System.out.println(" 1.Name Wise Sorting");
System.out.println(" 2.Salary Wise Sorting");
System.out.println();
Scanner sc2 = new Scanner(System.in);
System.out.print("Enter Your Choice : "); ch
= sc2.nextInt();
switch(ch)
{
case 0:break;
case 1:
{
for(int i = 0;i<size-1;i++)
{
for(int j = 0;j<size;j++)
{

if((e[i].name).compareTo(e[j].name) > 0)
{
EMP temp = e[i];
e[i] = e[j]; e[j]
= temp;
}
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 10
ROLL NO: 82
}
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 11
ROLL NO: 82
}

System.out.println("\n
");

Name Wise Sorting :


SALARY");

System.out.println("\n ENO

\t NAME \t

for(int i = 0;i<size;i++)
{
e[i].display();
}
break;
}
case 2:
{
for(int i = 0;i<size-1;i++)
{
for(int j = 0;j<size;j++)
{
if(e[i].salary > e[j].salary)
{
EMP temp = e[i];
e[i] = e[j]; e[j]
= temp;
}
}
}
System.out.println("\n Salary
Sorting : "); Wise
System.out.println("\n ENO
SALARY"); \t NAME \t for(int i =
0;i<size;i++)
12

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


{
e[i].display();
}
break;
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 13
ROLL NO: 82
}
default: System.out.println("Please Enter Valid
Choice !!!");break;
}
}while(ch!=0);
}
}
OUTPUT :-

Create abstract class Figure and its child classes Rectangle and Triangle. Figure class
has abstract area method which is implemented by Rectangle and Triangle class to
calculate area. Use run time polymorphism to calculate area or Rectangle and Triangle
objects.
CODE :-
import java.util.Scanner;
abstract class Figure
{
abstract void area(double a, double b);
}
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 14
ROLL NO: 82
class Rectangle extends Figure
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 15
ROLL NO: 82
{
void area(double a, double b)
{
double res = a*b;
System.out.println("THE AREA OF RECTANGLE :- "+res);
}
}
class Triangle extends Figure
{
void area(double a, double b)
{
double res = (a*b)/2;
System.out.println("THE AREA OF TRIANGLE :- "+res);
}
}
class Demo
{
public static void main(String arg[])
{
Figure f;
Scan
ner
sc =
new
Scan
ner(

"); Syst
em.i
n);
Rect
angl
e R1
16

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


= S
new y
Rect s
angl t
e(); e
Syste m
m.out.print("ENTER
THE LENGTH OF .
RECTANGLE :- "); o
d u
o t
u .
b p
l r
e i
a n
= t
s l
c n
. (
n )
e ;
x System.out.print("E
t NTER THE WIDTH
OF RECTANGLE :-
D
o d
u o
b u
l b
e l
( e
) b
; =
17

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


s t
c l
. n
n (
e )
x ;
t f = R1;
D f.area(a, b);
o System.out.println("
u
");
b
l
e
(
)
;
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 18
ROLL NO: 82
System.out.println(" ");
Triangle T1 = new Triangle();
System.out.print("ENTER THE SIDE 1 OF TRIANGLE :- ");
a = sc.nextDouble();
System.out.println();
System.out.print("ENTER THE SIDE 2 OF TRIANGLE :- ");
b = sc.nextDouble();
System.out.println(); f
= T1;
f.area(a, b);
}
}
OUTPUT :-

Write a java code which accept a string and display the string in reverse
order. CODE :-
class A
{
public static void main(String arg[])
{
StringBuffer s = new StringBuffer("KARTIK");
s.reverse();
System.out.println(s);
}
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 19
ROLL NO: 82
}
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 20
ROLL NO: 82

OUTPUT :-

Write a program for searching a given sub string from the given sentence. Also
calculate number of times given sub string occur in given sentence.
CODE :-
class Pro_6
{
public static boolean isEmpty(String s)
{
return s == null || s.length() == 0;
}
public static int
str) countMatches(String text,
String

{
if
(isEm
pty(te
xt) ||
isEmp
ty(str
)) {
return
0;
}

int index = 0, count = 0;


while (true)
{
index =
index); text.indexOf(str,
21

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


if (index != -1) {
count
++;
str.length();
index
}
+=
else {

}
} break;
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 22
ROLL NO: 82

return count;
}
public static void main(String[] args)
{
String text = "THIS IS A BOOK. AND IT IS
WRITTEN BY DENNIS RITCHIE.";
String str = "IS";
System.out.println();
System.out.println(text);
System.out.println();
System.out.println("Search 'IS' From The
Sentence");
int count =
countMatches
(text, str);
System.out.pr
intln();
System.out.println("The
Number Of Time 'IS'
Written In The Sentence :- "+count);
}
}
OUTPUT :-

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 :-
package balance;
23

SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT ROLL NO: 82


public class Account
{
int bal = 50000;
public void disp()
{
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 24
ROLL NO: 82
System.out.println("THE BALANCE IS :- "+bal);
}
}

import balance.Account;
class AccountDemo
{
public static void main(String arg[])
{
Account a = new
Account(); a.disp();
}
}
OUTPUT :-

Create your own User defined exception InvalidCharException if any one of the char
@,*,? is present in given string.
CODE :-
import java.util.Scanner;
class InvalidCharexception extends Exception
{
String a;
InvalidCharexception(String a)
{
this.a = a;
}
public String toString()
{
return "invalid char exception : "+a+" contain
invalid character";
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 25
ROLL NO: 82
}
}
class str
{
str(String s1)
{
try
{

if(s1.contains("@")||s1.contains("*")||s1.contains("?"))
{
throw new InvalidCharexception(s1);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Pro5
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String : "); str a
= new str(sc.nextLine());
}
}

OUTPUT :-
SYBCASEM-4 DIV-2 JAVAPRACTICAL ASSIGNMENT 26
ROLL NO: 82

Write a program to accept 10 names from the user and print all the names which start
from B with interval of 2 sec.
CODE :-
import java.util.Scanner;
class Pro7
{
public static void main(String arg[]) throws
InterruptedException
{
Scanner sc = new
Scanner(System.in); String name[] =
new String[10]; for(int i = 0;i <
10;i++)
{
System.out.print("Enter The Name : ");
name[i] = sc.nextLine();
}
System.out.println("\n Names Start With 'B' : "); for(int i
= 0;i < 10;i++)
{

if(name[i].startsWith("B")||name[i].startsWith("b"))
{
Thread.sleep(2000);
System.out.println(name[i]);
}
}
}
}
OUTPUT :-
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 16
DIV-3 ASSIGNMENT :-195

Create an applet program which display triangle having circle. Circle have to touch with
all edges of triangle. Also write your name between circle.
CODE :-
import java.applet.*;
import java.awt.*;
/*
<applet code = "Pro12" width = 1000 height = 1000>
</applet>
*/
public class Pro12 extends Applet
{
public void paint(Graphics g)
{
int x[] = {407,306,510};
int y[] = {125,327,327};
g.setColor(Color.yellow);
g.fillPolygon(x,y,3);
g.setColor(Color.blue);
g.fillOval(345,205,125,125);
g.setColor(Color.white);
g.drawString("KARTIK" ,390 ,275);
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 17
DIV-3 } ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 18
DIV-3 ASSIGNMENT :-195
OUTPUT :-

Create an applet program to display your name which move in window left to
right. CODE :-
import java.awt.*;
import java.applet.*;
/*
<applet code = "MovingName" width = 500 height = 500>
</applet>
*/
public class MovingName extends Applet implements Runnable
{
public String display;
public int x,y,flag;
Thread t;
public void init()
{
display = " KARTIK PATEL";
x = 100;
y = 100;
flag = 1;
t = new Thread(this, "MyThread");
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 19
DIV-3 t.start(); ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 18
DIV-3 ASSIGNMENT :-195
public void update()
{
x = x+10 * flag;
if(x > 300)
flag = -1;
if(x < 100)
flag = 1;
}
public void run()
{
while(true)
{
repaint();
update();
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
}
}
public void paint(Graphics g)
{
g.drawString(display, x, y);
}
}
OUTPUT :-
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 19
DIV-3 ASSIGNMENT :-195

Write an applet program which show Digital


Clock. CODE :-
import java.awt.*;
import java.applet.*;
import java.util.*;
/*
<applet code = "Pro13" width = 500 height = 500>
</applet>
*/
public class Pro13 extends Applet
{
Font f;
public void init()
{
Font f = new Font("Times New Roman",Font.BOLD,40);
setFont(f);
}
public void paint(Graphics g)
{
Date ob = new Date();
g.drawString(ob.toString().substring(11,19),30,30); try
{
Thread.sleep(1000);
}
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 20
DIV-3 ASSIGNMENT :-195
catch(Exception e){}
repaint();
}
}
OUTPUT :-

create an applet program which take input from parameters through param tag for
EMP class and Display name, age, salary, city of employee.
CODE :-
import java.awt.*;
import java.applet.*;
/*
<applet code = "EMP" width = 500 height = 500>
<para nam = "Name" value = "Kartik">
m e
<para nam = "AGE" value = "19">
m e
<para nam = "SALARY" value =
m e "50000">
<para nam = "CITY" value = "Surat">
m e
</applet>
*/
public class EMP extends Applet
{
String name;
String age;
String salary;
String city;
public void init()
{
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 21
DIV-3 name = ASSIGNMENT :-195
getParameter("NAME"); age
= getParameter("AGE");
salary = getParameter("SALARY");
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 22
DIV-3 ASSIGNMENT :-195
city = getParameter("CITY");
}
public void paint(Graphics g)
{
g.drawString("Details Of Employee : ",20,20);
g.drawString("NAME : "+ name,20,40);
g.drawString("AGE : "+ age,20,60);
g.drawString("SALARY : "+ salary,20,80);
g.drawString("CITY : "+ city,20,100);
}
}

OUTPUT :-

Create Singly Link List program. Use switch case to create menu driven program.
Switch case must have following options. Switch case - menu options.
1. Create Linked List
2. Display linked list
3. Insert node at specific position
4. Delete node from specific position
CODE :-
import java.util.Scanner;
class SLL
{
class Node
{
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 23
DIV-3 int info; ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 24
DIV-3 ASSIGNMENT :-195
Node next;
Node(int
num)
{
info = num;
next = null;
}
}

Node curr = null;


Node temp = null;
Node head = null;
Node tail = null;

void createNode()
{
Scanner sc1 = new Scanner(System.in);
System.out.print("\n Enter The Value Of Node : "); int
num = sc1.nextInt();
temp = new Node(num);
}
void createLinkedList()
{
createNode();
if(head==null)
head = temp;
else
tail.next = temp;
tail = temp;
}
void displayLinkedList()
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 25
DIV-3{ ASSIGNMENT :-195
curr = head;
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 26
DIV-3 ASSIGNMENT :-195
System.out.println();
while(curr!=null)
{
System.out.print(" "+curr.info+" ==> ");
curr = curr.next;
}
System.out.println("NULL");
}
void insertNode()
{
createNode();
Scanner sc2 = new Scanner(System.in);
System.out.print("\n Enter The Position : "); int
pos = sc2.nextInt();
curr = head;
int cnt = 1;
if(pos==1)
{
temp.next =
head; head =
temp; return;
}
while(cnt!=(pos-1))
{
curr = curr.next;
cnt++;
}
temp.next = curr.next;
curr.next = temp;
}
void deleteNode()
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 27
DIV-3{ ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 28
DIV-3 ASSIGNMENT :-195
Scanner sc3 = new Scanner(System.in);
System.out.print("\n Enter The Position : "); int
pos = sc3.nextInt();
int cnt = 1;
if(pos==1)
{
head = head.next;
return;
}
while(cnt!=(pos-1))
{
curr = curr.next;
cnt++;
}
curr.next = (curr.next).next;
}
}
class Demo
{
public static void main(String[] arg)
{
SLL ob = new SLL();
int ch;
do
{
System.out.println("\n 0.Exit");
System.out.println("\n 1.Create
Linked List");
System.out.println("\n 2.Display
Position"); Position");
Linked List");
System.out.println("\n 3.Insert
29

SYBCA SEM-4 JAVA PRACTICAL ROLL NO


DIV-3
Node At Specific ASSIGNMENT :-195

System.out.println("\n 4.Delete
Node At Specific

Scanner sc = new
Scanner(System.in);
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 30
DIV-3 ASSIGNMENT :-195
System.out.print("\n Enter Your Choice : ");
ch = sc.nextInt();
switch(ch)
{
case 0: break;
case 1:
ob.createLinkedList();break;
case 2:
ob.displayLinkedList();break;
case 3: ob.insertNode();break;
case 4: ob.deleteNode();break;
Choice !!!");
default: System.out.println("Enter
} Valid

}while(ch!=0);
}
}
OUTPUT :-
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 31
DIV-3 ASSIGNMENT :-195

Create Singly Circular Link List program. Use switch case to create menu driven
program. Switch case must have following options. Switch case - menu options

5. Create Circular Linked List


6. Display Circular linked list
7. Insert node at specific position
8. Delete node from specific position
CODE :-
import java.util.Scanner;
class CircularLinkedList
{
class Node
{
int info; Node
next;
Node(int
num)
{
info = num;
next = null;
}
}
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 32
DIV-3Node curr = null; ASSIGNMENT :-195
Node temp = null;
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 33
DIV-3 ASSIGNMENT :-195
Node head = null;
Node tail = null;

void createNode()
{
Scanner sc1 = new Scanner(System.in);
System.out.print("\n Enter The Value Of Node : "); int
num = sc1.nextInt();
temp = new Node(num);
}
void createCircularLinkedList()
{
createNode();
if(head==null)
head = temp;
else
tail.next = temp;
tail = temp;
tail.next = head;

}
void displayCircularLinkedList()
{
if(head==null)
{
System.out.println("\n Empty
List ...!!!"); return;
}
else
{
S
y
34

SYBCA SEM-4 JAVA PRACTICAL ROLL NO


DIV-3
st ASSIGNMENT :-195
e
m
.o
u
t.
p
ri
n
tl
n
(
);
c
u
rr
=
h
e
a
d
;
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 35
DIV-3 ASSIGNMENT :-195
do
{
System.out.print(" "+curr.info+" ==> ");
curr = curr.next;
}while(curr!=head);
System.out.println(" NULL");
}
}
void insertNode()
{
createNode();
Scanner sc2 = new Scanner(System.in);
System.out.print("\n Enter The Position : "); int
pos = sc2.nextInt();
curr = head;
int cnt = 1;
if(pos==1)
{
temp.next =
head; head =
temp; tail.next =
temp; return;
}
while(cnt!=(pos-1))
{
curr = curr.next;
cnt++;
}
temp.next = curr.next;
curr.next = temp;
}
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 36
DIV-3void deleteNode() ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 37
DIV-3 ASSIGNMENT :-195
{
Scanner sc3 = new Scanner(System.in);
System.out.print("\n Enter The Position : "); int
pos = sc3.nextInt();
int cnt = 1;
if(pos==1)
{
head = head.next;
return;
}
while(cnt!=(pos-1))
{
curr = curr.next;
cnt++;
}
curr.next = (curr.next).next;
}
}
class Demo
{
public static void main(String[] arg)
{
CircularLinkedList ob = new CircularLinkedList(); int
ch;
do
{
System.out.println("\n 0.Exit");
System.out.println("\n 1.Create
Linked List");
Position"); Position");
System.out.println("\n 2.Display
Linked List");
38

SYBCA SEM-4 JAVA PRACTICAL ROLL NO


DIV-3
System.out.println("\nASSIGNMENT
3.Insert :-195
Node At Specific

System.out.println("\n 4.Delete
Node At Specific
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 39
DIV-3 ASSIGNMENT :-195
Scanner sc = new Scanner(System.in);
System.out.print("\n Enter Your Choice : ");
ch = sc.nextInt();
switch(ch)
{
case 0: break;
case 1:
ob.createCircularLinkedList();brea
k;
case 2:
ob.displayCircularLinkedList();brea
k;

Choice !!!"); case 3: ob.insertNode();break;


} case 4: ob.deleteNode();break;
default: System.out.println("\n
Enter Valid
}while(ch!=0);
}
}
OUTPUT :-
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 40
DIV-3 ASSIGNMENT :-195
SYBCA SEM-4 JAVA PRACTICAL ROLL NO 41
DIV-3 ASSIGNMENT :-195

You might also like