java prgms
TRANSCRIPT
-
8/3/2019 Java Prgms
1/47
Prog1>
/*A class called Account that creates account with 500Rs
minimum balance, a deposit() method and a wihtdraw()method which throws LessBalanceException*/
import java.util.*;
public class Account {
public Scanner ss=new Scanner(System.in);
NullPointerException e=new NullPointerException("Less balance
exception");
public
double amt;
Account() {
amt=500.0;
}
public void deposit() {
double dep;
System.out.print("Enter the amount to be deposited:- ");
dep=ss.nextDouble();amt+=dep;
}
public void withdraw() {
double with;
System.out.print("Enter the amount to be withdrawn:- ");
with=ss.nextDouble();
try
{if(amt-with
-
8/3/2019 Java Prgms
2/47
catch (NullPointerException e) {
System.out.println(e+" ");
}
}
public void dis() {
System.out.println("The balance in the account is "+amt);
}
}
/*A class called LessBalanceException which returns the statement
that says withdraw amount (__Rs) is not valid*/
public class LessBalanceException {
double amt;
public Scanner ss=new Scanner(System.in);
LessBalanceException () {
amt=500.0;
}public void deposit () {
double dep;
System.out.print("Enter the amount to be deposited:- ");
dep=ss.nextDouble();
amt+=dep;
}
public String withdraw () {Scanner ss=new Scanner(System.in);
double with;
System.out.print("Enter the amount to be withdrawn:- ");
with=ss.nextDouble();
if(amt-with
-
8/3/2019 Java Prgms
3/47
return ("With draw amount Rs. "+with+" is not
valid");
else
{
amt-=with;
return ("The balance after withdrawing "+amt);
}
}
}
/*A class which creates two accounts, both account deposit and one
account tries to withdraw more money which generates a
LessBalanceException*/
public class AccountDemon {
public static void main(String[] args) {
Scanner ss=new Scanner(System.in);
Account ACC1=new Account();
LessBalanceException ACC2=new LessBalanceException ();
String res;
int ch;
for(;;) {
System.out.println("1. Accoutn1\t2. Account2\t3.
Exit");
System.out.print("Enter your choice:- ");
ch=ss.nextInt();
System.out.println();
switch(ch) {
case 1: ACC1.deposit();
ACC1.dis();
ACC1.withdraw();
ACC1.dis();
-
8/3/2019 Java Prgms
4/47
break;
case 2:ACC2.deposit();
res=ACC2.withdraw();
System.out.println(res+" ");
break;
case 3: break;
default: System.out.println("Invalid choice....."); break;
}
}
}
}
Output: -
1. Accoutn1 2. Account2 3. Exit
Enter your choice:- 1
Enter the amount to be deposited:- 2000
The balance in the account is 2500.0
Enter the amount to be withdrawn:- 3000java.lang.NullPointerException: Less balance exception
The balance in the account is 2500.0
1. Accoutn1 2. Account2 3. Exit
Enter your choice:- 1
Enter the amount to be deposited:- 100
The balance in the account is 2600.0
Enter the amount to be withdrawn:- 1500
The balance in the account is 1100.0
1. Accoutn1 2. Account2 3. Exit
-
8/3/2019 Java Prgms
5/47
Enter your choice:- 2
Enter the amount to be withdrawn:- 0
The balance after withdrawing 500.0
1. Accoutn1 2. Account2 3. Exit
Enter your choice:- 3
SOURCE CODE:-2a)class program
{
double radius;
figure()
{
radius=0.0;
}
figure(double r)
{
radius=r;
}
void show()
-
8/3/2019 Java Prgms
6/47
{
System.out.println(\n radius= +radius);
}
}
class circle extends figure
{
circle(double r)
{
super(r);
}
double area()
{
return 3.14*radius*radius;
}
void show()
{
super.show();
System.out.println(\n area of circle= +area());
}
}
class ellipse extends figure
{
double radius2;
ellipse(double r,double r2)
{
-
8/3/2019 Java Prgms
7/47
super(r);
radius2=r2;
}
double area()
{
return 3.14*radius*radius2;
}
void show()
{
super.show();
System.out.println(\n radius2= +radius2);
System.out.println(\n area of ellipse= +area());
}
}
class cylinder extends figure
{
double height;
cylinder(double r,double h)
{
super(r);
height=h;
}
double area()
{
return 2*3.14*radius*height;
-
8/3/2019 Java Prgms
8/47
}
void show()
{
super.show();
System.out.println(\n height= +height);
System.out.println(\n area of cylinder= +area());
}
}
public class inherit
{
public static void main( String args[])
{
circle c=new circle(3.5);
ellipse e=new ellipse(2.3,6.5);
cylinder cyl=new cylinder(2.8,4.6);
System.out.println(\n circle);
c.show();
System.out.println( );
System.out.println(\n ellipse);
e.show();
System.out.println( );
System.out.println(\n cylinder);
cyl.show();
}
}
-
8/3/2019 Java Prgms
9/47
2b)Class finallydemo
{
Static void procA()
{
Try{
System.out.println(Inside procA):
Throw new RuntimeExeption(Demo);
}
Finally{
Try{
System.out.println(Inside procB);
Return;
}
Finally{
System.out.println(procBs finally);
}
}
Static void proc c()
{
Try{
System.out.println(Inside proc C);
-
8/3/2019 Java Prgms
10/47
}
Finally{
System.out.println(Proc cs finally);
}
}
Public static void main(String args[])
{
Try{
Proc A();
}
Catch(Exception e)
{
System.out.println(exception caught);
}
procB();
procc();
}
}
OUTPUT:-
-
8/3/2019 Java Prgms
11/47
2a)Circle
Radius=3.5
Area of circle=38.465
Ellipse
Radius=2.3
Radius2=6.5
Area of ellipse=46.943
Cylinder
Radius=2.8
Height=4.6
Area of cylinder=80.8864
2b)Inside procA
Proc as finally
Exceptin caught
Inside procb
procBs finally
Inside procC
Proc Cs finally
Prog4>
Class Q
{
-
8/3/2019 Java Prgms
12/47
int n;
boolean valueSet=false;
synchronized int get()
{
while(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception Caught");
}
System.out.println("consumer: " +n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
{
while(valueSet)
try
{
wait();
-
8/3/2019 Java Prgms
13/47
}
catch(InterruptedException e)
{
System.out.println("InterruptedException Caught");
}
this.n=n;
valueSet=true;
System.out.println("Producer :" +n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
-
8/3/2019 Java Prgms
14/47
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
-
8/3/2019 Java Prgms
15/47
}
}
class ProdCon
{
public static void main(String args[])
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-c to Stop.");
}
}
OUTPUT:
Producer :0
Consumer:0
Producer:1
Consumer:1
Producer:2
consumer:2
producer:3
Consumer:3
producer :4
-
8/3/2019 Java Prgms
16/47
Consumer :4
producer:5
Consumer:5
producer:6
Consumer:6
producer:7
Consumer:7
producer:8
Consumer:8
producer:9
Consumer:9
Producer:10
Consumer:10
Producer:11
Consumer:11
......... . . . . .. . . . .. . . ..
.. . . . .. . . . . . . . .. . . . . . . . .
Prog 5>
// Define an integer stack interface.
interfaceIntStack
{
-
8/3/2019 Java Prgms
17/47
void push(int item); // store an item
int pop(); // retrieve an item
}
// An implementation of IntStack that uses fixed storage.
classFixedStack implements IntStack
{
privateintstck[];
privateinttos;
// allocate and initialize stack
FixedStack(int size)
{
stck = new int[size];
tos = -1;
-
8/3/2019 Java Prgms
18/47
}
// Push an item onto the stack
public void push(int item)
{
if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
publicint pop() {
if(tos< 0) {
-
8/3/2019 Java Prgms
19/47
System.out.println("Stack underflow.");
return 0;
} else
returnstck[tos--];
}
}
// Implement a "growable" stack.
classDynStack implements IntStack
{
privateintstck[];
privateinttos;
// allocate and initialize stack
DynStack(int size)
-
8/3/2019 Java Prgms
20/47
{
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
public void push(int item)
{
// if stack is full, allocate a larger stack
if(tos==stck.length-1)
{
int temp[] = new int[stck.length * 2]; // double size
for(int i=0; i
-
8/3/2019 Java Prgms
21/47
stck = temp;
stck[++tos] = item;
} else
stck[++tos] = item;
} // Pop an item from the stack
if(tos< 0) {
System.out.println("Stack underflow.");
return 0;
} else
returnstck[tos--];
}
}
/* Create an interface variable and access stacks through it. */
-
8/3/2019 Java Prgms
22/47
class lab5 {
public static void main(String args[]) {
IntStackmystack;
DynStack ds = new DynStack(5);
FixedStackfs = new FixedStack(8);
mystack = ds;
for(int i=0; i
-
8/3/2019 Java Prgms
23/47
System.out.println(mystack.pop());
mystack = fs;
System.out.println("Values in fixed stack:");
for(int i=0; i
-
8/3/2019 Java Prgms
24/47
7
6
5
4
3
2
1
0
Values in Fixedstack :
7
6
5
-
8/3/2019 Java Prgms
25/47
4
3
2
1
0
Prog 6>
Clas T{
String num;
T (String s)
{
num=s;
}
-
8/3/2019 Java Prgms
26/47
public String toString ()
{
return num;
}
}
class V
{
String str;
V (String s)
{
Str=s;
}
public String toString()
-
8/3/2019 Java Prgms
27/47
{
return str;
}
}
Class G
{
G(T ob1 , V ob2)
{
String st1= FIRST STRING IS: +ob1;
System.out.println(st1)
String st2=SECOND STRING IS:+ob2;
System.out.println(st2);
}
-
8/3/2019 Java Prgms
28/47
}
public class prog6
{
public Static void main (String[] args)
{
T t=new T (88);
V v=new V (GENERICS);
Gg;
g=new G(t,v);
}
}
OUTPUT
-
8/3/2019 Java Prgms
29/47
FIRST STRING IS: 88
SECOND STRING IS: GENERICS
Prog 7
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList ll=new LinkedList();
Scanner ss=new Scanner(System.in);
int ch,ele,pos;
for(;;) {
System.out.println();
System.out.println("\tMain menu");
System.out.println("1. Insert\t2. Delete\n3. Modify\t4. Display\n5. Exit");
System.out.println();
System.out.print("Enter your choice:- ");
ch=ss.nextInt();
System.out.println();
switch(ch)
case 1: INSERT: {
for(;;) {
-
8/3/2019 Java Prgms
30/47
System.out.println();
System.out.println("\t\tInsert menu");
System.out.println();
System.out.println("1. At first\t\t\t2. At last");
System.out.println("3. At specified position\t4. Display");
System.out.println("5. Main menu");
System.out.println();
System.out.print("Enter your choice:- ");
ch=ss.nextInt();
System.out.println();
switch(ch) {
case 1: System.out.print("Enter the element to be inserted:- ");
ele=ss.nextInt();
ll.addFirst(ele);
break;
case 2: System.out.print("Enter the element to be inserted:- ");
ele=ss.nextInt();
ll.addLast(ele);
break;
case 3: System.out.print("Enter the position:- ");
pos=ss.nextInt();
System.out.println();
if(ll.size()
-
8/3/2019 Java Prgms
31/47
else {
System.out.print("Enter the element to be inserted:- ");
ele=ss.nextInt();
ll.add(pos,ele);
}
break;
case 4: if(ll.isEmpty())
System.out.println("The linked list is empty.....");
else {
System.out.println("The elements in the linked list are");
System.out.println("\t"+ll);
}
break;
case 5: break INSERT;
default: System.out.println("Invalid choice....."); break;
}
}
}
break;
case 2: DELETE: {
for(;;) {
System.out.println();
System.out.println("\t\tDelete menu");
-
8/3/2019 Java Prgms
32/47
System.out.println();
System.out.println("1. At first\t\t\t2. At last");
System.out.println("3. At specified position\t4. Display");
System.out.println("5. Main menu");
System.out.print("Enter your choice:- ");
ch=ss.nextInt();
System.out.println();
switch(ch) {
case 1: if(ll.isEmpty())
System.out.println("The linked list is empty.....");
else
System.out.println("The deleted element is:- "+ll.removeFirst()
);
break;
case 2: if(ll.isEmpty())
System.out.println("The linked list is empty.....");
else
System.out.println("The deleted element is:-
"+ll.removeLast());
break;
case 3: if(ll.isEmpty())
System.out.println("The linked list is empty.....");
else {
System.out.print("Enter the position:- ");
-
8/3/2019 Java Prgms
33/47
pos=ss.nextInt();
if(ll.size()
-
8/3/2019 Java Prgms
34/47
else {
System.out.print("Enter the position where the data to be
modified:- ");
pos=ss.nextInt();
if(ll.size()
-
8/3/2019 Java Prgms
35/47
}
Output: -
C:\PROGRA~1\Java\jdk1.6.0_02\bin>javac LinkedListDemo.java
Note: LinkedListDemo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\PROGRA~1\Java\jdk1.6.0_02\bin>java LinkedListDemo
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 2
Delete menu
1. At first 2. At last
3. At specified position
10 | Page
4. Display
5. Exit
-
8/3/2019 Java Prgms
36/47
4. Dsiplay
Enter your choice:- 1
The linked list is empty.....
Delete menu
1. At first 2. At last
3. At specified position
Enter your choice:- 2
The linked list is empty.....
Delete menu
1. At first 2. At last
3. At specified position
Enter your choice:- 3
The linked list is empty.....
Delete menu
1. At first 2. At last
3. At specified position
-
8/3/2019 Java Prgms
37/47
Enter your choice:- 4
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 3
The linked list is empty.....
Main menu
1. Insert 2. Delete
Enter your choice:- 4
The linked list is empty.....
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 1
Insert menu
1. At first 2. At last
3. At specified position
Enter your choice:- 1
4. Dsiplay
-
8/3/2019 Java Prgms
38/47
4. Dsiplay
4. Dsiplay
4. Display
5. Exit
3. Modify
4. Display
5. Exit
4. Display 5. Exit
4. Dsiplay
11 | Page
Enter the element to be inserted:- 15
Insert menu
-
8/3/2019 Java Prgms
39/47
1. At first 2. At last
3. At specified position
Enter your choice:- 2
Enter the element to be inserted:- 55
Insert menu
1. At first 2. At last
3. At specified position
Enter your choice:- 3
Enter the position:- 5
Invalid position.....
Insert menu
1. At first 2. At last
3. At specified position
Enter your choice:- 3
Enter the position:- 1
Enter the element to be inserted:- 25
Insert menu
1. At first 2. At last
3. At specified position
-
8/3/2019 Java Prgms
40/47
Enter your choice:- 2
Enter the element to be inserted:- 65
Insert menu
1. At first 2. At last
3. At specified position
Enter your choice:- 4
The elements in the linked list are
[15, 25, 55, 65]
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 3
Enter the position where the data to be modified:- 2
4. Dsiplay
4. Dsiplay
4. Dsiplay
-
8/3/2019 Java Prgms
41/47
4. Dsiplay
4. Dsiplay
4. Display 5. Exit
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 4
The elements in the linked list are
[15, 25, 65, 65]
Main menu
1. Insert 2. Delete
3. Modify
Enter your choice:- 2
Delete menu
1. At first 2. At last
3. At specified position
Enter your choice:- 1
-
8/3/2019 Java Prgms
42/47
The deleted element is:- 15
Delete menu
1. At first 2. At last
3. At specified position
Enter your choice:- 2
The deleted element is:- 65
1. At first 2. At last
3. At specified position
Enter your choice:- 3
Enter the position:- 6
Invalid position..
1. At first 2. At last
3. At specified position
Enter your choice:- 3
Enter the position:- 1
The deleted element is:- 25
1. At first 2. At last
3. At specified position
Enter your choice:- 4
Main menu
-
8/3/2019 Java Prgms
43/47
1. Insert 2. Delete
3. Modify
Enter your choice:- 4
4. Display 5. Exit
4. Display 5. Exit
4. Dsiplay
4. Dsiplay
4. Dsiplay
4. Dsiplay
4. Dsiplay
4. Display 5. Exit
The elements in the linked list are
[65]
Main menu
1. Insert 2. Delete
-
8/3/2019 Java Prgms
44/47
3. Modify
Enter your choice:- 5
Language features: -
4. Display 5. Exit
Prog 11
import java.awt.*;import java.awt.event.*;import java.applet.*;/*
*/public class mouse_events extends Applet implements MouseListener,
MouseMotionListener {String msg="";int mouseX=0, mouseY=0;public void init(){addMouseListener(this);addMouseMotionListener(this);
-
8/3/2019 Java Prgms
45/47
addMouseWheelListener(this);}public void mouseClicked(MouseEvent me){mouseX=0;mouseY=10;msg="Mouse Clicked";repaint();}public void mouseEntered(MouseEvent me){mouseX=0;mouseY=10;msg="Mouse Entered";repaint();}public void mouseExited(MouseEvent me){mouseX=0;mouseY=10;msg="Mouse Exited";repaint();}public void mousePressed(MouseEvent me){mouseX=me.getX();mouseY=me.getY();
-
8/3/2019 Java Prgms
46/47
msg="Mouse Down";repaint();}
public void mouseReleased(MouseEvent me){mouseX=me.getX();mouseY=me.getY();msg="Mouse Up";repaint();}public void mouseDragged(MouseEvent me){mouseX=me.getX();mouseY=me.getY();msg="Drag";showStatus("Dragging mouse at " +mouseX+" , "+mouseY);repaint();}public void mouseWheelMoved(MouseWheelEvent me){msg="mouse wheel moved;repaint();}public void mouseMoved(MouseEvent me){showStatus("Moving mouse at "+me.getX()+" , "+me.getY());}
-
8/3/2019 Java Prgms
47/47
public void paint(Graphics g){g.drawString(msg, mouseX, mouseY);}
}
Output: