Java Programming Lab Manual R18 JNTUH 2
Java Programming Lab Manual R18 JNTUH 2
Java Programming
PROGRAMS
1. Use Eclipse or Net bean 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, auto fill. 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 containsat least one if else condition and a for loop.
Solution:
Click on Accept
Click on Select All and Accept Selected.
Source Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
* <applet code="Calculator" width=500 height=500></applet>
* */
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("+"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("-"))
{
v1=Integer.parseInt(t1.getText()); OP='-';
t1.setText("");
}
else if(str.equals("*"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("/"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("%")){
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("=")){
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("Clear"))
{
t1.setText("");
}
}
}
Output:
3.
a) Develop an applet in Java that displays a simple message.
b) Develop an applet in Java that receives an integer in one text field, and computes its factorial Value and
returns it in another text field, when the button named “Compute” is clicked.
// Import the packages to access the classes and methods in awt and applet classes.
import java.awt.*;
import java.applet.*;
Output:
Source code for question b:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
Output:
4.Write a Java program that creates a user interface to perform integer divisions. The user
enters two numbers in the text fields, Num1 and Num2. The division of Num1 and Num 2 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.
Source code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
res.setText(String.valueOf(num3));
msg = "Operation Succesfull!!!";
repaint();
} catch (NumberFormatException ex) {
System.out.println(ex);
res.setText("");
msg = "NumberFormatException - Non-numeric";
repaint();
} catch (ArithmeticException e) {
System.out.println("Can't be divided by Zero" + e);
res.setText("");
msg = "Can't be divided by Zero";
repaint();
}
}
}
}
Source code:
import java.util.Random;
SquareThread(int randomNumbern) {
number = randomNumbern;
}
CubeThread(int randomNumber) {
number = randomNumber;
}
Source code:
class Node {
int data;
Node previous;
Node next;
if (head == null) {
head = tail = newNode;
head.previous = null;
tail.next = null;
} else {
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
public void display() {
Node current = head;
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Nodes of doubly linked list: ");
while (current != null) {
dList.display();
}
}
Output:
7.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.
Source code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
* <applet code = "TrafficLightsExample" width = 1000 height = 500>
* </applet>
* */
redLight.addItemListener(this);
yellowLight.addItemListener(this);
greenLight.addItemListener(this);
add(redLight);
add(yellowLight);
add(greenLight);
add(msg);
msg.setFont(new Font("Serif", Font.BOLD, 20));
}
public void itemStateChanged(ItemEvent ie) {
redLight.setForeground(Color.BLACK);
yellowLight.setForeground(Color.BLACK);
greenLight.setForeground(Color.BLACK);
if(redLight.getState() == true) {
redLight.setForeground(Color.RED);
msg.setForeground(Color.RED);
msg.setText("STOP");
}
else if(yellowLight.getState() == true) {
yellowLight.setForeground(Color.YELLOW);
msg.setForeground(Color.YELLOW);
msg.setText("READY");
}
else{
greenLight.setForeground(Color.GREEN);
msg.setForeground(Color.GREEN);
msg.setText("GO");
}
}
}
Output:
8.Write a Java program to create an abstract class named Shape that contains two integers and an empty method
named print Area (). 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 print Area () that prints the
area of the given shape.
Source code:
import java.util.*;
Source code:
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
Source code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Output:
11.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(hint: use hash tables)
Source code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
Output:
12.Write a Java program that correctly implements the producer – consumer problem using
the concept of interthread communication.
Source Code:
class ItemQueue {
int item;
boolean valueSet = false;
{
while (!valueSet)
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("Consummed:" + item);
valueSet = false;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
notify();
return item;
}
ItemQueue itemQueue;
Consumer(ItemQueue itemQueue){
this.itemQueue = itemQueue;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
itemQueue.getItem();
}
}
}
class ProducerConsumer{
public static void main(String args[]) {
ItemQueue itemQueue = new ItemQueue();
new Producer(itemQueue);
new Consumer(itemQueue);
}
}
Output:
13.Write a Java program to list all the files in a directory including the files present in all its
subdirectories.
Source Code:
import java.util.Scanner;
import java.io.*;
Source Code:
String names[];
int length;
while (i <= j) {
while (this.names[i].compareToIgnoreCase(pivot) < 0) {
i++;
}
if (i <= j) {
exchangeNames(i, j);
i++;
j--;
}
}
if (lowerIndex < j) {
quickSort(lowerIndex, j);
}
if (i < higherIndex) {
quickSort(i, higherIndex);
}
}
Output:
15.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.
Source Code:
import java.util.Scanner;
public class BubbleSort {