Java Lab Manual - IT2305
-
Upload
ravijithan -
Category
Documents
-
view
68 -
download
1
description
Transcript of Java Lab Manual - IT2305
Develop a Java package with simple Stack and Queue classes. Use JavaDoc comments for documentation
package of stack and queue in java,java stack and queue package,stack and queue program,stack and queue in java.Queue package:package queuepackage;
public class queue2
{
private int maxsize;
private long[] queArray;
private int front;
private int rear;
private int nitems;
public queue2(int s)
{
maxsize=s;
queArray=new long[maxsize];
front=0;
rear=-1;
nitems=0;
}
public void insert(long j)
{
if(rear==maxsize-1)
rear=-1;
queArray[++rear]=j;
nitems++;
}
public long remove()
{
long temp=queArray[front++];
if(front==maxsize)
front=0;
nitems--;
return temp;
}
public long peekFront()
{
return queArray[front];
}
public boolean isEm
pty()
{
return(nitems==0);
}
public boolean isFull()
{
return(nitems==maxsize);
}
public int size()
{
return nitems;
}
}
Stack package:package stackpackage;
public class stack2
{
int []a;
int top;
public stack2(int n)
{
a=new int[n];
top=-1;
}
public void push(int val)
{
if(top==a.length-1)
{
System.out.println("stack overflow");
}
else
{
top++;
a[top]=val;
}
}
public void pop()
{
if(top==-1)
{
System.out.println("stack underflow");
}
else
{
System.out.println("element popped"+a[top]);
top--;
}
}
public void display()
{
if(top==-1)
{
System.out.println("stack empty");
}
else
{
for(int i=top;i>=0;i--)
{
System.out.println("stack element :"+a[i]);
}
}
}
}Main program:import queuepackage.queue2;
import stackpackage.stack2;
import java.io.*;
public class usestackqueue2
{
public static void main(String args[])
{
BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
int c;
stack2 s;
int n;
try
{
do
{
System.out.println("1.stack 2.queue");
c=Integer.parseInt(sc.readLine());
switch(c)
{
case 1:
System.out.println("enter the size of stack");
n=Integer.parseInt(sc.readLine());
s=new stack2(n);
int choice;
do
{
System.out.println("1.push,2.pop,3.display,0.exit,enter your choice:");
choice=Integer.parseInt(sc.readLine());
switch(choice)
{
case 1:
int value;
System.out.println("enter the element to push:");
value=Integer.parseInt(sc.readLine());
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 0:
break;
default:System.out.println("invalid choice");
}
}while(choice!=0);
break;
case 2:
queue2 thequeue = new queue2(5);
thequeue.insert(10);
thequeue.insert(20);
thequeue.insert(30);
thequeue.insert(40);
thequeue.remove();
thequeue.remove();
thequeue.remove();
thequeue.insert(50);
thequeue.insert(60);
thequeue.insert(70);
thequeue.insert(80);
while(!thequeue.isEmpty())
{
long n1= thequeue.remove();
System.out.print(n1);
System.out.print("");
}
System.out.println("");
break;
}
}while(c!=0);
}
catch(Exception e)
{}
}
}
Output:C:\j2sdk1.4.0\bin>javac usestackqueue2.java
C:\j2sdk1.4.0\bin>java usestackqueue2
1.stack 2.queue
1
enter the size of stack
5
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
1
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
2
1.push,2.pop,3.display,0.exit,enter your choice:
1
enter the element to push:
3
1.push,2.pop,3.display,0.exit,enter your choice:
3
sstack element :3
sstack element :2
sstack element :1
1.push,2.pop,3.display,0.exit,enter your choice:
2
element popped3
1.push,2.pop,3.display,0.exit,enter your choice:
3
sstack element :2
sstack element :1
1.push,2.pop,3.display,0.exit,enter your choice:
0
1.stack 2.queue
0
1.stack 2.queue
2
4050607080
1.stack 2.queue
0
1.stack 2.queue
2
4050607080
1.stack 2.queue
0
Design a class for Complex numbers in Java. In addition to methods for basic operations on complex numbers, provide a method to return the number of active objects created
Program:import java.io.*;
class complex
{
int a,b;
public static int c;
public complex(int x,int y)
{
a=x;b=y;
c++;
}
public static String add(complex n1,complex n2)
{
int a1=n1.a+n2.a;
int b1=n1.b+n2.b;
if(b12 && year%4==0)
day_mon[1]=29;
for(m=0;mjava chkd
enter the date:(dd mm yyyy):
30
04
1989
30-4-1989 this is :=sunday
C:\j2sdk1.4.0\bin>java chkd
enter the date:(dd mm yyyy):
30
02
2000
invalid......Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism
abstract class Shape{double dim1;double dim2;double PI=3.14;Shape(double a, double b){dim1 = a;dim2 = b;}abstract double area();}class Rectangle extends Shape{Rectangle(double a, double b){super(a, b);}double area(){System.out.println("Inside Area for Rectangle.");return dim1 * dim2;}}class Triangle extends Shape{Triangle(double a, double b){super(a, b);}double area() {System.out.println("Inside Area for Ellipse.");return PI * dim1 * dim2;}}class Square extends Shape{Square(double a, double b){super(a, b);}double area() {System.out.println("Inside Area for Square.");return dim1 * dim1;}}class AbstractAreas {public static void main(String args[]) {Rectangle r = new Rectangle(9, 5);Triangle t = new Triangle(10, 8);Circle c=new Circle(5,5);Ellipse e=new Ellipse(7,7);Square s=new Square(6,6);Shape figref; // this is OK, no object is createdfigref = r;System.out.println("Area is " + figref.area());figref = t;System.out.println("Area is " + figref.area());figref = c;System.out.println("Area is " + figref.area());figref = e;System.out.println("Area is " + figref.area());}figref = s;System.out.println("Area is " + figref.area());}}Design a Java interface for ADT Stack. Develop two different classes that implement this interface, one using array and the other using linked-list. Provide necessary exception handling in both the implementations.
Program:import java.io.*;
interface stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements stackoperation
{
int stack[];
int top;
Astack()
{
stack=new int[10];
top=0;
}
public void push(int item)
{
if(stack[top]==10)
System.out.println("overflow");
else
{
stack[++top]=item;
System.out.println("item pushed");
}
}
public void pop()
{
if(stack[top]java sample
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
1
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
1
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
2
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
1
enter the value to push:
3
item pushed
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
3
the elements are:
element:1
element:2
element:3
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
2
item popped
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
3
the elements are:
element:1
element:2
ARRAY STACK
1.push 2.pop 3.display 4.exit
enter ur choice:
4
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
2
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
2
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
1
enter the value for push:
3
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
3
the elements are:3
the elements are:2
the elements are:1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
2
popped element:3
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
3
the elements are:2
the elements are:1
LIST STACK:
1.push 2.pop 3.display 4.exit
enter your choice:
4
----------------------------------
1.Arraystack 2.liststack 3.exit
-----------------------------------
enter ur choice:
3
Write a Java program to read a file that contains DNA sequences of arbitrary length one per line (note that each DNA sequence is just a String). Your program should sort the sequences in descending order with respect to the number of 'TATA' subsequences present. Finally write the sequences in sorted order into another file
PROGRAM import java.io.*;
class files
{
String s[]=new String[100];
String str="",m,t;
int x,size=0,i,tem,j=0,chg;
int a[]=new int[100];
int b[]=new int[100];
int c[]=new int[100];
files(String s1,String s2)throws IOException
{
File r=new File(s1);
File w=new File(s2);
FileInputStream fin=new FileInputStream(r);
FileOutputStream fout=new FileOutputStream(w);
do{
i=fin.read();
if(i!=-1)
{
{
str=str+(char)i;
if((char)i=='\n')
{
s[j]=str;
j++;
size++;
str="";
}
}
}
while(i!=-1);
for(int l=0;l
{
if(s[l]==null)
break;
m=s[l];
t="tata";
b[l]=a[l]=search(m,t);
}
for(int p=0;p
{
for(int q=0;q
{
if(b[q]
{
tem=b[q];
b[q]=b[q+1];
b[q+1]=tem;
}
}
}
for(int h=0;h{
x=b[h];
c[h]=linear(x,a,size);
}
for(int y=0;y
{
chg=c[y];
byte buf[]=s[chg].getBytes();
fout.write(buf);
}
}
static int linear(int x,int a[],int size)
{
int m,d,item=x,siz=size;
for(d=0;djavac filehand.java
C:\j2sdk1.4.0\bin>java filehand
enter the read file name :
input.txt
enter the write file name:
output.txt
successfully writed
Input.txtjfjksdf tata fji
fjksdfi tatt fg tata taata tata
jfijsdf tata tjkltj tatta tata tata
Output.txtjfijsdf tata tjkltj tatta tata tata
fjksdfi tatt fg tata taata tata
jfjksdf tata fji