Java Pgms
Java Pgms
a) Write a Java program that shows the usage of try, catch and finally for
ArithmeticException and ArrayIndexOutOfBoundsException.
b) Write a Java program that shows the usage of throw and throws. Check whether a person
is eligible for voting based on his age and throw an exception if age is less than 18.
import java.util.*;
System.out.println("Result: "+(a/b));
OUTPUT:
import java.util.*;
class Age
{ void read(int a) throws ArithmeticException
{ try
{ if(a>=18)
System.out.println("Eligible to vote");
else
throw new ArithmeticException();
}
catch(Exception e)
{
System.out.println("Candidate is not eligible to vote");
}
}
}
OUTPUT
Write a Java program that read from a file and write to file by handling all file related
exceptions.
OUTPUT-
Content in abc.txt:
Hello world
Content in def.txt:
Hello World
22-01-2022
Familiarization of String Tokenizer Class
Write a Java program that reads a line of integers, and then displays each integer, and the
sum of all the integers (Use String Tokenizer class of java.util).
PROGRAM
import java.util.*;
while(st.hasMoreTokens()){
n= Integer.parseInt(st.nextToken());
System.out.print(n + " ");
sum=sum+n;
}
System.out.print("\nSum: "+ sum);
}
}
OUTPUT
Enter the numbers seperated by comas10,20,30,40,50
Numbers are: 10 20 30 40 50
Sum: 150
05-02-2022
Familiarization of thread synchronization
Write a class with two methods to print even numbers and odd numbers respectively. Share
the object of the above class among 2 T1 and T2. Use T1to invoke method to print odd
numbers and T2 to invoke method to print even numbers
PROGRAM
class OddEven{
synchronized public void odd()
{
for(int i=1;i<=10;i=i+2) {
if(i%2!=0){
System.out.println("Odd: " + i);
}
}
}
synchronized public void even()
{
for(int i=0;i<=10;i=i+2) {
if(i%2==0){
System.out.println("Even: " + i);
}
}
}
t1 o = new t1(oe);
t2 e = new t2(oe);
o.start();
e.start();
}
}
OUTPUT
Even: 0
Even: 2
Even: 4
Even: 6
Even: 8
Even: 10
Odd: 1
Odd: 3
Odd: 5
Odd: 7
Odd: 9
11-02-2022
Implementation of a simple calculator using Swing package
Write a Java program that works as a simple calculator. Arrange Buttons for digits and the +
- * % operations properly. Add a text field to display the result. Handle any possible
exceptions like divide by zero. Use Java Swing
PROGRAM
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class Calc extends JFrame implements ActionListener {
static JFrame f;
static JTextField l;
String s0, s1, s2;
Calc()
{
s0 = s1 = s2 = "";
}
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be,
beq, beq1;
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");
be = new JButton(".");
bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);
p.add(l);
p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bd);
p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);
f.add(p);
f.setSize(200, 220);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == 'C') {
s0 = s1 = s2 = "";
l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {
double te;
if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));
l.setText(s0 + s1 + s2 + "=" + te);
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
if (s1.equals("") || s2.equals(""))
s1 = s;
else {
double te;
if (s1.equals("+"))
te = (Double.parseDouble(s0) +
Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) -
Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) /
Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) *
Double.parseDouble(s2));
s0 = Double.toString(te);
s1 = s;
s2 = "";
}
l.setText(s0 + s1 + s2);
}
}
}
OUTPUT-
11-02-2022
Simulation of a traffic light using Swing package
Write a Java program that simulates a traffic light. The program lets the user select one of
three lights: red, yellow, or green. When a radio button is selected, the light is turned on,
and only one light can be on at a time. No light is on when the program starts.
PROGRAM-
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public Trafficlight(){
setBounds(0,0,500,300);
red_c= getBackground();
yellow_c= getBackground();
green_c= getBackground();
add(r1);
add(r2);
add(r3);
r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
}
OUTPUT-
19-02-2022
Implementation of doubly linked list
PROGRAM-
package doubly;
import java.util.*;
class node {
int data;
node next;
node prev;
}
class Doubly {
int j;
node temp;
p = temp.next;
temp.next = p.next;
}}
System.out.print("\nlinked list after deletion:\n");
display(n1);
}
}
}
//node1
node n1 = new node();
n1.data=10;
n1.prev=null;
//node2
node n2= new node();
n2.data=20;
n2.prev=n1;
n1.next=n2;
//node3
node n3= new node();
n3.data=30;
n3.prev=n2;
n2.next=n3;
n3.next=null;
int pos;
System.out.print("\nEnter position to delete from:\n");
pos=sc.nextInt();
d.delete(n1, pos);
}
}
OUTPUT-