Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views36 pages

II-AI&DS Java Program

The document contains multiple Java programs demonstrating various concepts such as string extraction, interfaces, exception handling, multithreading, GUI components, and mouse events. Each program showcases different functionalities like drawing shapes, handling user input, and implementing menus. The output sections illustrate how the programs behave when executed.

Uploaded by

Saranya Mahe
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)
4 views36 pages

II-AI&DS Java Program

The document contains multiple Java programs demonstrating various concepts such as string extraction, interfaces, exception handling, multithreading, GUI components, and mouse events. Each program showcases different functionalities like drawing shapes, handling user input, and implementing menus. The output sections illustrate how the programs behave when executed.

Uploaded by

Saranya Mahe
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/ 36

PROGRAM:

import java.util.Scanner;

public class Extractedstring

public static void main(String[]args)

Scanner in=new Scanner (System.in);

System.out.print("Enter a string:");

String os=in.nextLine();

System.out.print("Enter the Starting Index Value:");

int n=in.nextInt();

System.out.print("Enter the Ending Index Value:");

int m=in.nextInt();

String ES=os.substring(n,m); System.out.println("Original String:"+os);

System.out.println("Extracted String:"+ES);

}
OUTPUT:
PROGRAM:

import java.util.Scanner;

interface flyable

void fly();

interface walkable

void walk();

class Bird implements flyable,walkable

Scanner sc= new Scanner(System.in);

public void fly()

System.out.println("Name the Bird That Fly:");

String f=sc.nextLine();

}
public void walk()

System.out.println("Name the Bird That walk:");

String w=sc.nextLine();

public class Multipleinheritancedemo

public static void main(String[]args)

Bird nk=new Bird();

nk.fly();

nk.walk();

System.out.println("SAVE BIRDS");

}
OUTPUT:
PROGRAM:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class PayoutOutOfBoundsException extends ArithmeticException
{
public PayoutOutOfBoundsException(String message)
{
super(message);
}
}
public class Main
{
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 PayoutOutOfBoundsException("PAY OUT OF BOUNDS");
}
System.out.println("ANSWER: " + c);
}
catch (PayoutOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
}
}
OUTPUT 1:

Enter Employee Salary:

5000

The given salary of Employee is 5000

OUTPUT 2:

Enter Employee Salary:

1500

PayOutOfBoundsException: Salary must be 3000 to 10000


PROGRAM:

import java.util.*;

public class MultiThread extends Thread

private int tableNumber;

private int max;

public MultiThread(int tableNumber, int max)

this.tableNumber = tableNumber;

this.max = max;

// @Override

public void run()

for (int i = 1; i <= max; i++)

System.out.println(tableNumber + " x " + i + " = " + (tableNumber * i));


try

Thread.sleep(1000); // Add a slight delay to better visualize the output

catch (InterruptedException e)

e.printStackTrace();

public static void main(String[] args)

int limit = 10;

Scanner in =new Scanner(System.in);

System.out.println("Enter three numbers for multiplication tables: ");

int x=in.nextInt();

int y=in.nextInt();
int z=in.nextInt();

// Create three threads with different priorities

MultiThread t1 = new MultiThread(x, limit);

MultiThread t2 = new MultiThread(y, limit);

MultiThread t3 = new MultiThread(z, limit);

// Set different priorities to the threads (MIN_PRIORITY = 1,

MAX_PRIORITY = 10)

t1.setPriority(Thread.MIN_PRIORITY);

t2.setPriority(Thread.NORM_PRIORITY);

t3.setPriority(Thread.MAX_PRIORITY);

// Start the threads

t1.start();

t2.start();

t3.start();

}
OUTPUT:

Enter three numbers for multiplication tables: 2

2x1=2

3x1=3

5x1=5

2x2=4

3x2=6

5 x 2 = 10

2x3=6

3x3=9

5 x 3 = 15

2x4=8

3 x 4 = 12

5 x 4 = 20

2 x 5 = 10

3 x 5 = 15

5 x 5 = 25
2 x 6 = 12

3 x 6 = 18

5 x 6 = 30

2 x 7 = 14

3 x 7 = 21

5 x 7 = 35

2 x 8 = 16

3 x 8 = 24

5 x 8 = 40

2 x 9 = 18

3 x 9 = 27

5 x 9 = 45

2 x 10 = 20

3 x 10 = 30

5 x 10 = 50
PROGRAM:

import java.awt.*;

import javax.swing.*;

public class ShapeDrawer extends JPanel

@Override

protected void paintComponent(Graphics g)

super.paintComponent(g);

// Set the background color to white

setBackground(Color.WHITE);

// Draw a red circle

g.setColor(Color.RED);

g.fillOval(50, 50, 100, 100);

// Draw a blue rectangle

g.setColor(Color.BLUE);

g.fillRect(200, 50, 150, 100);


// Draw a green triangle

g.setColor(Color.GREEN);

int[] xPoints = {300, 350, 400};

int[] yPoints = {150, 50, 150};

g.fillPolygon(xPoints, yPoints, 3);

// Draw a yellow ellipse

g.setColor(Color.YELLOW);

g.fillOval(450, 50, 150, 75);

public static void main(String[] args)

JFrame frame = new JFrame("Shape Drawer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new ShapeDrawer());

frame.setSize(600, 200);

frame.setVisible(true);

}
OUTPUT:
PROGRAM:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MultiSelectionListbox extends JFrame
{
public JLabel label;
public JButton submit;
public JList<String> list1, list2;
public MultiSelectionListbox()
{
setTitle("Multiple Selection ListBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(new FlowLayout(FlowLayout.LEFT));
String values[] = {"C", "C++", "Java", "Python", "R", "HTML", "XML",
"CSS", "PHP"};
label = new JLabel("Which Languages do you know?");
list1 = new JList<>(values);
list2 = new JList<>();
list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SEL
ECTION);
submit = new JButton("Submit");
submit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
list2.setListData(list1.getSelectedValuesList().toArray(new String[0]));
}
};
add(label);
add(new JScrollPane(list1));
add(submit);
add(new JScrollPane(list2));
setVisible(true);
}
public static void main(String[] args)
{
new MultiSelectionListbox();
}
}
OUTPUT:
PROGRAM:

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class UserForm

public static void main(String[] args)

// Create the main frame

JFrame frame = new JFrame("User Profile Form");

frame.setSize(320, 500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));

// Create labels and text fields

JLabel nameLabel = new JLabel("Name:");

JTextField name = new JTextField(20);


JLabel ageLabel = new JLabel("Age:");

JTextField age = new JTextField(20);

JLabel qualificationLabel = new JLabel("Qualification:");

JTextField qualification = new JTextField(20);

JLabel addressLabel = new JLabel("Address:");

JTextArea address = new JTextArea(5, 20);

// Create a button to trigger the action

JButton submit = new JButton("Submit");

// Add components to the frame

frame.add(nameLabel);

frame.add(name);

frame.add(ageLabel);

frame.add(age);

frame.add(qualificationLabel);

frame.add(qualification);

frame.add(addressLabel);

frame.add(new JScrollPane(address));
frame.add(submit);

// Create ActionListener for the button

submit.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e)

// Display all values in a dialog box

String message = "Name: " + name.getText() + "\nAge: " + age.getText() +

"\nQualification: " + qualification.getText() + "\nAddress:\n" +

address.getText();

JOptionPane.showMessageDialog(frame, message);

};

// Set frame visibility

frame.setVisible(true);

}
OUTPUT:
PROGRAM:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
public class UserMenu
{
private static String text="";
private static JTextArea content;
public static void main(String[] args)
{
JFrame frame = new JFrame("Menu Program");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content=new JTextArea(50,50);
content.setFont(new Font("Serif",Font.PLAIN,20));
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("Edit");
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
JMenuItem selectallItem = new JMenuItem("Select All");
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(selectallItem);
JMenu helpMenu = new JMenu("Help");
JMenuItem aboutItem = new JMenuItem("About");
helpMenu.add(aboutItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
frame.add(new JScrollPane(content));
frame.setVisible(true);
newItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
content.setText("");
}
});
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
cutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text=(String)content.getSelectedText();
content.replaceSelection("");
}
});
copyItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text=(String)content.getSelectedText();
}
});
pasteItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int pos=content.getCaretPosition(); content.insert(text,pos);
}
});
selectallItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
content.selectAll();
}
});
aboutItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "Menu Bar and Pull DownMenu
Program");
}
});
}
}
OUTPUT:
PROGRAM:`.

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class MouseClicks extends JFrame

public MouseClicks()

setTitle("Mouse Events ");

setSize(400, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel l=new JLabel();

add(l);

addMouseListener(new MouseAdapter()

public void mouseClicked(MouseEvent e)

{
l.setText("Mouse Clicked");

public void mouseEntered(MouseEvent e)

l.setText("Mouse Entered");

public void mousePressed(MouseEvent e)

l.setText("Mouse Pressed");

public void mouseExited(MouseEvent e)

l.setText("Mouse Exited");

public void mouseReleased(MouseEvent e)

l.setText("Mouse Released");
}

});

setVisible(true);

public static void main(String[] args)

new MouseClicks();

}
OUTPUT:
PROGRAM:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class ShapeDrawer extends JPanel implements MouseListener

private int x, y;

private boolean drawCircle = true;

public ShapeDrawer() { addMouseListener(this);

public void paintComponent(Graphics g)

super.paintComponent(g);

if (drawCircle) {g.drawOval(x, y, 50, 50); // draw circle

else

{
if (x < y)

g.drawRect(x, y, 50, 50); // draw square

else

g.drawRoundRect(x, y, 50, 50, 20, 20); // draw rectangle

public void mouseClicked(MouseEvent e)

x = e.getX();

y = e.getY();

drawCircle = drawCircle; // toggle between circle and rectangle repaint();

}
public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public static void main(String[] args) {

JFrame frame = new JFrame("Shape Drawer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new ShapeDrawer());

frame.setSize(400, 400);

frame.setVisible(true);

}
OUTPUT:

You might also like