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
contains at least one if else condition and a for loop.
Program
Step1:
Open eclipse IDE
Step2:
Click on Launch then eclipse workspace will be opened
Step3:
Go to FileNewJava Project
Project Name: FirstApp
Click on Finish then Eclipse window will be opened
Go to Project ExplorerFirstAppRight Click on srcNewClick on class
Name: DemoCheck the checkbox public static void main(String[] args)
Click On Finish then [Link] file will be created in that file write the
Following code
import [Link];
public class Demo
{
public static void main(String[] args)
{
int n,i,r,k,s=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter n value");
n=[Link]();
k=n;
for(;n!=0;)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==k)
[Link](k+" is palindrom");
else
[Link](k+" is not palindrom");
}
Output
Enter n value
121
121 is palindrom
2. 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 divided by zero.
Program
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Cal extends Applet implements ActionListener
{
GridLayout g=new GridLayout(4,5);
TextField t=new TextField(20);
Button b0=new Button("0");
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b10=new Button("+");
Button b11=new Button("-");
Button b12=new Button("*");
Button b13=new Button("/");
Button b14=new Button("%");
Button b15=new Button("=");
Button b16=new Button("Clear");
int p,q=0;
String str1="",oper;
public void init()
{
setLayout(g);
setBackground([Link]);
setForeground([Link]);
add(t);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
String str=[Link]();
if([Link]("+"))
{
p=[Link]([Link]());
oper=str;
[Link](str1="");
}
else if([Link]("-"))
{
p=[Link]([Link]());
oper=str;
[Link](str1="");
}
else if([Link]("*"))
{
p=[Link]([Link]());
oper=str;
[Link](str1="");
}
else if([Link]("/"))
{
p=[Link]([Link]());
oper=str;
[Link](str1="");
}
else if([Link]("%"))
{
p=[Link]([Link]());
oper=str;
[Link](str1="");
}
else if([Link]("="))
{
str1="";
if([Link]("+"))
{
q=[Link]([Link]());
[Link]([Link](p+q));
}
else if([Link]("-"))
{
q=[Link]([Link]());
[Link]([Link](p-q));
}
else if([Link]("*"))
{
q=[Link]([Link]());
[Link]([Link](p*q));
}
else if([Link]("/"))
{
q=[Link]([Link]());
[Link]([Link](p/q));
}
else if([Link]("%"))
{
q=[Link]([Link]());
[Link]([Link](p%q));
}
}
else if([Link]("clear"))
[Link]("");
else
{
[Link]([Link](str));
str1=[Link]();
}
}
catch (Exception e)
{
[Link](null,e);
}
}
}
/*<applet code="[Link]" height="150" width="700"> </applet>*/
Output
3. a) Develop an applet in Java that displays a simple message.
Program
import [Link].*;
import [Link].*;
public class msg extends Applet
{
public void paint(Graphics g)
{
setBackground([Link]);
setForeground([Link]);
[Link]("I LOVE JAVA",10,50);
}
}
/*<applet code="[Link]" height="500" width="500">
</applet>*/
Output
3b) 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.
Program
import [Link].*;
import [Link].*;
import [Link].*;
public class fact extends Applet implements ActionListener
{
Label l1=new Label("Enter a number");
Label l2=new Label("Factorial");
TextField t1=new TextField(20);
TextField t2=new TextField(20);
Button b=new Button("Compute");
public void init()
{
add(l1);
add(t1);
add(l2);
add(t2);
add(b);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
int f=1;
String s1=[Link]();
int n=[Link](s1);
for(int i=1;i<=n;i++)
f=f*i;
String s2=[Link](f);
[Link](s2);
}
}
/*<applet code="[Link]" height="500" width="500">
</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.
Program
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class div extends Applet implements ActionListener
{
Label l1=new Label("NUM1");
Label l2=new Label("NUM2");
Label l3=new Label("RESULT");
TextField t1=new TextField(20);
TextField t2=new TextField(20);
TextField t3=new TextField(20);
Button b=new Button("Div");
public void init()
{
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
[Link](this);
}
public void actionPerformed(ActionEvent ae)
{
int a=0,b=0;
double c=0.0;
String s1=[Link]();
String s2=[Link]();
try
{
a=[Link](s1);
b=[Link](s2);
c=a/b;
String s3=[Link](c);
[Link](s3);
}
catch(ArithmeticException ae1)
{
[Link](null,"ArthimeticException");
}
catch(NumberFormatException nfe)
{
[Link](null,"NumberFormatException");
}
}
/*<applet code="[Link]" height="500" width="500">
</applet>*/
5. Write a Java program that implements a multi-thread application that has three
threads. First thread generates random integer every 1 second and if the value 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
{
int x;
even(int x)
{
this.x=x;
}
public void run()
{
[Link]("From Thread Even:"+x+"\nSquare of "+x+"
is:"+(x*x));
}
}
class odd implements Runnable
{
int x;
odd(int x)
{
this.x=x;
}
public void run()
{
[Link]("From Thread Odd:"+x+"\nCube of "+x+"
is:"+(x*x*x));
}
}
class A extends Thread
{
public void run()
{
int n;
Random r=new Random();
for(int i=1;i<=50;i++)
{
n=[Link](100);
if(n%2==0)
{
even e=new even(n);
Thread t1=new Thread(e);
[Link]();
try
{
[Link](2000);
}
catch(InterruptedException ie)
{
[Link]();
}
}
else
{
odd o=new odd(n);
Thread t2=new Thread(o);
[Link]();
try
{
[Link](2000);
}
catch(InterruptedException ie)
{
[Link]();
}
}
}
}
}
class demo
{
public static void main(String args[])
{
A obj=new A();
[Link]();
}
}
Output
6. Write a Java program for the following: Create a doubly linked list of elements.
Delete a given element from the above list. Display the contents of the list after
deletion.
Program
class node
{
node prev;
int data;
node next;
}
class cal
{
node First;
cal()
{
First=null;
}
public void insert(int n)
{
node t=new node();
[Link]=n;
[Link]=null;
[Link]=null;
if(First==null)
First=t;
else
{
[Link]=First;
[Link]=t;
First=t;
}
}
public void delete(int n)
{
node p;
if([Link]==n)
{
[Link]("deleted:"+[Link]);
First=[Link];
}
else
{
p=[Link];
while(p!=null)
{
if([Link]==n)
{
[Link]("deleted:"+[Link]);
[Link]=[Link];
[Link]=[Link];
}
}
}
}
public void show()
{
[Link]("Data Form Dll:");
node p=First;
while(p!=null)
{
[Link]([Link]+"<=>");
p=[Link];
}
[Link]();
}
}
class Dll
{
public static void main(String[] args)
{
cal c=new cal();
[Link](10);
[Link](20);
[Link](30);
[Link]();
[Link](30);
[Link]();
}
}
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.
Program
import [Link].*;
import [Link].*;
import [Link].*;
class TrafficLight extends JFrame implements ActionListener
{
String msg=" " ;
private JLabel label;
private JTextField display;
private JRadioButton r1,r2,r3;
private ButtonGroup bg;
private Container c;
public TrafficLight()
{
setLayout(new FlowLayout());
c=getContentPane();
label=new JLabel(" Traffic Light");
display =new JTextField(10);
r1=new JRadioButton("RED");
r2=new JRadioButton("GREEN");
r3=new JRadioButton("YELLOW");
bg=new ButtonGroup();
[Link](label);
[Link](r1);
[Link](r2);
[Link](r3);
[Link](display);
[Link](r1);
[Link](r2);
[Link](r3);
[Link](this);
[Link](this);
[Link](this);
setSize(400,400);
setVisible(true);
[Link]([Link]);
}
public void actionPerformed(ActionEvent ie)
{
msg=[Link]();
if ([Link]("RED"))
{
[Link]([Link]);
[Link](msg+ " :TURN ON");
}
else if ([Link]("GREEN"))
{
[Link]([Link]);
[Link](msg+ " :TURN ON");
}
else if ([Link]("YELLOW"))
{
[Link]([Link]);
[Link](msg+ " :TURN ON");
}
}
public static void main(String args[])
{
TrafficLight light=new TrafficLight();
[Link](JFrame.EXIT_ON_CLOSE);
}
}
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.
Program
abstract class shape
{
int x,y;
public abstract void printArea();
}
class rectangle extends shape
{
public void printArea()
{
double a;
a=x*y;
[Link]("Area of Rectangle:"+a);
}
}
class triangle extends shape
{
public void printArea()
{
double a;
a=0.5*x*y;
[Link]("Area of triangle:"+a);
}
}
class circle extends shape
{
public void printArea()
{
double a;
a=([Link])*x*x;
[Link]("Area of Circle:"+a);
}
}
class area
{
public static void main(String args[])
{
rectangle r=new rectangle();
r.x=10;
r.y=20;
[Link]();
triangle t=new triangle();
t.x=1;
t.y=2;
[Link]();
circle c=new circle();
c.x=5;
[Link]();
}
}
Output
9. 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].*;
import [Link].*;
class A extends JFrame
{
public A()
{
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout g = new GridLayout(0, 3);
setLayout(g);
try {
FileInputStream fin = new FileInputStream("D:\\PRIW\\PRIW-2020\\cse-2-1Lab-
latest\\9\\[Link]");
Scanner sc = new Scanner(fin).useDelimiter(",");
String[] arrayList;
String a;
while ([Link]()) {
a = [Link]();
arrayList = [Link](",");
for (String i : arrayList) {
add(new JLabel(i));
}
}
}
catch (Exception ex)
{
[Link]();
}
setDefaultLookAndFeelDecorated(true);
pack();
setVisible(true);
}
}
public class Tbl
{
public static void main(String args[])
{
A a = new A();
}
}
Output
10. Write a Java program that handles all mouse events and shows the event name
at the center of the window when a mouse event is fired (Use Adapter classes).
Program
import [Link].*;
import [Link].*;
import [Link].*;
public class mouse 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=100;
y=353;
msg="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
x=150;
y=250;
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
x=150;
y=250;
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=150;
y=250;
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="Mouse Up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="*";
showStatus("dragging mouse at "+x+","+y);
repaint();
}
public void mouseMoved(MouseEvent me)
{
x=[Link]();
y=[Link]();
msg="Hello";
showStatus("Moving mouse at "+x+","+y);
repaint();
}
public void paint(Graphics g)
{
[Link](msg,x,y);
}
}
/*<applet code="[Link]" height="500" width="500">
</applet>*/
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).
Program
import [Link].*;
import [Link].*;
public class Hashtbl
{
public static void main(String[] args)
{
try
{
FileInputStream fs = new FileInputStream("F:\\[Link]");
Scanner sc = new Scanner(fs).useDelimiter("\\s+");
Hashtable<String, String> ht = new Hashtable<String, String>();
String[] arrayList;
String a;
[Link]("Welcome TO MEDHA Eng College");
[Link]("HASH TABLE IS");
[Link]("--------------------------");
[Link]("KEY : VALUE");
while ([Link]())
{
a = [Link]();
arrayList = [Link]("\\s+");
[Link](arrayList[0], arrayList[1]);
[Link](arrayList[0] + ":" + arrayList[1]);
}
[Link]("Welcome TO MEDHA Eng College");
[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]();
if([Link]("1"))
{
[Link]("Enter Name");
name = [Link]();
if ([Link](name)) {
[Link]("Mobile is " + [Link](name));
}
else
{
[Link]("Not Found");
}
}
else if([Link]("2")) {
[Link]("Enter mobile");
mobile = [Link]();
if ([Link](mobile)) {
for ([Link] e : [Link]()) {
if ([Link]([Link]())) {
[Link]("Name is " + [Link]());
}
}
} else {
[Link]("Not Found");
}
}
else if([Link]("3")) {
opt = "3";
[Link]("Menu Successfully Exited");
}
else
[Link]("Choose Option betwen 1 and Three");
}
}
catch (Exception ex)
{
[Link]();
}
}
}
[Link]
Ram 7324567
raj 83457873787
ravi 92568365826
Output
12. Write a Java program that correctly implements the producer – consumer
problem using the concept of interthread communication.
Program
class Thread1
{
int num;
boolean vs=false;
synchronized int get()
{
if (!vs)
try
{
wait();
}
catch (Exception e)
{
[Link]("Excepton occurs at : "+e);
}
[Link]("get" +num);
try
{
[Link](1000);
}
catch (Exception e)
{
[Link]("Excepton occurs at : "+e);
}
vs=false;
notify();
return num;
}
synchronized int put(int num)
{
if (vs)
try
{
wait();
}
catch (Exception e)
{
[Link]("Excepton occur at : "+e);
}
[Link]=num;
vs=true;
[Link]("put"+num);
try
{
[Link](1000);
}
catch (Exception e)
{
[Link]("Excepton occur at : "+e);
}
notify();
return num;
}
}
class Producer implements Runnable
{
Thread1 t;
Producer(Thread1 t)
{
this.t=t;
new Thread(this,"Producer").start();
}
public void run()
{
int x=0;
int i = 0;
while (x<10)
{
[Link](i++);
x++;
}
}
}
class Consumer implements Runnable
{
Thread1 t;
Consumer(Thread1 t)
{
this.t=t;
new Thread(this,"Consumer").start();
}
public void run()
{
int x=0;
while (x<10)
{
[Link]();
x++;
}
}
}
class ProducerConsumer
{
public static void main(String[] args)
{
Thread1 t=new Thread1();
new Producer(t);
new Consumer(t);
}
}
Output
13. Write a Java program to list all the files in a directory including the files
present in all its subdirectories.
Program
import [Link];
import [Link];
public class Display {
public static void main(String[] args) {
File currentDir = new File("."); // current directory
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
try {
File[] files = [Link]();
for (File file : files) {
if ([Link]()) {
[Link]("directory:" +
[Link]());
displayDirectoryContents(file);
} else {
[Link](" file:" +
[Link]());
}
}
} catch (IOException e) {
[Link]();
}
}
}
Output
14. Write a Java program that implements Quick sort algorithm for sorting a list of
names in ascending order
Program
import [Link].*;
class Qsort
{
public static void main(String[] args)
{
String s[];
int i,n;
Scanner sc=new Scanner([Link]);
[Link]("Enter size of array");
n=[Link]();
s=new String[n];
[Link]("Enter "+n+" names into array");
for(i=0;i<n;i++)
s[i]=[Link]();
[Link](s);
for(i=0;i<n;i++)
[Link](s[i]);
}
}
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.
Program
import [Link].*;
class Bubble
{
public static void main(String args[])
{
int a[],n,i,j,temp,c=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter size of the array");
n=[Link]();
a=new int[n];
[Link]("enter "+n+" elements into array");
for(i=0;i<n;i++)
a[i]=[Link]();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
c++;
}
}
}
[Link]("Sorted Array:");
for(i=0;i<n;i++)
[Link](a[i]);
[Link]("Number of interchanges:"+c);
}
}
Output