0% found this document useful (0 votes)
3 views27 pages

All Java Lab

The document contains multiple Java programs demonstrating various concepts such as string manipulation, multiple inheritance using interfaces, custom exceptions, multithreading, GUI applications with shapes, user forms, menus, mouse events, and drawing shapes based on mouse clicks. Each program includes source code and a brief description of its functionality. The examples illustrate fundamental Java programming techniques and graphical user interface (GUI) development.

Uploaded by

vinithamadesh0
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)
3 views27 pages

All Java Lab

The document contains multiple Java programs demonstrating various concepts such as string manipulation, multiple inheritance using interfaces, custom exceptions, multithreading, GUI applications with shapes, user forms, menus, mouse events, and drawing shapes based on mouse clicks. Each program includes source code and a brief description of its functionality. The examples illustrate fundamental Java programming techniques and graphical user interface (GUI) development.

Uploaded by

vinithamadesh0
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

1.

Write a Java Applications to extract a portion of a character string and print the
extracted string.
Source Code
import [Link];
public class PrintString
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
[Link]("Enter a string : ");
String s = [Link]();
int len = [Link]();
[Link]("Enter index of starting character : ");
int n = [Link]();
if(n < 0 || n >= len)
{
[Link]("Invalid index");
[Link](1);
}
[Link]("Enter number of characters to extract from String :\n ");
int m = [Link]();
int substrend = n + m;
if( m <= 0 || substrend > len)
{
[Link]("Invalid number of characters");
[Link](1);
}
String extstr = [Link](n, substrend);
[Link]("Extracted String = " + extstr);
}
}
Output:

[Link] a Java Program to implement the concept of multiple inheritance using Interfaces
Source Code
import [Link];
//Student Detail
interface Detail
{
final String academic_year="2023-2024";
public void getDetail();
}
//Student Marks
interface Marks
{
public void getMarks();
public void print();
}
//multiple inheritance using inteface
class Student implements Detail,Marks
{
String rollno,name,degree,sem;
int m1,m2,m3,total;
float percent;
public void getDetail()
{
Scanner in = new Scanner([Link]);
[Link]("Enter Student Roll Number : ");
rollno = [Link]();
[Link]("Enter Student Name : ");
name = [Link]();
[Link]("Enter degree : ");
degree = [Link]();
[Link]("Enter year : ");
sem = [Link]();
}
public void getMarks()
{
Scanner in = new Scanner([Link]);
[Link]("Enter Mark1 : ");
m1 = [Link]();
[Link]("Enter Mark2 : ");
m2 = [Link]();
[Link]("Enter Mark3 : ");
m3 = [Link]();
total=m1+m2+m3;
percent=(total*100)/300;
}
public void print()
{
[Link]("\nAcademic Year: "+academic_year);
[Link]("Student Roll Number: "+rollno);
[Link]("Student Name: "+name);
[Link]("Degree: "+degree);
[Link]("Semester: "+sem);
[Link]("Total Marks: "+total);
[Link]("Percentage: "+percent);
}
public static void main (String args[])
{
Student s = new Student();
[Link]();
[Link]();
[Link]();
}
}
Output

[Link] a Java Program to create an Exception called PayOut of Bounds and throw the
exception.
Source Code
import [Link].*;
//user defined exception class for pay out of bounds Exception
class PayOutOfBoundsException extends Exception
{
//Constructor
public PayOutOfBoundsException(String s)
{
super(s); //calls Exception class Constructor
}
}
//Main class for User defined Exception
public class Salary
{
public static void main(String args[])
{

Scanner in =new Scanner([Link]);


[Link]("Enter Employee Salary: ");
//Get Employee salary from User
int esal=[Link]();
try{
//Check if employee salary not between 3000 and 10000
if(esal<3000 || esal>10000)
{
// throw pay out of bounds exception with a message
throw new PayOutOfBoundsException("Salary must be 3000 to 10000");
}
//print employee salary if given salary is within bounds
[Link]("The given salary of Employee is "+esal);
}
//catch block for payoutof bounds exception
catch(PayOutOfBoundsException e)
{
//catch the exception and prints the error message
[Link](e);
}
}
}

Output:
[Link] a Java Program to implement the concept of multithreading with the use of any
three multiplication tables and assign three different priorities to them.
Source Code
import [Link].*;
public class MultiThread extends Thread
{
private int tableNumber;
private int max;
public MultiThread(int tableNumber, int max)
{
[Link] = tableNumber;
[Link] = max;
}
// @Override
public void run() {
for (int i = 1; i <= max; i++) {
[Link](tableNumber + " x " + i + " = " + (tableNumber * i));
try {
[Link](1000); // Add a slight delay to better visualize the output
} catch (InterruptedException e) {
[Link]();
}
}
}
public static void main(String[] args)
{
int limit = 10;
Scanner in =new Scanner([Link]);
[Link]("Enter three numbers for multiplication tables: ");
int x=[Link]();
int y=[Link]();
int z=[Link]();
// 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)
[Link](Thread.MIN_PRIORITY);
[Link](Thread.NORM_PRIORITY);
[Link](Thread.MAX_PRIORITY);
// Start the threads
[Link]();
[Link]();
[Link]();
}
}

OUTPUT:
[Link] a Java Program to draw several shapes in the created windows.
Syntax for Methods used in this program
To draw Oval:
drawOval(int x, int y, int width, int height)
To change Pen Color:
setColor(Color c)
To color the Oval:
fillOval(int x, int y, int width, int height)
To draw rectangle:
drawRect(int x, int y, int width, int height)
To draw line:
drawLine(int x1, int y1, int x2, int y2)
To draw arc:
drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Source Code
import [Link].*;
import [Link].*;
public class Shapes extends Applet
{
public void paint(Graphics g)
{
[Link](new Font("Calibri", [Link],30));
[Link]("Different Shapes in Applet", 20, 20);
[Link](50,50,80,100);
[Link]([Link]);
[Link](50,50,80,100);
[Link](150,150,120,100);
[Link](270,270,350,350);
[Link](60,280,100,80,180,180);
}
}
/* <applet code="[Link]" width="400" height="400">
</applet>
*/
Output:
[Link] a Java Program to demonstrate the Multiple Selection List-box
Source Code
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class MultiSelectionListbox extends JFrame
{
public JLabel label;
public JButton submit;
public JList list1,list2;
public MultiSelectionListbox()
{
setTitle("Multiple Selection ListBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(new FlowLayout([Link]));
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();
[Link](ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
submit = new JButton("Submit");
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]([Link]());
}
});
add(label);
add(new JScrollPane(list1));
add(list1);
add(submit);
add(new JScrollPane(list2));
add(list2);
setVisible(true);
}
public static void main(String[] args)
{
new MultiSelectionListbox();}}
OUTPUT
[Link] a Java Program to create a frame with three text fields for name, age and
qualification and a text field for multiple line for address
Source Code
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class UserForm
{
public static void main(String[] args)
{
// Create the main frame
JFrame frame = new JFrame("User Profile Form");
[Link](320, 500);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout([Link],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
[Link](nameLabel);
[Link](name);
[Link](ageLabel);
[Link](age);
[Link](qualificationLabel);
[Link](qualification);
[Link](addressLabel);
[Link](new JScrollPane(address));
[Link](submit);
// Create ActionListener for the button
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Display all values in a dialog box
String message = "Name: " + [Link]() + "\nAge: " + [Link]() + "\nQualification: " +
[Link]() + "\nAddress:\n" + [Link]();
[Link](frame, message);
}
});

// Set frame visibility


[Link](true);
}
}
OUTPUT
[Link] a Java Program to create Menu Bars and pull down menus
Source Code
import [Link].*;
import [Link];
import [Link];
import [Link].*;
public class UserMenu
{
private static String text="";
private static JTextArea content;
public static void main(String[] args)
{
JFrame frame = new JFrame("Menu Program");
[Link](500, 500);
[Link](JFrame.EXIT_ON_CLOSE);
content=new JTextArea(50,50);
[Link](new Font("Serif",[Link],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");
[Link](newItem);
[Link](openItem);
[Link](saveItem);
[Link]();
[Link](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");
[Link](cutItem);
[Link](copyItem);
[Link](pasteItem);
[Link](selectallItem);
JMenu helpMenu = new JMenu("Help");
JMenuItem aboutItem = new JMenuItem("About");
[Link](aboutItem);
[Link](fileMenu);
[Link](editMenu);
[Link](helpMenu);
[Link](menuBar);
[Link](new JScrollPane(content));
[Link](true);
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](0);
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text=(String)[Link]();
[Link]("");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text=(String)[Link]();
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int pos=[Link]();
[Link](text,pos);
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]();
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link](frame, "Menu Bar and Pull DownMenu Program");
}
});
}
}

OUTPUT:
[Link] a Java Program to create a frame which responds to the mouse clicks. For each
events with mouse such as mouse up, mouse down, etc, the corresponding message to be
displayed
Source Code
import [Link].*;
import [Link].*;
import [Link];
import [Link];
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)
{
[Link]("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
[Link]("Mouse Entered");
}
public void mousePressed(MouseEvent e)
{
[Link]("Mouse Pressed");
}
public void mouseExited(MouseEvent e)
{
[Link]("Mouse Exited");
}
public void mouseReleased(MouseEvent e)
{
[Link]("Mouse Released");
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MouseClicks();
}
}

OUTPUT
[Link] a Java Program to draw a circle, square, ellipse and rectangle at the mouse click
positions
Source Code
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class MouseDraw extends JFrame
{
private Point clickPoint;
private String selectedShape;
public MouseDraw()
{
setTitle("MouseDrawer");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
clickPoint = [Link]();
repaint(); //calls paint method
}
});
String[] shapes = {"Circle", "Square", "Ellipse", "Rectangle"};
JComboBox<String> shapeCB = new JComboBox<String>(shapes);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e){
selectedShape = (String) [Link]();
repaint();
}
});
add(new JLabel("Select Shape: "));
add(shapeCB);
}
public void paint(Graphics g)
{
[Link](g);
if (clickPoint != null)
{
int size = 50;
if ([Link]("Circle")) {
[Link](clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
} else if ([Link]("Square")) {
[Link](clickPoint.x - size / 2, clickPoint.y - size / 2, size, size);
} else if ([Link]("Ellipse")) {
[Link](clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
} else if ([Link]("Rectangle")) {
[Link](clickPoint.x - size, clickPoint.y - size / 2, size * 2, size);
}
}

}
public static void main(String[] args)
{

MouseDraw shapes = new MouseDraw();


[Link](true);
}
}

OUTPUT:

You might also like