0% found this document useful (0 votes)
215 views36 pages

Java Lab Manual

This document contains a lab manual for a Java Programming course. It provides instructions and sample code for 4 programming assignments: 1) demonstrating constructor and method overloading, 2) string handling methods, 3) inheritance concepts, and 4) exception handling. The assignments cover core Java topics like classes, objects, inheritance, interfaces, exceptions and string/input-output operations. Sample code and output is provided for each programming task to help students understand and implement the concepts.

Uploaded by

Tejaswini
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
215 views36 pages

Java Lab Manual

This document contains a lab manual for a Java Programming course. It provides instructions and sample code for 4 programming assignments: 1) demonstrating constructor and method overloading, 2) string handling methods, 3) inheritance concepts, and 4) exception handling. The assignments cover core Java topics like classes, objects, inheritance, interfaces, exceptions and string/input-output operations. Sample code and output is provided for each programming task to help students understand and implement the concepts.

Uploaded by

Tejaswini
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 36

Java Programming Lab(18MCA26) 2018-2019

JAVA Programming

LAB

MANUAL

18MCA26

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

1. a. Write a JAVA Program to demonstrate Constructor


Overloading and Method Overloading.

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);

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

b.Write a JAVA Program to implement Inner class and demonstrate


its Access protection.

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();

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

}
}
}
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

2. Write a program in Java for String handling which performs the


following:
i) Checks the capacity of StringBuffer objects.
ii) Reverses the contents of a string given on console and converts
the resultant string in
upper case.
iii) Reads a string from console and appends it to the resultant string
of ii.

import java.util.*;
class StringManupulation
{
public static void main(String ar[])

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

3. a. Write a JAVA Program to demonstrate Inheritance.

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[])

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
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

b. Simple Program on Java for the implementation of Multiple


inheritance using interfaces to calculate the area of a rectangle and
triangle.

interface Area //Interface Defied


{
float computeArea(float x,float y);
}
class Rectangle implements Area //interface implemented
{
public float computeArea(float x,float y)
{
return(x*y);
}
}
class Triangle implements Area //another implementation
{
public float computeArea(float x,float y)

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
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

4. Write a JAVA program which has


i. A Class called Account that creates account with 500Rs minimum
balance, a deposit() method to deposit amount, a withdraw() method
to withdraw amount and also throws LessBalanceException if an
account holder tries to withdraw money which makes the balance
become less than 500Rs.
ii. A Class called LessBalanceException which returns the statement
that says withdraw amount ( Rs) is not valid.

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

iii. A Class which creates 2 accounts, both account deposit money


and one account tries to withdraw more money which generates a
LessBalanceException take appropriate action for the same.

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:");

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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);

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

}
}
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:

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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:

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

5. Write a JAVA program using Synchronized Threads, which


demonstrates Producer Consumer concept.

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
{

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
Resource q;
consumer(Resource q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}

-----OutPut-----

6. Write a JAVA program to implement a Queue using user defined


Exception Handling (also make use of throw, throws.).

import java.util.Scanner;
import java.lang.Exception;

class MyException extends Exception //User defined Exception


Inherited
{
MyException(String m)
{
super(m);
}
}
class Queue
{

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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]);

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

}
}
}

public class UseQueue


{
public static void main(String args[])
{
int ch,item;
Scanner S1=new Scanner(System.in);//for console input.
Object declration
Queue a=new Queue(); //Object declration
try{
while(true)
{
System.out.println("\nMenu Queue");
System.out.println("1.insert");
System.out.println("2.delete");
System.out.println("3.display");
System.out.println("4.exit");
System.out.println("enter your choice");
ch=S1.nextInt();
switch(ch)
{
case 1:
System.out.println("enter the item");
item=S1.nextInt();
a.q_insert(item);
break;
case 2:
item=a.q_delete();
System.out.println("the deleted item is"+item);
break;
case 3:
a.q_display();

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

7. Complete the following:


i. Create a package named shape.
ii. Create some classes in the package representing some common
shapes like Square, Triangle, and Circle.
iii. Import and compile these classes in other program.

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

/** File Name: Square.java


directory where store: ..\shape
*/
package shape;
public class Square
{
double side;
public Square(double a)
{
side=a;
}
public double perimeter()
{
return (4*side);
}
public double area()
{
return (side*side);
}
}
/** File Name: Circle.java
directory where store: ..\shape
*/

package shape;
public class Circle
{
double radius;
public Circle(double r)
{
radius=r;
}
public double perimeter()

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
return 2*Math.PI*radius;
}
public double area()
{
return (Math.PI*radius*radius);
}
}

/** File Name: Square.java


directory where store: ..\shape
*/
package shape;
public class Triangle1
{
double base,height,A,B,C;
public Triangle1(double a,double b,double x,double y,double z)
{
base=a;
height=b;
A=x;
B=y;
C=z;
}
public double perimeter()
{
return (A+B+C);
}
public double area()
{
return (base*height)/2;
}
}

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

C:\MCAlab\java\shape>javac Square.java
C:\MCAlab\java\shape>javac Circle.java
C:\MCAlab\java\shape>javac Triangle1.java

/** File name : shapeTest.java


*/
import java.util.Scanner;
import java.io.*;
import shape.*;
public class shapeTest{
double s,bs,r,h,a,b,c;
void squareTest()
{
Scanner sc=new Scanner(System.in);
System.out.println("Area & Perimeter of Square");
System.out.print("Enter Side: ");
s=sc.nextDouble();
Square sq=new Square(s);
System.out.println("Area: "+sq.area()+"\t\t"+"Perimeter: "+sq.perimeter());
}
void triangleTest()
{
Scanner sc=new Scanner(System.in);
System.out.println("Area & Perimeter of Triangle");
System.out.print("Enter Base: ");
bs=sc.nextDouble();
System.out.print("Enter height: ");
h=sc.nextDouble();
System.out.println("Enter 3 side A , B & C: ");
System.out.print("A: ");
a=sc.nextDouble();
System.out.print("B: ");

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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:

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

st.triangleTest();
break;
case 3:
st.circleTest();
break;
case 4:
System.exit(0);
default: System.out.println("Invalid!");
break;
}
}

}
}

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

8. Write a JAVA Program to Create an enumeration Day of Week


with seven values SUNDAY through SATURDAY. Add a method is
Workday( ) to the DayofWeek class that returns true if the value on
which it is called is MONDAY through FRIDAY. For example, the
call DayOfWeek. SUNDAY.isWorkDay ( ) returns false.

public class DayofWork


{
public enum Days //Enumaration Data type
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
public static void main(String args[])
{
for(Days d : Days.values()) //For Each Days a type & d is
variable
{
Workday(d);
}
}
private static void Workday(Days d) //method
{
if((d.equals(Days.Sunday))||(d.equals(Days.Saturday)))
//Condition

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

System.out.println(d+" is workday: false");


else
System.out.println(d+" is workday: True");
}
}
Output:
C:\Users\Mangaldeep Sarkar>java DayofWork
Sunday is workday: false
Monday is workday: True
Tuesday is workday: True
Wednesday is workday: True
Thursday is workday: True
Friday is workday: True
Saturday is workday: false

9. Write a JAVA program which has


i. A Interface class for Stack Operations
ii. A Class that implements the Stack Interface and creates a fixed
length Stack.
iii. A Class that implements the Stack Interface and creates a
Dynamic length Stack.
iv. A Class that uses both the above Stacks through Interface
reference and does the Stack operations that demonstrates the
runtime binding.

Lab5.java
**********************************************************
********
import java.io.*;
class Lab5
{
public static void main(String s[])throws IOException
{

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

{
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--];

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

}
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;

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

}
}
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]+" ");
}
}

10. Write a JAVA Program which uses FileInputStream /


FileOutPutStream Classes.

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:");

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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");
}
}
}

11. Write JAVA programs which demonstrates utilities of Linked


List Class.
import java.util.*;
import java.io.*;
class Lab7
{
public static void main(String arg[])throws IOException
{
LinkedList list=new LinkedList();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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
{

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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); } } } }

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

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();
}
}

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology


Java Programming Lab(18MCA26) 2018-2019

*********************************
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.

Note 2: Change of program is not permitted in the practical examination.

Dr.G.N.K.Suresh Babu – Department of MCA – Acharya Institute of Technology

You might also like