Java Lab Manual
Java Lab Manual
JAVA Programming
LAB
MANUAL
18MCA26
class Room
{
float length,breadth;
Room(float x,float y)
{
length=x;
breadth=y;
}
Room(float x)
{
length=breadth=x;
}
float area()
{
return(length*breadth);
}
float area(float z)
{
return(length*breadth*z);
}
}
public class Demo
{
public static void main(String a[])
{
float ar;
Room r1=new Room(25.0f,10.0f);
Room r2=new Room(20.0f);
ar=r1.area();
System.out.println("the constractor overloding Area: "+ar);
ar=r2.area(30.0f);
System.out.println("the Method overloding Area: "+ar);
}
}
------Output:------
C:\Users\MCAstudent>javac Demo.java
C:\Users\MCAstudent>java Demo
the constractor overloding Area: 250.0
the Method overloding Area: 12000.0
class out
{
private int x=10;
void showout()
{
System.out.println("X inside showout:" + x);
In in=new In();
System.out.println("in.y:"+ in.y);
}
class In
{
private int y=20;
void showin()
{
System.out.println("X inside showin:" + x);
System.out.println("Y inside showin:" + y);
showout();
}
}
}
class lab1b
{
public static void main(String args[])
{
out.In in=new out().new In();
in.showin();
}
}
-----OUT-PUT-----
C:\User\MCAstudent\lab1b java
X inside showin:10
Y inside showin:20
X inside showout:10
in.y:20
import java.util.*;
class StringManupulation
{
public static void main(String ar[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter a String : ");
String s1 = s.nextLine();
StringBuffer str = new StringBuffer(s1);
System.out.println("Original String : "+str);
System.out.println("Capacity : "+str.capacity());
StringBuffer rev = new StringBuffer(str).reverse();
String uper = rev.toString();
uper=uper.toUpperCase();
System.out.println("The Result of Reverse:
"+rev+"\nUperCase: "+uper);
System.out.println("Enter a String : ");
String s3 = s.nextLine();
StringBuffer ap = new StringBuffer(uper);
ap.append(s3);
System.out.println("The Result of Append: "+ap);
}
}
-----Out-put:-----
C:\Users\MCAstudent>javac StringManupulation.java
C:\Users\MCAstudent>java StringManupulation
Enter a String :
Java
Original String : Java
Capacity : 21
The Result of Reverse: avaJ
UperCase: AVAJ
Enter a String :
Lab
The Result of Append: AVAJLab
class demo1
{
int n1,n2;
public void getdata1(int a,int b)
{
n1=a;
n2=b;
}
public void showdemo1()
{
System.out.println("N1=" +n1);
System.out.println("N2=" +n2);
}
}
class demo2 extends demo1
{
int n3,n4;
public void getdata2(int a,int b)
{
n3=a;
n4=b;
}
public void showdemo2()
{
System.out.println("N3=" + n3);
System.out.println("N4=" + n4);
}
}
class lab2a
{
public static void main(String args[])
{
demo2 ob=new demo2();
ob.getdata1(10,20);
ob.getdata2(30,40);
ob.showdemo1();
ob.showdemo2();
}
}
-----Out-Put-----
C:\User\MCAstudents\lab2a java
N1=10
N2=20
N3=30
N4=40
{
return((x*y)/2);
}
}
class InterfaceTest
{
public static void main(String a[])
{
Rectangle r = new Rectangle();
Triangle t = new Triangle();
Area area; //interface object
area = r; //refer
System.out.println("Area of Rectangle =
"+area.computeArea(10,20));
area = t; //refer
System.out.println("Area of Triangle =
"+area.computeArea(10,20));
}
}
-----Output:-----
C:\Users\MCAstudent>javac InterfaceTest.java
C:\Users\MCAstudent>java InterfaceTest
Area of Rectangle = 200.0
Area of Triangle = 100.0
import java.io.*;
class LessBalanceException extends Exception
{
int bal;
public LessBalanceException(int b)
{
bal=b;
}
public String toString()
{
return "Withdraw Amount:" +bal +" Is Not Valid";
}
}
class Account
{
String accountNo;
String name;
String accountType;
String address;
int initial;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
public void createAccount()throws Exception
{
accountNo=String.valueOf(this);
System.out.println("Your Account Number is:"+accountNo);
System.out.println("Enter Name:");
name=br.readLine();
System.out.println("Enter Account Type:");
accountType=br.readLine();
System.out.println("Enter Address:");
address=br.readLine();
System.out.println("Enter Initial Amount Should Not Be Less Than
500.");
initial=Integer.parseInt(br.readLine());
System.out.println("******************************************
*");
}
public void showAccount()
{
System.out.println("Account No:"+accountNo);
System.out.println("Person Name:"+name);
System.out.println("Account Type:"+accountType);
System.out.println("Address:"+address);
System.out.println("Initial Amount:"+initial);
System.out.println("******************************************
**");
}
public void withdraw()throws Exception
{
System.out.println("Enter Withdraw Amount:");
int amt=Integer.parseInt(br.readLine());
try
{
if((initial-amt)<500)
{
throw new LessBalanceException(amt);
}
else
{
initial=initial-amt;
}
}
catch(LessBalanceException e)
{
System.out.println(e);
}
}
public void deposit()throws Exception
{
System.out.println("Enter Deposit Amount:");
int amt=Integer.parseInt(br.readLine());
initial=initial+amt;
}
}
class lab3
{
public static void main(String args[])
{
Account a1=new Account();
Account a2=new Account();
try
{
a1.createAccount();
a1.deposit();
a1.withdraw();
a1.showAccount();
a2.createAccount();
a2.deposit();
a2.withdraw();
a2.showAccount();
}
catch(Exception e)
{}
}
}
-----Out-Put-----
C:\User\MCAstudent\lab3a java
Your Accout number is:Account@b8df17
Entter name:
Sabu
Enter Account Type:
Savings
Enter Address:
Saptagiri
Enter initial amount that shouldnot be less then 500
800
Enter the deposit amount:
1000
Enter the withdraw amount:
300
Account no:Account@b8df17
Person name:sabu
Account type:Savings
Address:saptagiri
Initial amount:1500
Your Accout number is:Account@1be2d65
Entter name:
Sahana
Enter Account Type:
Savings
Enter Address:
class Resource
{
int n;
boolean val=false;
synchronized int get()
{
while(!val)
{
try
{
wait();
}
catch(Exception e)
{}
}
System.out.println("Consumer:"+n);
try
{
Thread.sleep(500);
}
catch(Exception e1)
{}
val=false;
notify();
return n;
}
synchronized void put(int n)
{
while(val)
{
try
{
wait();
}
catch(Exception e)
{}
}
this.n=n;
val=true;
System.out.println("Producer:"+n);
try
{
Thread.sleep(500);
}
catch(Exception e1)
{}
notify();
}
}
class producer implements Runnable
{
Resource q;
producer(Resource q)
{
this.q=q;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Resource q;
consumer(Resource q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
-----OutPut-----
import java.util.Scanner;
import java.lang.Exception;
int front,rear,size=10;
int q[]=new int[size];
Queue() //Constructor for intialization
{
rear=front=-1;
}
void q_insert(int n)throws MyException
{
if(rear==size-1)
throw new MyException("Queue is full");
rear++;
q[rear]=n;
if(front==-1)
front=0;
}
int q_delete()throws MyException
{
if(front==-1)
throw new MyException("Queue is empty");
int temp=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return(temp);
}
void q_display()throws MyException
{
if(front==-1)
throw new MyException("Queue is empty");
else
{
System.out.println("\ncotents of queue");
for(int i=front;i<=rear;i++)
System.out.print("\t"+q[i]);
}
}
}
break;
case 4:
System.exit(0);
default:
System.out.println("no choice!");
break;
}
}
}
catch(MyException e)
{
System.out.println(e.getMessage());
}
}
}
Output:
C:\Users\Mangaldeep Sarkar>javac UseQueue.java
C:\Users\Mangaldeep Sarkar>java UseQueue
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
1
enter the item
500
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
1
enter the item
600
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
3
cotents of queue
500 600
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
1
enter the item
900
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
3
cotents of queue
500 600 900
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
2
the deleted item is500
Menu Queue
1.insert
2.delete
3.display
4.exit
enter your choice
4
Step 1:
Create a sub folder on the path
C:\MCAlab\java>mkdir shape
C:\MCAlab\java>cd shape
Following file save on shape folder
package shape;
public class Circle
{
double radius;
public Circle(double r)
{
radius=r;
}
public double perimeter()
{
return 2*Math.PI*radius;
}
public double area()
{
return (Math.PI*radius*radius);
}
}
C:\MCAlab\java\shape>javac Square.java
C:\MCAlab\java\shape>javac Circle.java
C:\MCAlab\java\shape>javac Triangle1.java
b=sc.nextDouble();
System.out.print("C: ");
c=sc.nextDouble();
Triangle1 ta=new Triangle1(bs,h,a,b,c);
System.out.println("Area: "+ta.area()+"\t\t"+"Perimeter: "+ta.perimeter());
}
void circleTest()
{
Scanner sc=new Scanner(System.in);
System.out.println("Area & Perimeter of Circle");
System.out.print("Enter Radious: ");
r=sc.nextDouble();
Circle cr=new Circle(s);
System.out.println("Area: "+cr.area()+"\t\t"+"Perimeter: "+cr.perimeter());
}
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
shapeTest st=new shapeTest();
int ch;
while(true)
{
System.out.println("\n\n1. Square \t 2.Triangle \t 3. Circle \t
4.Exit");
System.out.print("\nEnter your choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1:
st.squareTest();
break;
case 2:
st.triangleTest();
break;
case 3:
st.circleTest();
break;
case 4:
System.exit(0);
default: System.out.println("Invalid!");
break;
}
}
}
}
Lab5.java
**********************************************************
********
import java.io.*;
class Lab5
{
public static void main(String s[])throws IOException
{
MyStack istack;
FixedStack fs=new FixedStack(5);
DynamicStack ds=new DynamicStack(5);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int ch;
while(true)
{
System.out.println("\n1.Create fixed stack.");
System.out.println("\n2.Create dynamic stack.");
System.out.println("\n3.Exit.");
System.out.println("\nEnter your choice:");
ch=Integer.parseInt(br.readLine());
System.out.println("\n");
switch(ch)
{
case 1: istack=fs;
for(int item=1;item<=5;item++)
istack.push(item);
fs.show();
break;
case 2: istack=ds;
for(int item=1;item<=10;item++)
istack.push(item);
ds.show();
break;
default:System.exit(1);
}
}
}
}
i)MyStack.java
**********************************************************
*****
public interface MyStack
{
void push(int item);
int pop();
}
ii) FixedStack.java
**********************************************************
****
class FixedStack implements MyStack
{
int arr[];
int top;
public FixedStack(int size)
{
arr=new int [size];
top=-1;
}
public void push(int item)
{
if(top==arr.length-1)
System.out.println("Stack full.");
else
{
top++;
arr[top]=item;
}
}
public int pop()
{
if(top==-1)
{
System.out.println("Stack underflow.");
return 0;
}
else
return arr[top--];
}
public void show()
{
for(int i=0;i<=top;i++)
System.out.println(arr[i]+" ");
}
}
iii)
DynamicStack.java******************************************
******************
class DynamicStack implements MyStack
{
int arr[];
int top;
public DynamicStack(int size)
{
arr=new int[size];
top=-1;
}
public void push(int item)
{
if(top==arr.length-1)
{
int temp[]=new int[arr.length+1];
for(int i=0;i<arr.length;i++)
temp[i]=arr[i];
arr=temp;
top++;
arr[top]=item;
}
else
{
top++;
arr[top]=item;
}
}
public int pop()
{
if(top==-1)
{
System.out.println("Stack underflow.");
return 0;
}
else
return arr[top--];
}
public void show()
{
for(int i=0;i<=top;i++)
System.out.println(arr[i]+" ");
}
}
import java.io.*;
class Lab8
{
public static void main(String s[])throws IOException
{
String file1,file2;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
try
{
System.out.println("Enter file to read:");
file1=br.readLine();
System.out.println("Enter file to write:");
file2=br.readLine();
FileInputStream f1=new FileInputStream (file1);
FileOutputStream f2=new FileOutputStream (file2);
int val=0;
val=f1.read();
while(val!=-1)
{
f2.write(val);
val=f1.read();
}
f1.close();
f2.close();
}
catch(FileNotFoundException e)
{
System.out.println("file is not available");
}
catch(Exception e1)
{
System.out.println("e1");
}
}
}
int pos,ch;
String item;
while(true)
{
System.out.println("1.Add First.");
System.out.println("2.Add Last.");
System.out.println("3.Add At Position.");
System.out.println("4.Remove At Position.");
System.out.println("5.Size Of List.");
System.out.println("6.Display Items.");
System.out.println("Enter Your Choice:");
item=br.readLine();
ch=Integer.parseInt(item);
switch(ch)
{
case 1:
System.out.println("Enter Item To Store:");
item=br.readLine();
list.addFirst(item);
System.out.println("..........................................");
break;
case 2:
System.out.println("Enter Item To Store:");
item=br.readLine();
list.addLast(item);
System.out.println("..........................................");
break;
case 3:
System.out.println("Enter Position:");
item=br.readLine();
pos=Integer.parseInt(item);
System.out.println("Enter Item To Store:");
item=br.readLine();
try
{
list.add(pos,item);
}
catch(Exception e)
{
System.out.println("Invalid Position.");
}
System.out.println("..........................................");
break;
case 4:
System.out.println("Enter Position To Delete:");
item=br.readLine();
pos=Integer.parseInt(item);
try
{
list.remove(pos);
}
catch(Exception e)
{
System.out.println("Invalid Position.");
}
System.out.println("..........................................");
break;
case 5:
System.out.println("Size Of List:"+list.size());
System.out.println("..........................................");
break;
case 6:
System.out.println("Items In The List.");
System.out.println(list);
System.out.println("..........................................");
break;
default:
System.exit(1); } } } }
12. Write a JAVA program which uses Datagram Socket for Client
Server Communication.
import java.net.*;
class udpip_server
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static void Myserver() throws Exception
{
int pos=0;
while(true)
{
int c=System.in.read();
switch(c)
{
case -1: System.out.println("Server quits");
return;
case '\r':break;
case '\n':ds.send(new
DatagramPacket(buffer,pos,InetAddress.getLocalHost(),777));
pos=0;
break;
default:
buffer[pos++]=(byte) c;
}
}
}
public static void main(String args[]) throws Exception
{
System.out.println("Server ready..\n Please type here");
ds=new DatagramSocket(888);
Myserver();
}
}
*********************************
udpip_client**********************************
import java.net.*;
class udpip_client
{
public static DatagramSocket ds;
public static byte buffer[]=new byte[1024];
public static void Myclient() throws Exception
{
while(true)
{
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
public static void main(String args[]) throws Exception
{
System.out.println("Client - Press CTRL+C to quit");
ds=new DatagramSocket(777);
Myclient();
}
}
Note 1 : In the practical Examination student has to execute one program from a lot of all
the 12 questions and demonstrates Part B Mini Project.