0% found this document useful (0 votes)
1 views

Final Java LabPrgs

The document outlines a series of Java lab programs, each demonstrating various programming concepts such as Fibonacci series, inheritance, sorting, constructor overloading, string methods, vector methods, interfaces, packages, multithreading, and thread priority. Each program is accompanied by code snippets and brief descriptions of their functionality. The programs range from basic to more advanced topics, providing a comprehensive overview of Java programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Final Java LabPrgs

The document outlines a series of Java lab programs, each demonstrating various programming concepts such as Fibonacci series, inheritance, sorting, constructor overloading, string methods, vector methods, interfaces, packages, multithreading, and thread priority. Each program is accompanied by code snippets and brief descriptions of their functionality. The programs range from basic to more advanced topics, providing a comprehensive overview of Java programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Java Lab Programs

Sl.No Program Name


1 Java Program to display Fibonacci series up to n terms using command line
arguments.(LAB PROGRAM 1)****
2 Java Program to demonstrate Single Inheritance. (LAB PROGRAM 2)****
3 Java Program to sort n elements using array(LAB PROGRAM 3)******
4 Java Program to implement constructor overloading by passing different
number of parameter of different types. (LAB PROGRAM 4) *****
5 Java Program to demonstrate String methods(LAB PROGRAM 5) *****
6 Java Program to demonstrate vector methods(LAB PROGRAM 6)*****
7 Java program to demonstrate concept of interface.(LAB PROGRAM 7)***
8 Java Program to demonstrate concept of creating, accessing and using a
package. (LAB PROGRAM 8)******
9 Java Program to demonstrate multithreaded programming.
(LAB PROGRAM 9)******
10 Java Program to demonstrate thread priority. (LAB PROGRAM 10)******
11 Java program to Create an applet to draw a human face.
(LAB PROGRAM 11)******
12 Java Program to demonstrate simple banner applet.
(LAB PROGRAM 12)******
13 Java program to count number of strings, integers and float values through
command line arguments.(LAB PROGRAM 13)******
14 Java program to accept a message from the keyboard and display the
number of words and non alphabetical characters.(LAB PROGRAM 14)**
15 Java program to demonstrate creation of list using an applet.
(LAB PROGRAM 15)**
16 Java program to demonstrate concept of event handling.
(LAB PROGRAM 16)**
17 Java program to demonstrate different types of fonts.
(LAB PROGRAM 17)**
18 Java program to create an applet to tokenize the strings.
(LAB PROGRAM 18)**
19 Java program to design a simple calculator using java applets.
(LAB PROGRAM 19)**
20 Java Program to implement static and dynamic stack using interface using
abstract class. (LAB PROGRAM 20)*****
Java Lab Programs

1) Java Program display Fibonacci series up to n terms using command line


arguments.(LAB PROGRAM 1)****
class Fibonacci
{
public static void main(String args[])
{
int f1=0,f2=1,f3,n;
n=Integer.parseInt(args[0]);
System.out.println("The Fibonacci series upto "+n+ " terms are : ");
System.out.println(f1+"\n"+f2+"\n");
for(int i=2;i<n;i++)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
System.out.println(f3+"\n");
}
}
}
2) Java Program to demonstrate Single Inheritance. (LAB PROGRAM 2)****

import java.io.DataInputStream;
import java.io.IOException;
class Student
{
int regno,m1,m2,m3;
String name;
DataInputStream obj = new DataInputStream(System.in);
void getdata() throws IOException
{
System.out.println("\n*************************");
System.out.println("Enter Student Information");
System.out.println("*************************\n");

System.out.print("Enter Registration Number : ");


regno = Integer.parseInt(obj.readLine());

System.out.print("Enter Name : ");


name = obj.readLine();

System.out.println("\nEnter Marks for 3 Subjects(out of 100)");


System.out.print("Enter Marks for Java : ");
m1 = Integer.parseInt(obj.readLine());

System.out.print("Enter Marks for Software Engineering : ");


m2 = Integer.parseInt(obj.readLine());

System.out.print("Enter Marks for System Programming : ");


m3 = Integer.parseInt(obj.readLine());
}
void display()
{
System.out.println("\n\n*******************");
System.out.println("Student Details");
System.out.println("*******************");

System.out.println("\nRegistration Number : "+regno);


System.out.println("Name : "+name);
System.out.println("\nMarks Scored by "+name);
System.out.println("Java : "+m1);
System.out.println("Software Engineering : "+m2);
System.out.println("System Programming : "+m3);
}
}

class Calculate extends Student


{
float total;
double percent;
void compute()
{
total = m1+m2+m3;
percent = (total/300)*100;
}

void show()
{
System.out.println("\nTotal : "+total);
System.out.println("Percenatge : "+percent);
}
}
class Single_Inheritance
{
public static void main(String args[])throws IOException
{
Calculate c1 = new Calculate();
c1.getdata();
c1.display();
c1.compute();
c1.show();
}
}
3) Java Program to sort n elements using array(Lab Program 3)******
import java.io.*;
class Sort
{
public static void main(String args[])throws IOException
{
int n,temp=0;
DataInputStream obj=new DataInputStream(System.in);
System.out.println("Enter the number of elements you want to
sort");
n=Integer.parseInt(obj.readLine());
int[] array=new int[20];
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++)
array[i]=Integer.parseInt(obj.readLine());
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
System.out.println("Sorted Elements are:");
for(int i=0;i<n;i++)
System.out.println(array[i]);
}
}
4) Java Program to implement constructor overloading by passing different
number of parameter of different types. (Lab Program 4***)
class Triangle
{
int x,y;
double z;
Triangle()
{
System.out.println("DEFAULT CONSTRUCTOR");
x=1;
y=2;
z=3;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i)
{
System.out.println("ONE PARAMETERIZED CONSTRUCTOR");
x=i;
y=i;
z=i;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i,double j)
{
System.out.println("TWO PARAMETERIZED CONSTRUCTOR");
x=i;
y=i;
z=j;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i,int j,float k)
{
System.out.println("THREE PARAMETERIZED CONSTRUCTOR");
x=i;
y=j;
z=k;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
double perimeter()
{
return(x+y+z);
}
}
class ConstructOverload
{
public static void main(String args[])
{ double res;
Triangle t1=new Triangle();
res=t1.perimeter();
System.out.println("Perimeter of Triangle (default values) = "+res+"\n");

Triangle t2=new Triangle(3);


res=t2.perimeter();
System.out.println("Perimeter of Equilateral Triangle = "+res+"\n");

Triangle t3=new Triangle(2,4.6);


res=t3.perimeter();
System.out.println("Perimeter of Isosceles Triangle = "+res+"\n");

Triangle t4=new Triangle(3,6,7.5f);


res=t4.perimeter();
System.out.println("Perimeter of Triangle = "+res+"\n");
}
}
5) Java Program to demonstrate String methods(LAB PROGRAM 5****)
class StringMethods
{
public static void main(String args[])
{
String s[]={"JAVA","java","delhi","Deharadun"," Java ","jawa"};
System.out.println("Array of Strings are:");
for(String i:s)
System.out.print(i+" ");
System.out.println("\n\nString "+s[0]+" in lowercase is
"+s[0].toLowerCase());
System.out.println("String "+s[1]+" in uppercase is
"+s[1].toUpperCase());

int n=s[1].length();
System.out.println("\nThe string "+s[1]+" consist of "+n+"
characters");

boolean b=s[0].equals(s[1]);
if(b)
System.out.println("\nString "+s[0]+" equals to "+s[1]);
else
System.out.println("\nString "+s[0]+" not equals to "+s[1]);

b=s[0].equalsIgnoreCase(s[1]);
if(b)
System.out.println("String "+s[0]+" equals to string "+s[1]+"
by ignoring case");
else
System.out.println("String "+s[0]+" not equals to "+s[1]+" by
ignoring case");

n=s[2].compareTo(s[3]);
if(n<0)
System.out.println("String "+s[2]+" is less than "+s[3]);
else if(n==0)
System.out.println("String "+s[2]+" equals to string "+s[3]);
else
System.out.println("String "+s[2]+" is greater than string
"+s[3]);

n=s[1].indexOf('v');
System.out.println("\nIndex of v in "+s[1]+" is "+n);

char c=s[1].charAt(3);
System.out.println("\nCharacter at 4th position of string "+s[1]+" is
"+c);

System.out.println("\nThe string before trimming "+s[4]+".");


System.out.println("The string after trimming "+s[4].trim()+".");

String y=s[1].concat(s[2]);
System.out.println("\nThe string "+s[1]+" & "+s[2]+" after
concatination is "+y);

String z=y.substring(3,8);
System.out.println("\nThe substring of "+y+" from 4th position to
9th position is "+z);

System.out.print("\nThe string "+s[5]+" after replacing w by v is


"+s[5].replace('w','v'));
}
}
6) Java Program to demonstrate vector methods(LAB PROGRAM 6)
import java.util.*;
class VectorMethods
{
public static void main(String args[])
{
Vector v=new Vector(3,2);
System.out.println("Initial size "+v.size());
System.out.println("Initial capacity="+v.capacity());

v.addElement("One");
v.addElement("Two");
v.addElement("Three");
System.out.println("\nVector List: "+v);
System.out.println("Size= "+v.size());
System.out.println("Capacity ="+v.capacity());

v.insertElementAt("zero",0);
v.insertElementAt("BCA",3);
v.insertElementAt("Four",5);
v.insertElementAt("BSc",6);
v.insertElementAt(20,7);
System.out.println("\nVector List: "+v);
System.out.println("size="+v.size());
System.out.println("capacity=" +v.capacity());

System.out.println("\nElement at 7th position is "+v.elementAt(6));


System.out.println("The first element in vector is
"+v.firstElement());
System.out.println("The last element in vector is
"+v.lastElement());
System.out.println("The object 'BCA' is found at position :
"+v.indexOf("BCA"));
v.removeElement("Four");
v.removeElementAt(1);
System.out.println("\nAfter removing two elements:\n"+v);
System.out.println("size="+v.size());
System.out.println("capacity=" +v.capacity());

v.removeAllElements();
System.out.println("\nAfter removing all elements:\n"+v);
System.out.println("size="+v.size());
System.out.println("capacity=" +v.capacity());
}
}
7) Java program to demonstrate concept of interface.(LAB PROGRAM 7)
import java.util.Scanner;
interface Area // Interface defined
{
final static float PI= 3.142F;
float compute ();
void display(float s);
}

class Rectangle implements Area //Interface Implementation


{
float l,b;
Rectangle(float x,float y)
{
l=x;
b=y;
}

public float compute()


{
return(l*b);
}

public void display(float s)


{
System.out.println("Area of Rectangle:"+s);
}
}
class Circle implements Area //Another implementation
{
float r;
Circle(float r)
{
this.r=r;
}

public float compute ()


{
return(PI*r*r);
}
public void display(float s)
{
System.out.println("Area of Circle:"+s);
}
}

class InterfacePrg
{
public static void main(String args[])
{
Rectangle rect = new Rectangle(10,20);
Circle cir = new Circle(10);
float x;
boolean flag=true;
Area a=null; // Interface object
Scanner sc=new Scanner(System.in);
System.out.println("\nTo calculate the Area ");
System.out.println("\nEnter the choice: R - Rectangle C - Circle");
String s=sc.nextLine();
if(s.equals("R"))
a = rect;
else if(s.equals("C"))
a=cir;
else
flag=false;
if(flag)
{
x=a.compute();
a.display(x);
}
else
System.out.println("Wrong Choice");
}
}
8) Java Program to demonstrate concept of creating, accessing and using a
package. (LAB PROGRAM 8)

Circle.java in area folder


package area;
public class Circle
{
public double cirarea(double r)
{
return(3.142*r*r);
}
}

Rectangle.java in area folder


package area;
public class Rectangle
{
public double rectarea(double l,double br)
{
return l*br;
}
}

Square.java in area folder


package area;
public class Square
{
public double sqrarea(double a)
{
return a*a;
}
}
Triangle.java in area folder
package area;
public class Triangle
{
public double triarea(double b,double h)
{
return (0.5*b*h);
}
}

PackageDemo.java outside the folder(area)


import area.*;
class PackageDemo
{
public static void main(String args[])
{
Circle c=new Circle();
System.out.println("Area of Circle:"+c.cirarea(1.5));
Square s=new Square();
System.out.println("Area of Square:"+s.sqrarea(4));
Triangle t=new Triangle();
System.out.println("Area of Triangle:"+t.triarea(3,6));
Rectangle r=new Rectangle();
System.out.println("Area of Rectangle:"+r.rectarea(10,2));
}
}
9) Java Program to demonstrate multithreaded programming.
(LAB PROGRAM 9)******
class MyThread implements Runnable
{
String name;
MyThread(String n)
{
name=n;
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(" From Thread "+name+" : "+i);
try
{
Thread.sleep(500);
}
catch(InterruptedException e){}
}
System.out.println("Exiting from thread "+name);
}
}
class ThreadDemo
{
public static void main(String args[])
{
MyThread obj=new MyThread("A");
Thread t1=new Thread(obj);
t1.start();

Thread t2=new Thread(new MyThread("B"));


t2.start();
new Thread(new MyThread("C")).start();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e){}
System.out.println("Exiting main method");
}
}

10) Java Program to demonstrate thread priority. (LAB PROGRAM 10)******


import java.io.*;
import java.lang.Thread;

class Thread1 extends Thread


{
public void run()
{
System.out.println("\n Thread A started");
for(int i = 1; i <=3; i++)
{
System.out.println("\n From Thread A\t\t i : "+i);
}
System.out.println("\nExit from Thread A");
}
}

class Thread2 extends Thread


{
public void run()
{
System.out.println("\n Thread B started");
for(int j = 1; j <=3; j++)
{
System.out.println("\n From Thread B\t\t j : "+j);
}
System.out.println("\nExit from Thread B");
}
}

class ThreadPriority
{
public static void main(String args[])throws IOException
{
Thread t=Thread.currentThread();
System.out.println("\nCurrent Thread Running is: "+t);

t.setName("Main thread");
t.setPriority(t.getPriority()-2);
System.out.println("After name and priority change of Main thread:" +t);

Thread1 t1 = new Thread1();


System.out.println("\nClass Thread1 Thread: "+t1);
t1.setName("Thread A");
t1.setPriority(Thread.MAX_PRIORITY);
System.out.println("After Name and Priority Change of Thread1:"+t1);
t1.start();

Thread2 t2 = new Thread2();


System.out.println("\nClass Thread2 Thread: "+t2);
t2.setName("Thread B");
t2.setPriority(Thread.MIN_PRIORITY);
System.out.println("After Name and Priority change of Thread2 : " +t2);
t2.start();
for(int k=1;k<=3;k++)
{
System.out.println("\nFrom Main Thread\t\tk : "+k);
}
System.out.println("\nEnd of MainThread");
}
}

11) Java program to Create an applet to draw a human face.


(LAB PROGRAM 11)******
import java.applet.*;
import java.awt.*;
/*
<applet code = HumanFace.class width=500 height=500>
</applet>
*/
public class HumanFace extends Applet
{
//Initialize the applet
public void init()
{
setBackground(Color.white);
}

//Draw the human face


public void paint(Graphics g)
{
//Change color to cream
Color clr=new Color(255,179,86);
g.setColor(clr);

//Draw and fill the face


g.drawOval(100,100,250,300);
g.fillOval(100,100,250,300);
//Change color to black
g.setColor(Color.black);

//Draw the left eye


g.drawOval(160,185,40,25);
g.fillOval(160,185,40,25);

//Draw the right eye


g.drawOval(250,185,40,25);
g.fillOval(250,185,40,25);

//Draw the Left Eyebrow


g.drawArc(160,170,35,10,0,180);

//Draw the Right Eyebrow


g.drawArc(250,170,35,10,0,180);

//Draw the Nose


g.drawLine(210,265,210,275);
g.drawLine(240,265,240,275);
g.drawArc(210,275,30,10,0,-180);

//Draw the smile


g.drawArc(175,300,100,50,0,-180);
}
}
12) Java Program to demonstrate simple banner applet.
(LAB PROGRAM 12)******

import java.awt.*;
import java.applet.*;
/*
<applet code="Banner.class" width=1000 height=1000>
</applet>
*/
public class Banner extends Applet implements Runnable
{
String msg=" JSS SMI College ";
Thread t;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.blue);
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
try
{
repaint();
Thread.sleep(200);//delays each thread by 250ms
//shifts the first character of banner text to the last position
msg=msg.substring(1)+msg.charAt(0);
}
catch(InterruptedException e)
{
}
}
}
public void paint(Graphics g)
{
Font fl=new Font("Times New Roman", Font.BOLD,45);
g.setFont(fl);
g.drawString(msg,300,300);
}
}

13) Java program to count number of strings, integers and float values through
command line arguments.(Lab program 13)******
class CmdLine
{
public static void main(String args[])
{
int cs=0,ci=0,cf=0;
for(int i=0;i<args.length;i++)
{
try
{
int j= Integer.parseInt(args[i]);
ci++;
}
catch(NumberFormatException e)
{
String s=args[i];
try
{
float f=Float.parseFloat(s);
cf++;
}
catch(NumberFormatException n)
{
cs++;
}
}
}
System.out.println("String:"+cs+"\nIntegers:"+ci+"\nFloat:"+cf);
}
}

14) Java program to accept a message from the keyboard and display the number
of words and non alphabetical characters.(Lab Program 14)****
import java.util.Scanner;
class Count
{
public static void main(String args[])
{
String s;
int i,alph=1,nonalph=0;
char ch;
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the message");
s=sc.nextLine();
String s2=s.trim();
System.out.println(s2.length());
if(s2.length()==0)
{
alph=0;
}
else
{
for(i=0;i<s2.length();i++)
{
ch=s2.charAt(i);
if(ch==' ')
alph++;
else if(Character.isLetter(ch)==false)
nonalph++;
}
}
System.out.println("\nNumber of Words="+alph);
System.out.println("\nNumber of Non Alphabetical Characters = "+nonalph);
}
}

15) Java program to demonstrate creation of list using an applet.


(LAB PROGRAM 15)*****
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*<applet code = "ListDemo.class" height = 500 width = 900></applet>*/


public class ListDemo extends Applet implements ItemListener
{
List ls;
Label l1, l2;
TextArea t1;
public void init()
{ setLayout(new FlowLayout());
ls= new List();
ls.add("Red");
ls.add("Black");
ls.add("Blue");
ls.add("Yellow");
ls.add("Green");
ls.add("Gray");
ls.add("Pink");
ls.add("Purple");
l1=new Label("Please select your favorite color");
t1=new TextArea(15,25);
add(l1);
add(ls);
add(t1);
ls.addItemListener(this);
}

public void itemStateChanged(ItemEvent e)


{
List l = (List)e.getSource();
t1.append("Item Event Selected : "+l.getSelectedItem()+"\n");
}
}

OUTPUT
16) Java program to demonstrate concept of event handling.
(LAB PROGRAM 16)*****

import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
/*<applet code="MouseEventprg.class" height=500 width=1000></applet>*/
public class MouseEventprg extends Applet implements MouseListener
{
String msg="";
int x=50,y=100;
public void init()
{
setBackground(Color.PINK);
setForeground(Color.BLUE);
Font f=new Font("Arial",Font.BOLD,26);
setFont(f);
addMouseListener(this);

}//end of init

public void mouseClicked(MouseEvent me)


{
msg="Mouse Clicked at Co-ordinates " +me.getX()+" , "+me.getY();
}

public void mouseEntered(MouseEvent me)


{
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="Mouse Pressed";
repaint();
}

public void mouseReleased(MouseEvent me)


{
x=me.getX();
y=me.getY();
msg="Mouse Released";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);

}//end of paint

}//end of class
17) Java program to demonstrate different types of fonts.
(LAB PROGRAM 17)*****
import java.awt.*;
import java.applet.*;
/*<applet code = "FontDemo.class" height = 500 width =800></applet>*/
public class FontDemo extends Applet
{
Font f1,f2,f3,f4;
public void init()
{
setForeground(Color.BLUE);
f1 = new Font("Algerian",Font.BOLD |Font.ITALIC,40);
f2 = new Font("Bookman Old Style",Font.BOLD,24);
f3 = new Font("Cooper Black",Font.ITALIC,28);
f4 = new Font("Times New Roman",Font.PLAIN,32);
}

public void paint(Graphics g)


{
g.setFont(f1);
g.drawString("Features of Java",50,50);

g.setFont(f2);
g.drawString("1) Object Oriented, Robust & Secure",60,100);

g.setFont(f3);
g.drawString("2) Portable & Platform Independent",60,130);

g.setFont(f4);
g.drawString("3) Multithreaded",60,160);
}
}
18) Java program to create an applet to tokenize the strings.
(LAB PROGRAM 18)*****
import java.awt.*;
import java.applet.*;
import java.util.StringTokenizer;
/*
<applet code="StrToken.class" width=400 height=500></applet>
*/
public class StrToken extends Applet
{
public void init()
{
setBackground(Color.pink);
setForeground(Color.blue);
}
public void paint(Graphics g)
{
try
{
Font fl=new Font("Bookman Old Style",Font.BOLD,45);
g.setFont(fl);
StringTokenizer st = new StringTokenizer("This is a java
programming lab"," ");
while(st.hasMoreTokens())
{
for(int i=10;i<500;i+=50)
{
g.drawString(st.nextToken(),20,i+60);
}
}
}
catch(Exception e){}
}
}
19) Java program to design a simple calculator using java applets.
(LAB PROGRAM 19)**
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code = "Calculator.class" width=500 height=500>
</applet>
*/
public class Calculator extends Applet implements ActionListener
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1,b2,b3,b4;

public void init()


{
setBackground(Color.pink);
setForeground(Color.black);
setLayout(null);

l1=new Label("First Number:");


l1.setBounds(10,20,100,20);
add(l1);

t1=new TextField(20);
t1.setBounds(120,20,60,20);
add(t1);

l2=new Label("Second Number:");


l2.setBounds(10,40,100,20);
add(l2);
t2=new TextField(20);
t2.setBounds(120,40,60,20);
add(t2);

l3=new Label("Result");
l3.setBounds(10,60,100,20);
add(l3);

l4=new Label();
l4.setBounds(120,60,100,20);
add(l4);

b1=new Button("Add");
b1.setBounds(10,80,30,30);
add(b1);

b2=new Button("Sub");
b2.setBounds(40,80,30,30);
add(b2);

b3=new Button("Mul");
b3.setBounds(70,80,30,30);
add(b3);

b4=new Button("Div");
b4.setBounds(100,80,30,30);
add(b4);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Double num1=Double.parseDouble(t1.getText());
Double num2=Double.parseDouble(t2.getText());
if(ae.getSource()==b1)
{
Double value=num1+num2;
l4.setText(""+value);
}

if(ae.getSource()==b2)
{
Double value=num1-num2;
l4.setText(""+value);
}

if(ae.getSource()==b3)
{
Double value=num1*num2;
l4.setText(""+value);
}

if(ae.getSource()==b4)
{
Double value=num1/num2;
l4.setText(""+value);
}
}
}
20) Java Program to implement static and dynamic stack using interface using
abstract class. (LAB PROGRAM 20)*****
import java.util.*;
interface stackop
{
void push(int item);
void pop();
}
abstract class FixedStack implements stackop
{
int stk[ ];
int tos;
void getdata(int size)
{
stk=new int[size];
tos=-1;
int a=stk.length;
System.out.println("Fixed Stack Length="+a);
}
public void push(int item)
{
if(tos==stk.length-1)
{
System.out.println("Stack is full.\n");
}
else
System.out.println(stk[++tos]=item);
}
public void pop()
{
if(tos==-1)
{
System.out.println("All elements are popped out!Stack
underflows");
}
else
System.out.println(stk[tos--]);
}
}
class DynStack extends FixedStack implements stackop
{
DynStack(int size)
{
stk=new int[size];
tos=-1;
int a=stk.length;
}
public void push(int item)
{
if(tos==stk.length-1)
{
System.out.println(("Stack is full. Increasing the capacity."));
int t[ ]=new int[stk.length * 2]; //double size
for(int i=0;i<stk.length;i++)
t[i]=stk[i];
stk=t;
int a=stk.length;
System.out.println("Length(Double Size)="+a);
System.out.println(stk[++tos]=item);
}
else
System.out.println(stk[++tos]=item);

}
public void pop()
{
if(tos==-1)
{
System.out.println("Stack underflows");

}
else
System.out.println(stk[tos--]);
}
}
class StackTest extends FixedStack
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);

int n=3;
System.out.println("The size of Fixed Stack:"+n);
System.out.print("\nEnter the size of Dynamic Stack:");
int p=sc.nextInt();
DynStack ds=new DynStack(p);

stackop mystk; //Interface Object


StackTest fs=new StackTest();
mystk=fs;
System.out.println("\nFixed Stack Operations:");
fs.getdata(n);
System.out.println("Inserting Elements into stack are:");
for(int i=0;i<4;i++)
mystk.push(i);

System.out.println("Fixed length Stack Contents are.");


for(int i=0;i<4;i++)
mystk.pop();

mystk=ds;
System.out.println("\nDynamic Stack Operations:");
System.out.println("Initial Length of dynamic stack="+p);
System.out.println("Inserting Elements into stack are:");
for(int i=0;i<(p+2);i++)
mystk.push(i);
System.out.println("\nDynamic length Stack Contents are");
for(int i=0;i<(p+2);i++)
mystk.pop();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy