0% found this document useful (0 votes)
4 views66 pages

Java Lab Manual

The document is a Java Programming Lab Manual for Malla Reddy College of Engineering for Women, detailing various lab exercises over five weeks. Each week includes specific programming tasks, such as creating a simple calculator, developing applets, handling exceptions, and implementing multithreading. Additionally, it provides example code, expected outputs, and viva questions related to the exercises.

Uploaded by

tanukushivani528
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)
4 views66 pages

Java Lab Manual

The document is a Java Programming Lab Manual for Malla Reddy College of Engineering for Women, detailing various lab exercises over five weeks. Each week includes specific programming tasks, such as creating a simple calculator, developing applets, handling exceptions, and implementing multithreading. Additionally, it provides example code, expected outputs, and viva questions related to the exercises.

Uploaded by

tanukushivani528
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

MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

JAVA PROGRAMMING LAB MANUAL

WEEK-1

1. Aim: Use Eclipse or Netbean platform and acquaint with the various menus.
Create a test Project, add a test class and run it. See how you can use auto
suggestions, autofill. Try code formatter and code refactoring like renaming
variables, methods and classes. Try debug step by step with a small program of
about 10 to 15 lines which contains at least one if else condition and a for loop.

Program:

import [Link].*;
public class Testproject {
public static void main(String[] args) {
[Link]("MALLA REDDY COLLEGE OF ENGINEERING FOR
WOMEN");
[Link]("--------Prime Number--------");
Scanner sc = new Scanner([Link]);
[Link]("Enter valid Number");
int n = [Link]();
int c = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}}
if (c == 2) {
[Link](n + "is Prime Number");
}
else
{
[Link](n + "is not Prime Number");
}
}
}

1
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

2
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-2

2. Aim: Write a java program that works as a simple calculator. Use a grid layout
to arrange buttons for the digits and for the +, -, *, % operations. Add a text
field to display the result. Handle any possible exceptions like divide by zero.

Program:

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

class A5 extends JFrame implements ActionListener {

public JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16;

public JTextField tf1;

public JPanel p;

public String v = "";

public String v1 = "0";

public String op = "";

public A5() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 400);

p = new JPanel(new FlowLayout());

tf1 = new JTextField(10);

[Link](tf1);

add(p);

setLayout(new GridLayout(0, 3));

3
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

b1 = new JButton("1");

[Link](this);

add(b1);

b2 = new JButton("2");

[Link](this);

add(b2);

b3 = new JButton("3");

[Link](this);

add(b3);

b4 = new JButton("4");

[Link](this);

add(b4);

b5 = new JButton("5");

[Link](this);

add(b5);

b6 = new JButton("6");

[Link](this);

add(b6);

b7 = new JButton("7");

[Link](this);

add(b7);

b8 = new JButton("8");

[Link](this);

add(b8);

b9 = new JButton("9");

4
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](this);

add(b9);

b10 = new JButton("0");

[Link](this);

add(b10);

b11 = new JButton("+");

[Link](this);

add(b11);

b12 = new JButton("-");

[Link](this);

add(b12);

b13 = new JButton("*");

[Link](this);

add(b13);

b14 = new JButton("/");

[Link](this);

add(b14);

b16 = new JButton("%");

[Link](this);

add(b16);

b15 = new JButton("=");

[Link](this);

add(b15);

setVisible(true);

5
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

public void actionPerformed(ActionEvent ae) {

String b = [Link]();

switch (b) {

case "1": {

v = v + "1";

[Link](v);

break;

case "2": {

v = v + "2";

[Link](v);

break;

case "3": {

v = v + "3";

[Link](v);

break;

case "4": {

v = v + "4";

[Link](v);

break;

case "5": {

v = v + "5";

6
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](v);

break;

case "6": {

v = v + "6";

[Link](v);

break;

case "7": {

v = v + "7";

[Link](v);

break;

case "8": {

v = v + "8";

[Link](v);

break;

case "9": {

v = v + "9";

[Link](v);

break;

case "0": {

v = v + "0";

7
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](v);

break;

case "+": {

op = "+";

v1 = [Link]();

v = "";

break;

case "-": {

op = "-";

v1 = [Link]();

v = "";

break;

case "*": {

op = "*";

v1 = [Link]();

v = "";

break;

case "/": {

op = "/";

v1 = [Link]();

v = "";

8
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

break;

case "%": {

op = "%";

v1 = [Link]();

v = "";

break;

case "=": {

switch (op) {

case "+": {

v = [Link]();

if ([Link]("")) {

v = "0";

long i = [Link](v1) + [Link](v);

[Link]([Link](i));

v="";

break;

case "-": {

v = [Link]();

if ([Link]("")) {

v = "0";

9
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

long i = [Link](v1) - [Link](v);

[Link]([Link](i));

v="";

break;

case "*": {

v = [Link]();

if ([Link]("")) {

v = "0";

long i = [Link](v1) * [Link](v);

[Link]([Link](i));

v="";

break;

case "/": {

try {

v = [Link]();

if ([Link]("")) {

v = "0";

long i = [Link](v1) / [Link](v);

[Link]([Link](i));

v="";

} catch (Exception ex) {

10
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](this, [Link]());

break;

case "%": {

try {

v = [Link]();

if ([Link]("")) {

v = "0";

long i = [Link](v1) % [Link](v);

[Link]([Link](i));

v="";

} catch (Exception ex) {

[Link](this, [Link]());

}}

break;

}}

break;

}}}

public class Calculator{

public static void main(String[] args) {

A5 a = new A5();

}}

11
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

12
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

13
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Viva Questions

1) What are Looping Statements?


2) What is Layout Manager?
3) What are the different types of layout manager?
4) How do you raise an exception when a number is divided by zero?
5) What are the basic operations of a calculator

14
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-3

3. a. Aim: Develop an applet in java that displays a simple message.

Program:

import [Link].*;

import [Link].*;

/*<applet code="[Link]" width=200 height=200>

</applet>*/

public class Apppletlifecycle extends Applet

public void init()

public void start()

public void stop()

public void destroy()

public void paint(Graphics g)

[Link]("welcome to applet program",50,50);

15
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

16
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

b. Aim: Develop an applet in java that receives an integer in one textfield, and
computes its factorial value and returns it in another text field, when the button
named “Compute” is clicked.

Program:

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

/*<applet code="[Link]" width=180 height=300></applet>*/

public class Factorial extends Applet implements ActionListener

Button compute;

TextField fact,num;

Label l1,l2;

public void init()

l1=new Label("NUMBER:",[Link]);

l2=new Label("FACTORIAL:",[Link]);

compute=new Button("compute");

num=new TextField(40);

fact=new TextField(10);

add(l1);

add(num);

add(l2);

add(fact);

add(compute);

17
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](this);

[Link](this);

[Link](this);

public void actionPerformed(ActionEvent ae)

String msg=[Link]();

if([Link]("compute"))

int f=1;

String str=[Link]();

int n=[Link](str);

for(int i=1;i<=n;i++)

f=f*i;

String str1=[Link](f);

[Link](str1);

repaint();

}}

public void paint(Graphics g)

}}

Output:

18
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Viva Questions

1) What is an applet?
2) What are the different types of applets available in java?
3) Why it is not possible to use remote applet than local applet?
4) What are the different ways of creating a button, label, text field?
5) What is the use of Graphics Class?

19
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-4

4. Aim: Write a java program that creates a user interface to perform integer
divisions. The user enters two numbers in the textfields, Num1 and Num2. The
division of Num1 and Num2 is displayed in the Result field when the Divide button
is clicked. If Num1 or Num2 were not an integer, the program would throw a
Number Format Exception. If Num2 were zero, the program would throw an
Arithmetic Exception. Display the exception in a message dialog box.

Program:

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

/*<applet code="[Link]" width=180 height=300></applet>*/

public class Division extends Applet implements ActionListener

Button div;

TextField result,Num1,Num2;

Label l1,l2,l3;

String msg1;

public void init()

l1=new Label("NUMBER1",[Link]);

l2=new Label("NUMBER2",[Link]);

l3=new Label("RESULT",[Link]);

div=new Button("DIVIDE");

result=new TextField(40);

Num1=new TextField(10);

20
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Num2=new TextField(10);

add(l1);

add(Num1);

add(l2);

add(Num2);

add(l3);

add(result);

add(div);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

public void actionPerformed(ActionEvent ae)

try

String msg=[Link]();

String str1=[Link]();

int n1=[Link](str1);

String str2=[Link]();

int n2=[Link](str2);

int f=n1/n2;

String str;

str=[Link](f);

21
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](str);

catch(ArithmeticException e)

[Link]("ARITHMETIC EXCEPTION");

catch(NumberFormatException e)

[Link]("NUMBER FORMAT EXCEPTION");

repaint();

public void paint(Graphics g)

}}

22
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1) What is an exception?
2) What is the super class of all exception classes?
3) When does an number format exception occurs?
4) What are the different exceptions available in java programming language?
5) What is the difference between Interface and a class?

23
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-5

5. Aim: Write a java program that implements a multithread application that has
three threads. First thread generates random integer every 1 second and if the
values is even, second thread computes the square of the number and prints. If the
value is odd, the third thread will print the value of cube of the number.

Program:

import [Link].*;
class even implements Runnable {
public int x;
public even(int x) {
this.x = x;
}
public void run() {
[Link]("Thread Name:Even Thread and " + x + "is even Number and Square
of " + x + " is: " + x * x);
}
}
class odd implements Runnable {
public int x;
public odd(int x) {
this.x = x;
}
public void run() {
[Link]("Thread Name:ODD Thread and " + x + " is odd number and Cube of
" + x + " is: " + x * x * x);
}
}
class A extends Thread {
public String tname;
public Random r;
public Thread t1, t2;
public A(String s) {
tname = s;
}
public void run() {
int num = 0;
r = new Random();
try {
24
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

for (int i = 0; i < 50; i++) {


num = [Link](100);
[Link]("Main Thread and Generated Number is " + num);
if (num % 2 == 0) {
t1 = new Thread(new even(num));
[Link]();
} else {
t2 = new Thread(new odd(num));
[Link]();
}
[Link](1000);
[Link]("--------------------------------------");
}
}
catch (Exception ex) {
[Link]([Link]());
}
}
}
public class Mthread {
public static void main(String[] args) {
A a = new A("One");
[Link]();
}
}

25
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1) What is a Thread ?
2) What are the different ways of using threads?
3) What is the difference between Runnable and Thread?
4) Explain the life cycle of a thread?
5) What is the difference between Stop and Wait?

26
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-6

6. Aim: Write a Java program for the following:


i) Create a doubly linked list of elements.
ii) Delete a given element from the above list.
iii) Display the contents of the list after deletion.

Program:

import [Link].*;

import [Link].*;

class Node{

public int data;

public Node next;

public Node prev;

class DLL{

public Node head;

public DLL(){

head=null;

public Node create(int val, Node New){

Node temp;

temp=head;

if(New==null)

[Link]("\n memory is not allocated");

[Link]=val;

if(head==null){

head=New;

27
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

temp=head;

else{

while([Link]!=null)

temp=[Link];

[Link]=New;

[Link]=temp;

temp=New;

return head;

public void display(Node head)throws NullPointerException{

Node temp;

temp=head;

if(temp==null){

[Link]("\n The list is empty \n");

return;

[Link]("The doubly linked list in forward direction is...");

while(temp!=null){

[Link](" "+[Link]);

temp=[Link];

[Link]("\n The doubly linked list in reverse direction is...");

temp=head;

28
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

while([Link]!=null)

temp=[Link];

while(temp!=null){

[Link](" "+[Link]);

temp=[Link];

Node search(Node head,int key){

Node temp;

int found=0;

temp=head;

if(temp==null){

[Link]("The linked list is empty");

return null;

while(temp!=null && found==0){

if([Link]!=key)

temp=[Link];

else

found=1;

if(found==1){

return temp;

else{

29
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

return null;

public Node dele(Node head,int key)throws NullPointerException{

Node temp,temp_prev;

temp=head;

if(temp==null){

[Link]("\n The list is empty. Can not delete");

return null;

temp=search(head,key);

if(temp!=null){

temp_prev=[Link];

if(temp_prev!=null){

if([Link]==null){

temp_prev.next=null;

temp=null;

else{

temp_prev.next=[Link];

([Link]).prev=temp_prev;

[Link]=null;

temp=null;

30
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

else{

if([Link]==null&&[Link]==null)

return null;

head=[Link];

[Link]=null;

temp=null;

[Link]("\t the element is deleted!!!");

return head;

class DLLDemo{

public static void main(String ar[])throws IOException,NullPointerException{

int choice,val,key;

char ans='y';

DLL obj=new DLL();

Scanner input=new Scanner([Link]);

do{

[Link]("\n Program to perform various operations on doubly linked list");

[Link]("[Link]");

[Link]("[Link]");

[Link]("[Link] an element from list");

[Link]("\n Enter your choice: ");

31
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

choice=[Link]().charAt(0);

switch(choice){

case '1': [Link]("enter the element: ");

val=[Link]();

Node New=new Node();

[Link]=null;

[Link]=null;

[Link]=[Link](val,New);

break;

case '2': [Link]([Link]);

break;

case '3': [Link]("\n Enter the element to be deleted from the linked list:
");

key=[Link]();

[Link]=[Link]([Link],key);

[Link]([Link]);

break;

[Link]("\n Do you want to go to main menu(y/n)?");

ans=[Link]().charAt(0);

}while(ans=='y');

32
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions:
1. Define Doubly linked list?
2. How to display the multiple cases?
3. What are the control statements?
4. What are the transfer statements available in the application?
5. Difference between singly linked list and doubly linked list?

33
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-7

7. Aim: Write a java program that simulates a traffic light. The program lets the
user select one of three lights: red, yellow, or green with radio buttons. On selecting
a button, an appropriate message with “Stop” or “Ready” or “Go” should appear
above the buttons in selected color. Initially, there is no message shown.

Program:

import [Link].*;

import [Link].*;

import [Link].*;

/*<applet code="[Link]" width=180 height=300></applet>*/

public class Traffic extends Applet implements ItemListener

Frame t=new Frame();

Checkbox red,orange,green;

CheckboxGroup cg;

public void start()

public void stop()

public void destroy()

public void paint(Graphics g)

34
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](10,10,100,180);

[Link](45,15,50,50);

[Link](45,67,50,50);

[Link](45,119,50,50);

if([Link]()==true)

[Link]([Link]);

[Link](45,15,50,50);

if([Link]()==true)

[Link]([Link]);

[Link](45,67,50,50);

if([Link]()==true)

[Link]([Link]);

[Link](45,119,50,50);

public void init()

cg=new CheckboxGroup();

red=new Checkbox("red",cg,false);

35
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

orange=new Checkbox("orange",cg,false);

green=new Checkbox("green",cg,false);

add(red);

add(orange);

add(green);

[Link](60,250,30,30);

[Link](90,250,30,30);

[Link](120,250,30,30);

[Link](this);

[Link](this);

[Link](this);

public void itemStateChanged(ItemEvent ie)

repaint();

public static void main(String args[])

Traffic t1=new Traffic();

36
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1) What is an applet ?
2) What is the difference between an application and an applet?
3) How do you run an applet application?
4) How do use threads in an applet?
5) What is an life cycle of an applet ?

37
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-8

8. Aim: Write a java program to create an abstract class named Shape that contains
two integers and an empty method named printArea().Provide three classes named
Rectangle, Triangle and Circle such that each one of the classes extends the class
Shape. Each one of the classes contains only the method printArea() that prints the
area of the given shape.

Program:

abstract class shape


{
public int x, y;
public abstract void printArea();
}
class Rectangle extends shape
{
public void printArea()
{
[Link]("Area of Rectangle is " + x * y);
}
}
class Triangle extends shape
{
public void printArea()
{
[Link]("Area of Triangle is " + (x * y) / 2);
}
}
class Circle extends shape
{
public void printArea()
{
[Link]("Area of Circle is " + (22 * x * x) / 7);
}
}
public class Abstex
{
/**
* @param args the command line arguments
*/
38
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

public static void main(String[] args)


{
// TODO code application logic here
Rectangle r = new Rectangle();
r.x = 10;
r.y = 20;
[Link]();
Triangle t = new Triangle();
t.x = 30;
t.y = 35;
[Link]();
Circle c = new Circle();
c.x = 2;
[Link]();
}
}

Output:

Area of Rectangle is 200

Area of Triangle is 525

Area of Circle is 12

Viva Questions

1) What is an abstract class?


2) What is the difference between abstract class and an class?
3) Can an abstract class have Static variable? If, yes justify
4) How do you use an already defined abstract class?
5) What is the difference between extends and implements?

39
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-9

9. Aim: Suppose that a table named [Link] is stored in a text file. The first line in
the file is the header, and the remaining lines correspond to rows in the table. The
elements are separated by commas. Write a java program to display the table using
Labels in Grid Layout.

Program:

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class Table1 extends JFrame

int i=0;

int j=0,k=0;

Object data[][]=new Object[5][4];

Object list[][]=new Object[5][4];

JButton save;

JTable table1;

FileInputStream fis;

DataInputStream dis;

public Table1()

String d= " ";

Container con=getContentPane();

[Link](new BorderLayout());

final String[] colHeads={"Name","Roll Number","Department","Percentage"};


40
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

try

String s=[Link]("Enter the File name present in the current


directory");

FileInputStream fis=new FileInputStream(s);

DataInputStream dis = new DataInputStream(fis);

while ((d=[Link]())!=null)

StringTokenizer st1=new StringTokenizer(d,",");

while ([Link]())

for (j=0;j<4;j++)

data[i][j]=[Link]();

[Link](data[i][j]);

i++;

[Link]("______________");

catch (Exception e)

[Link]("Eception raised" +[Link]());

table1=new JTable(data,colHeads);

41
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;

int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

JScrollPane scroll=new JScrollPane(table1,v,h);

[Link](scroll,[Link]);

public static void main(String args[])

Table1 t=new Table1();

[Link]([Link]);

[Link]("Display Data");

[Link](500,300);

[Link](true);

[Link](JFrame.EXIT_ON_CLOSE);

Output:

42
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Viva Questions

1) What is layout manager?


2) What are the different types of layout managers available in java?
3) What is the difference between Grid layout and Grid Bag Layout?
4) How do you display a table using grid layout in java?
5) What is the use of JFrame.EXIT_ON_CLOSE?

43
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-10

10. Aim: Write a java program that handles all mouse events and shows the event
name at the center of the window when a mouse is fired.

Program:

import [Link].*;
import [Link];
import [Link].*;
/*<applet code="Mouseevents"width=200 height=100>
</applet>*/
public class Mouseevents extends Applet implements
MouseListener,MouseMotionListener
{
String msg="";
int x=0,y=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
x=10;
y=20;
msg="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
x=10;
y=20;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
x=10;
y=20;
msg="mouse exited";
repaint();

44
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

}
public void mousePressed(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="*";
showStatus("dragging mouse at"+x+","+y);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("moving mouse at"+[Link]()+","+[Link]());
}
public void paint(Graphics g)
{
[Link](msg,x,y);
}
}

45
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1. What is an Event? When does an Event occur?


2. What are the different types of event available in java?
3. What are the difference between Mouse events and Key Events?
4. What are the different types of methods available in Mouse Listener Interface?
5. What is the difference between List and an Interface?

46
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-11

11. Aim: Write a java program that loads names and phone numbers from a text file
where the data is organized as one line per record and each field in a record are
separated by a tab(\t). It takes a name or phone number as input and prints the
corresponding other value from the hash table.

Program:

import [Link].*;

import [Link].*;

public class Hashtbl

public static void main(String[] args)

try

FileInputStream fs = new FileInputStream("D:\\[Link]");

Scanner sc = new Scanner(fs).useDelimiter("\\s+");

Hashtable<String, String> ht = new Hashtable<String, String>();

String[] arrayList;

String a;

[Link]("Welcome");

[Link]("HASH TABLE IS");

[Link]("--------------------------");

[Link]("KEY : VALUE");

while ([Link]()) {

a = [Link]();

arrayList = [Link]("\\s+");
47
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link](arrayList[0], arrayList[1]);

[Link](arrayList[0] + ":" + arrayList[1]);

[Link]("Welcome");

[Link]("----MENU------");

[Link]("----[Link] by Name------");

[Link]("----[Link] by Mobile------");

[Link]("----[Link]------");

String opt = "";

String name, mobile;

Scanner s = new Scanner([Link]);

while (opt != "3") {

[Link]("Enter Your Option 1,2,3");

opt = [Link]();

switch (opt) {

case "1": {

[Link]("Enter Name");

name = [Link]();

if ([Link](name)) {

[Link]("Mobile is " + [Link](name));

} else {

[Link]("Not Found");

break;

48
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

case "2": {

[Link]("Enter mobile");

mobile = [Link]();

if ([Link](mobile)) {

for ([Link] e : [Link]()) {

if ([Link]([Link]())) {

[Link]("Name is " + [Link]());

} else {

[Link]("Not Found");

break;

case "3": {

opt = "3";

[Link]("Menu Successfully Exited");

break;

default:

[Link]("Choose Option betwen 1 and Three");

break;

} catch (Exception ex)

49
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link]([Link]());

}}

Output:

50
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Viva Questions

1) What are collection classes available in java?


2) What is Hash Table?
3) Why do you need to go for Hash table than to use Arrays?
4) What is the difference between Contains Key and Contains Str ?
5) What is the difference between hasNext and has methods?

51
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-12

12. Aim: Write a Java program that correctly implements the producer – consumer
problem using the concept of interthread communication.

Program:

class Producer implements Runnable{

InterThread obj;

Producer(InterThread obj){

[Link]=obj;

new Thread(this,"Producer").start();

public void run(){

for(int i=0;i<10;i++){

[Link](i);

class Consumer implements Runnable{

InterThread obj;

Consumer(InterThread obj){

[Link]=obj;

new Thread(this, "Consumer").start();

public void run(){

for(int i=0;i<10;i++){

[Link]();

52
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

class InterThread{

int n;

boolean flag=false;

synchronized int get(){

if(!flag)

try{

wait();

catch(InterruptedException ie){

[Link]("Interrupted Exception");

[Link]("Consumer Consuming:"+n);

flag=false;

notify();

return n;

synchronized void put(int n){

if(flag)

try{

wait();

catch(InterruptedException ie){

53
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link]("InterruptedException");

this.n=n;

flag=true;

[Link]("Producer producing"+n);

notify();

public static void main(String ar[]){

InterThread t=new InterThread();

new Producer(t);

new Consumer(t);

54
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1) What is a Thread?
2) Define Synchronization?
3) Define InterThread Communication?
4) Thread Priorities.
5) Thread Model.

55
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-13

13. Aim: Write a Java program to list all the files in a directory including the files
present in all its subdirectories.

Program:

import [Link].*;

public class FileDisplay{

public static void main(String ar[]){

File DirName=new File(ar[0]);

displayContents(DirName);

public static void displayContents(File dir){

try{

File[] list=[Link]();

for(File file:list){

if([Link]()){

[Link]("directory:"+[Link]());

displayContents(file);

else{

[Link](" file:"+[Link]());

catch(IOException ie){

[Link]();
56
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions

1) What is the use of File Input Stream?


2) What is the use of File?
3) What is the need for Directory?
4) What is the use of try catch block?
5) What is the difference between file and directory?

57
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-14

14. Aim: Write a Java program that implements Quick sort algorithm for sorting
a list of names in ascending order.

Program:

import [Link].*;

import [Link].*;

class QuickSort{

public int A[];

public QuickSort(int MAX){

A=new int[MAX];

public void quick(int low,int high){

int m,i;

if(low<high){

m=partition(low,high);

quick(low,m-1);

quick(m+1,high);

public int partition(int low,int high){

int pivot=A[low],i=low,j=high;

while(i<=j){

while(A[i]<=pivot)

i++;

while(A[j]>pivot)

j--;
58
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

if(i<j)

swap(i,j);

swap(low,j);

return j;

public void swap(int i,int j){

int temp;

temp=A[i];

A[i]=A[j];

A[j]=temp;

public void display(int n){

[Link]("\n The sorted list is....\n");

for(int i=0;i<n;i++)

[Link](" "+A[i]);

class QuickSortDemo{

public static void main(String ar[])throws IOException{

int n;

[Link]("\n \t Program for Quick Sort:");

[Link]("\n Enter the [Link] elements:");

Scanner sc=new Scanner([Link]);

n=[Link]();

59
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

QuickSort obj=new QuickSort(n);

[Link]("\n Enter the elements");

for(int i=0;i<n;i++)

obj.A[i]=[Link]();

[Link](0,n-1);

[Link](n);

Output:

Viva Questions

1) What is meant by QuickSort?


2) What is meant by Sorting?
3) What is meant by pivot element?
4) What are the different types of sortings?
5) How does quick sort works?

60
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WEEK-15

15. Aim: Write a Java program that implements Bubble sort algorithm for sorting in
descending order and also shows the number of interchanges occurred for
the given set of integers.

Program:

import [Link].*;

import [Link].*;

class Bubble{

public int n;

public int a[];

public Bubble(int MAX){

a=new int[MAX];

public void bubbleSort(int n){

int temp;

for(int i=0;i<=n-2;i++){

for(int j=0;j<=n-2-i;j++){

if(a[j]<a[j+1]){

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

public void display(int n){

61
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

[Link]("\n The Sorted list is...\n");

for(int i=0;i<n;i++)

[Link](" "+a[i]);

class BubbleDemo{

public static void main(String ar[])throws


IOException,ArrayIndexOutOfBoundsException{

int n;

[Link]("\n\t Program for bubble sort");

[Link]("\n Enter the [Link] elements:");

Scanner sc=new Scanner([Link]);

n=[Link]();

Bubble obj=new Bubble(n);

[Link]("\n Enter the Elements:");

for(int i=0;i<n;i++)

obj.a[i]=[Link]();

[Link](n);

[Link](n);

62
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

Output:

Viva Questions:

1. What is meant by Sorting?

2. What is meant by Bubble Sort?

3. What is the difference between Quick sort and bubble sort?

4. Other names of the bubble sort?

5. What are the control statements?

63
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

ADDITIONAL EXPERIMENTS

64
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

The Fibonacci sequence is defined by the following rule. The first 2 values in the
sequence are 1, 1. Every subsequent value is the sum of the 2 values preceding it. Write a
Java program that uses both recursive and non-recursive functions to print the nth value of
the Fibonacci sequence.

Program:-

import [Link];
class Fib {
public static void main(String args[ ]) {
Scanner input=new Scanner([Link]);
int i,a=1,b=1,c=0,t;
[Link]("Enter value of t:");
t=[Link]();
[Link](a);
[Link](" "+b);
for(i=0;i<t-2;i++) {
c=a+b;
a=b;
b=c;
[Link](" "+c);
}
[Link]();
[Link](t+"th value of the series is: "+c);
}
}

Output:-

65
MALLA REDDY COLLEGE OF ENGINEERING FOR WOMEN R-18 JAVA PROGRAMMING LAB MANUAL

WAJP that prompts the user for an integer and then prints out all the prime numbers up
to that Integer.

Program:-
import [Link].*
class Test {
void check(int num) {
[Link] ("Prime numbers up to "+num+" are:");
for (int i=1;i<=num;i++)
for (int j=2;j<i;j++) {
if(i%j==0)
break;
else if((i%j!=0)&&(j==i-1))
[Link](“ “+i);
}
}
} //end of class Test
class Prime {
public static void main(String args[ ]) {
Test obj1=new Test();
Scanner input=new Scanner([Link]);
[Link]("Enter the value of n:");
int n=[Link]();
[Link](n);
}
}

Output :-

66

You might also like