0% found this document useful (0 votes)
12 views

Java Exception Handling

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)
12 views

Java Exception Handling

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

CODING:

import java.io.*;
import java.lang.String;
class list1
{
public static void main(String args[])
{
try
{
BufferedReader in =new BufferedReader(new
InputStreamReader(System.in));
int a,b;
System.out.println("ENTER THE STRING:");
String s1=in.readLine();
System.out.println("ENTER THE STARTING POSITION VALUE:");
a=Integer.parseInt(in.readLine());
System.out.println("ENTER THE ENDING POSITION VALUE:");
b=Integer.parseInt(in.readLine());
String s2=(s1.substring(a,b));
System.out.println("YOUR CHARACTER STRING IS:" +s2);
}
catch(IOException e)
{
System.out.println("ERROR");
System.exit(1);
}
}
}
OUTPUT:
CODING:
import java.io.*;
import java.lang.String;
class student
{
String name;
int regno;
void getdata(String sname,int rno)
{
name=sname;
regno=rno;
}
void putdata()
{
System.out.println("NAME:" +name);
System.out.println("REGNO:" +regno);
}
}
class mark extends student
{
int m1,m2,m3;
void getmarks(int mark1,int mark2,int mark3)
{
m1=mark1;
m2=mark2;
m3=mark3;
}
void putmarks()
{
System.out.println("MARK1:" +m1);
System.out.println("MARK2:" +m2);
System.out.println("MARK3:" +m3);
}
}
interface s
{
int pract_mark=60;
}
class result extends mark implements s
{
int total;
void display()
{
putdata();
putmarks();
System.out.println("PRACTICAL MARK:" +pract_mark);
total=m1+m2+m3+pract_mark;
System.out.println("TOTAL :" +total);
if ((m1 > 40) && (m2 > 40) && (m3 > 40) && (pract_mark > 40))
System.out.println("THE STUDENT IS PASS");
else
System.out.println("THE STUDENT IS FAIL");
}
}
class list2
{
public static void main(String args[])
{
result st=new result();
st.getdata("Yashwanth",1001);
st.getmarks(98,99,100);
st.display();
}
}
OUTPUT:
CODING:
import java.io.*;
class list3
{
public static void main(String args[])throws IOException
{
BufferedReader in =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("ENTER A:");
int a = Integer.parseInt(in.readLine());
System.out.println("ENTER B:");
int b = Integer.parseInt(in.readLine());
int c;
try
{
c = b - a;
if (c > 32768)
{
throw new ArithmeticException("PAY OUT BOUNDS");
}
System.out.println("ANSWER:" + c);
}
catch (ArithmeticException e)
{
System.out.println(e);
}
}
}
OUTPUT:
CODING:
import java.io.*;
class A extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println(i + "*"+5+ "="+(i * 5));
}
System.out.println("END OF THE 1st THREAD");
}
}
class B extends Thread
{
public void run()
{
for (int j = 1; j <= 7; j++)
{
System.out.println(j + "*" +7+ "=" +(j * 7));
}
System.out.println("END OF THE 2st THREAD");
}
}
class C extends Thread
{
public void run()
{
for (int k = 1; k <= 13; k++)
{
System.out.println(k + "*" +13+ "=" +(k * 13));
}
System.out.println("END OF THE 3st THREAD");
}
}
public class list4
{
public static void main(String args[])throws IOException
{
A ThreadA=new A();
B ThreadB=new B();
C ThreadC=new C();
ThreadA.setPriority(Thread.MAX_PRIORITY);
ThreadB.setPriority(Thread.NORM_PRIORITY);
ThreadC.setPriority(Thread.MIN_PRIORITY);
ThreadA.start();
ThreadB.start();
ThreadC.start();
}
}
OUTPUT:
CODING:
import java.awt.*;
import javax.swing.*;
public class list5 extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Circle
g.setColor(Color.RED);
g.drawString("Circle", 20, 20);
g.setColor(Color.GREEN);
g.fillOval(20, 30, 40, 40);
// Draw Ellipse
g.setColor(Color.RED);
g.drawString("Ellipse", 20, 100);
g.setColor(Color.PINK);
g.fillOval(20, 110, 50, 40);
// Draw Square
g.setColor(Color.RED);
g.drawString("Square", 90, 80);
g.setColor(Color.MAGENTA);
g.fillRect(90, 90, 90, 90);
// Draw Rectangle
g.setColor(Color.RED);
g.drawString("Rectangle", 80, 30);
g.setColor(Color.BLUE);
g.fillRect(80, 40, 80, 40);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new list5());
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
OUTPUT:
CODING:
import java.awt.*;
import java.awt.event.*;

public class list6 extends Frame implements WindowListener, ActionListener {


TextField tname, tstreet, tcity, tpincode;
Label lname, lstreet, lcity, lpincode;
Button mydet;

public list6() {
// Set layout and frame title
setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
setTitle("Personal Details");

// Initialize text fields and labels


tname = new TextField(20);
tstreet = new TextField(20);
tcity = new TextField(20);
tpincode = new TextField(20);

lname = new Label("NAME", Label.RIGHT);


lstreet = new Label("STREET", Label.RIGHT);
lcity = new Label("CITY", Label.RIGHT);
lpincode = new Label("PINCODE", Label.RIGHT);

mydet = new Button("MY DETAIL");

// Add components to the frame


add(mydet);
add(lname);
add(tname);
add(lstreet);
add(tstreet);
add(lcity);
add(tcity);
add(lpincode);
add(tpincode);

// Set initial text for text fields


tname.setText("");
tstreet.setText("");
tcity.setText("");
tpincode.setText("");

// Add action and window listeners


mydet.addActionListener(this);
addWindowListener(this);

// Set frame size and visibility


setSize(320, 240);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if (e.getSource() == mydet() {
tname.setText("tharmar");
tstreet.setText("B172, TNHB Colony, Elango Nagar");
tcity.setText("Civil Aerodrome Post, Coimbatore");
tpincode.setText("641 014");
}
}

public void windowClosing(WindowEvent e) {


setVisible(false);
dispose();
System.exit(0);
}

public void windowOpened(WindowEvent e) {}


public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {


new list6();
}
OUTPUT:
CODING:
import java.awt.*;
import javax.swing.*;

public class list7 extends JFrame {


JList<String> colours, shapes;
DefaultListModel<String> colourModel, shapeModel;
JTextArea display;

public list7() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);

// Initialize list models


colourModel = new DefaultListModel<>();
shapeModel = new DefaultListModel<>();

// Add items to colour model


colourModel.addElement("Blue");
colourModel.addElement("Red");
colourModel.addElement("Black");
colourModel.addElement("Cyan");

// Add items to shape model


shapeModel.addElement("Circle");
shapeModel.addElement("Rectangle");
shapeModel.addElement("Oval");
shapeModel.addElement("Square");

// Initialize lists
colours = new JList<>(colourModel);
shapes = new JList<>(shapeModel);

// Enable multiple selections for colours

colours.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELE
CTION);

// Enable single selection for shapes


shapes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// Add lists to scroll panes


JScrollPane colourPane = new JScrollPane(colours);
JScrollPane shapePane = new JScrollPane(shapes);

// Initialize display area


display = new JTextArea(5, 20);
display.setEditable(false);

// Add components to frame with GridBagLayout


gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
add(new JLabel("Colours:"), gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
add(colourPane, gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
add(new JLabel("Shapes:"), gbc);

gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
add(shapePane, gbc);

gbc.gridx = 0;
gbc.gridy = 4;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
add(display, gbc);

// Register listeners
colours.addListSelectionListener(e -> updateDisplay());
shapes.addListSelectionListener(e -> updateDisplay());

// Set frame properties


setTitle("Multiple Selection List Box");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

private void updateDisplay() {


String msg = "Colours: ";
for (String colour : colours.getSelectedValuesList()) {
msg += colour + " ";
}
msg += "\nShape: " + shapes.getSelectedValue();
display.setText(msg);
}

public static void main(String[] args) {


new list7();
}
}
OUTPUT
CODING:
import java.awt.*;
import java.awt.event.*;

public class list8 extends Frame implements WindowListener {

public list8() {
FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 10, 10);
setLayout(flow);

TextField tname = new TextField(15);


TextField tage = new TextField(3);
TextField tqualification = new TextField(20);
TextArea taddress = new TextArea(5, 20);

Label lname = new Label("NAME:", Label.RIGHT);


Label lage = new Label("AGE:", Label.RIGHT);
Label lqualification = new Label("QUALIFICATION:", Label.RIGHT);
Label laddress = new Label("ADDRESS:", Label.RIGHT);

add(lname); add(tname);
add(lage); add(tage);
add(lqualification); add(tqualification);
add(laddress); add(taddress);

tname.setText("tharmar");
tage.setText("18");
tqualification.setText("Bsc");
taddress.setText(" airport Coimbatore-14");

addWindowListener(this);

setSize(600, 400);
setVisible(true);
}

// Implement WindowListener methods


public void windowOpened(WindowEvent e) {}
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public static void main(String[] args) {


new list8();
}
}
OUTPUT:
CODING:
import java.awt.*;
import java.awt.event.*;

public class list9 extends Frame implements ActionListener,


WindowListener {

MenuBar a = new MenuBar();


Menu FileMenu = new Menu("FILE");
Menu EditMenu = new Menu("EDIT");
Menu SearchMenu = new Menu("SEARCH");

MenuItem NewItem = new MenuItem("NEW");


MenuItem OpenItem = new MenuItem("OPEN");
MenuItem SaveItem = new MenuItem("SAVE");
MenuItem SaveasItem = new MenuItem("SAVE AS");
MenuItem ExitItem = new MenuItem("EXIT");

MenuItem CutItem = new MenuItem("CUT");


MenuItem CopyItem = new MenuItem("COPY");
MenuItem PasteItem = new MenuItem("PASTE");

MenuItem FindItem = new MenuItem("FIND");


MenuItem ReplaceItem = new MenuItem("REPLACE");

public list9() {
setTitle("Menu Bar Example");
setMenuBar(a);

a.add(FileMenu);
a.add(EditMenu);
a.add(SearchMenu);

FileMenu.add(NewItem);
FileMenu.add(OpenItem);
FileMenu.add(SaveItem);
FileMenu.add(SaveasItem);
FileMenu.add(ExitItem);

EditMenu.add(CutItem);
EditMenu.add(CopyItem);
EditMenu.add(PasteItem);

SearchMenu.add(FindItem);
SearchMenu.add(ReplaceItem);

NewItem.addActionListener(this);
OpenItem.addActionListener(this);
SaveItem.addActionListener(this);
SaveasItem.addActionListener(this);
ExitItem.addActionListener(this);
CutItem.addActionListener(this);
CopyItem.addActionListener(this);
PasteItem.addActionListener(this);
FindItem.addActionListener(this);
ReplaceItem.addActionListener(this);

addWindowListener(this);
setSize(320, 240);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if (e.getSource() == ExitItem) {
quitApp();
} else {
System.out.println(e.getActionCommand() + " selected");
}
}

public void windowClosed(WindowEvent e) {}

public void windowClosing(WindowEvent e) {


quitApp();
}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void quitApp() {
setVisible(false);
dispose();
System.exit(0);
}
public static void main(String[] args) {
new list9();
}
}
OUTPUT:
CODING:
import java.awt.*;
import java.awt.event.*;
public class list10 extends Frame implements MouseListener,
MouseMotionListener, WindowListener {

String s = " ";

public list10() {
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(this);
setSize(320, 240);
setVisible(true);
}

public void mouseClicked(MouseEvent e) {


s = "Mouse Clicked";
repaint();
}

public void mouseReleased(MouseEvent e) {


s = "Mouse Up";
repaint();
}

public void mouseEntered(MouseEvent e) {


s = "Mouse Entered";
repaint();
}
public void mouseDragged(MouseEvent e) {
s = "Mouse Dragged";
repaint();
}

public void mouseMoved(MouseEvent e) {


s = "Mouse Moved";
repaint();
}

public void mouseExited(MouseEvent e) {


s = "Mouse Exited";
repaint();
}

public void mousePressed(MouseEvent e) {


s = "Mouse Pressed";
repaint();
}

public void paint(Graphics g) {


g.drawString(s, 50, 100);
}

public void windowClosed(WindowEvent e) {


}

public void windowClosing(WindowEvent e) {


setVisible(false);
dispose();
System.exit(0);
}
public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {


}

public void windowOpened(WindowEvent e) {


}

public void windowActivated(WindowEvent e) {


}

public void windowDeactivated(WindowEvent e) {


}

public static void main(String[] args) {


new list10();
}
}
OUTPUT:
CODING:
import java.awt.*;

import java.awt.event.*;

public class list11 extends Frame implements KeyListener, MouseListener,


MouseMotionListener {

int x = 1;

int x1, x2, y1, y2;

int i = 0, j = 0;

public list11() {

addMouseListener(this);

addMouseMotionListener(this);

addKeyListener(this);

setSize(400, 400);

setVisible(true);

public void keyPressed(KeyEvent ke) {

char ch = ke.getKeyChar();

if (ch == 'o') {

x = 1; // Draw Oval

if (ch == 'c') {

x = 2; // Draw Circle (assuming same method as oval)

if (ch == 'r') {
x = 3; // Draw Rectangle

if (ch == 's') {

x = 4; // Draw Square (assuming same method as rectangle)

repaint();

public void keyTyped(KeyEvent ke) {}

public void keyReleased(KeyEvent ke) {}

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e) {

x1 = e.getX();

y1 = e.getY();

public void mouseReleased(MouseEvent e) {

x2 = e.getX();

y2 = e.getY();

repaint();

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {

x2 = e.getX();

y2 = e.getY();

repaint();
}

public void paint(Graphics g) {

g.drawString("Circle (c), Oval (o), Rectangle (r), Square (s)", 50, 50)

switch (x) {

case 1:

g.drawOval(x1, y1, x2 - x1, y2 - y1); // Draw Oval

break;

case 2:

g.drawOval(x1, y1, x2 - x1, y2 - y1); // Draw Circle (same as oval)

break;

case 3:

g.drawRect(x1, y1, x2 - x1, y2 - y1); // Draw Rectangle

break;

case 4:

g.drawRect(x1, y1, x2 - x1, y2 - y1); // Draw Square (same as rectangle)

break;

public static void main(String[] args) {

list11 frame = new list11();

frame.addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent e) {

System.exit(0);

});

}
OUTPUT:
CODING:
import java.io.*;

public class list12 {

public static void main(String args[]) {

RandomAccessFile newfile;

try {

newfile = new RandomAccessFile("new.txt", "rw");

newfile.seek(newfile.length());

BufferedReader reader = new BufferedReader(new


InputStreamReader(System.in));

System.out.println("Enter the text to be updated:");

String s = reader.readLine();

newfile.writeBytes(s + "\n"); // Append text with a newline to file

newfile.close();

System.out.println("Text appended successfully.");

} catch (IOException e) {

System.out.println("Error: " + e.getMessage());

}
OUTPUT:

You might also like