Professional Documents
Culture Documents
ROLL NO: 82
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
}
}
}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
");
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
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
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;
}
}
} 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
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
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;
}
}
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
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
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
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;