0% found this document useful (0 votes)
32 views15 pages

Java Pgms

The document describes a Java program that implements a simple calculator using Swing. It includes: - Buttons for digits (0-9) and operators (+ - * /) - A text field to display calculations - Methods to handle button clicks and perform calculations - Exception handling for errors like divide by zero When the user clicks buttons, numbers and operators are added to a calculation string. The "=" button evaluates the calculation and displays the result.

Uploaded by

Atul Draws
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)
32 views15 pages

Java Pgms

The document describes a Java program that implements a simple calculator using Swing. It includes: - Buttons for digits (0-9) and operators (+ - * /) - A text field to display calculations - Methods to handle button clicks and perform calculations - Exception handling for errors like divide by zero When the user clicks buttons, numbers and operators are added to a calculation string. The "=" button evaluates the calculation and displays the result.

Uploaded by

Atul Draws
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
You are on page 1/ 15

17-12-2022

Familiarization of Exception Handling

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

public class exception {


public static void main(String [] args) {
try {
Scanner sc=new Scanner(System.in);
int a,b;
int ar[] = {20,20,40};
System.out.println("Enter first number: ");
a=sc.nextInt();
System.out.println("Enter second number: ");
b=sc.nextInt();

System.out.println("Result: "+(a/b));

for(int i = 5; i >= 0; i--)


{ System.out.println("The value of array is" +ar[i]);
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array is out of Bounds");
}
catch(ArithmeticException e)
{
System.out.println("Cannot divide by zero");
}
finally
{
System.out.println("End of program");
}
}
}

OUTPUT:

Enter first number:


20
Enter second number:
10
Result: 2
Array is out of Bounds
End of program

Enter first number:


20
Enter second number:
0
Cannot divide by zero
End of program

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

public class exception


{
public static void main(String[]args)
{
int age;
String name;
Age ob=new Age();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name: ");
name=sc.nextLine();
System.out.println("Enter age: ");
age=sc.nextInt();
ob.read(age);
}
}

OUTPUT

Enter the name:


atul
Enter age:
19
Eligible to vote

Enter the name:


abc
Enter age:
15
Candidate is not eligible to vote
07-01-2022
Working with Files

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

public class tokeniser {


public static void main (String args[]) {
String str;
Scanner sc =new Scanner(System.in);
int n, sum=0;

System.out.print("Enter the numbers seperated by comas");


str=sc.nextLine();
StringTokenizer st = new StringTokenizer(str,",");

System.out.print("Numbers are: ");

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

class t1 extends Thread{


OddEven o1;
t1(OddEven o){
o1=o;
}
public void run(){
o1.odd();
}
}

class t2 extends Thread{


OddEven e1;
t2(OddEven e){
e1=e;
}
public void run(){
e1.even();
}
}

public class tsync{


public static void main(String[] args){
OddEven oe = new OddEven();

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

public static void main(String args[])


{
f = new JFrame("calculator");

Calc c = new Calc();


l = new JTextField(16);
l.setEditable(false);

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

beq1 = new JButton("=");

ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");

be = new JButton(".");

JPanel p = new JPanel();

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

if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) ==


'.') {
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

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

class Trafficlight extends JPanel implements ActionListener{


private Color red_c;
private Color yellow_c;
private Color green_c;
JRadioButton r1= new JRadioButton("Red");
JRadioButton r2= new JRadioButton("Yellow");
JRadioButton r3= new JRadioButton("Green");

public Trafficlight(){

setBounds(0,0,500,300);

red_c= getBackground();
yellow_c= getBackground();
green_c= getBackground();

ButtonGroup g = new ButtonGroup();


g.add(r1);
g.add(r2);
g.add(r3);

add(r1);
add(r2);
add(r3);

r1.addActionListener(this);
r2.addActionListener(this);
r3.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


if (r1.isSelected() ==true) {
red_c=Color.red;
yellow_c= getBackground();
green_c= getBackground();
}
else if (r2.isSelected() ==true) {
yellow_c=Color.yellow;
red_c= getBackground();
green_c= getBackground();
}
else if (r3.isSelected() ==true) {
green_c=Color.green;
yellow_c= getBackground();
red_c= getBackground();
}
repaint();
}

public void paintComponent(Graphics g) {


g.drawOval(230, 50, 50, 50);
g.drawOval(230, 110, 50, 50);
g.drawOval(230, 170, 50, 50);
g.setColor(red_c);
g.fillOval(230, 50, 50, 50);
g.setColor(yellow_c);
g.fillOval(230, 110, 50, 50);
g.setColor(green_c);
g.fillOval(230, 170, 50, 50);
}
}

public class trl {


public static void main(String args[]) {

JFrame f= new JFrame();


f.setSize(500,300);
f.setVisible(true);
f.setLayout(null);
Trafficlight t= new Trafficlight();
f.add(t);
}
}

OUTPUT-
19-02-2022
Implementation of doubly linked list

Write a Java program for the following:


1) Create a doubly linked list of elements.
2) Delete a given element from the above list.
3) Display the contents of the list after deletion.

PROGRAM-

package doubly;
import java.util.*;

class node {
int data;
node next;
node prev;
}

class Doubly {
int j;
node temp;

void display(node ptr){


node p=ptr;
while (p!=null){
System.out.print(p.data + ", ");
p=p.next;
}
}

void delete (node n1,int pos){


node pt=n1;
while (pt!=null){
pt=pt.next;
j=j+1;
}
if(j-1<pos) {
System.out.print("\nEnter valid position\n");
}
else {
int i=0;
node p;
if (n1==null){
System.out.print("Empty!");
}
else{
if(pos==0){
n1=n1.next;}
else{
temp=n1;
while (i < pos - 1) {
temp = temp.next;
i=i+1;
}

p = temp.next;
temp.next = p.next;
}}
System.out.print("\nlinked list after deletion:\n");
display(n1);
}
}
}

public class doublyll {


public static void main(String args[]) {
Scanner sc= new Scanner(System.in);

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

Doubly d=new Doubly();

System.out.print("\nlinked list before deletion:\n");


d.display(n1);

int pos;
System.out.print("\nEnter position to delete from:\n");
pos=sc.nextInt();

d.delete(n1, pos);
}
}

OUTPUT-

linked list before deletion:


10, 20, 30,
Enter position to delete from:
1

linked list after deletion:


10, 30,

linked list before deletion:


10, 20, 30,
Enter position to delete from:
4

Enter valid position

You might also like