0% found this document useful (0 votes)
20 views34 pages

Mukul Java File

Uploaded by

rajia9782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views34 pages

Mukul Java File

Uploaded by

rajia9782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Core Java Lab (CS-512)

22010203049

ATAL BIHARI VAJPAYEE GOVT. INSTITUTE OF


ENGINEERING AND TECHNOLOGY PRAGATINAGAR
(SHIMLA)

CORE JAVA LAB


LAB FILE
(CS-512)

Submitted By- Submitted To-


SHIVANSH Er. Namita Chandel

Roll No- Branch -


22010203049 5th Sem CSE

Signature-
Core Java Lab (CS-512)
22010203049
SR. NAME OF PRACTICAL PAGE DATE SUBMISSION TEACHER’S
NO. NO. DATE SIGNATURE
Core Java Lab (CS-512)
22010203049
Introduction and 1-5 08-08- 22-08-24
installation of JDK, 24
JVM&JRE.
1. Demonstrating the use 6-9 08-08- 22-08-24
of methods of math 24
class.
2. Demonstrating the use 10-13 22-08- 05-09-24
of methods of string 24
class.
3. Program to 14-23 29-08- 14-11-24
demonstrating 24
inheritance.
4. To demonstrate super 24-25 05-09- 14-11-24
and this. 24
5. Program to 26-27 12-09- 14-11-24
demonstrate 24
interfaces.
6. Program to static 28-29 19-09- 14-11-24
variables and methods. 24
7. Program to 30-33 26-09- 14-11-24
demonstrate 24
exception.
8. Program to 34-35 03-10- 14-11-24
demonstrate creation 24
of a frame
9. To demonstrate labels 36-37 03-10- 14-11-24
and buttons with 24
proper events.
10. To demonstrate menu 38-43 03-10- 14-11-24
bars and menus. 24
11. To demonstrate dialog 44-45 03-10- 14-11-24
boxes. 24
12. To demonstrate 46-47 07-11- 14-11-24
checkboxes with 24
proper events.
13. To demonstrate 48-51 07-11- 14-11-24
checkbox groups with 24
proper events.
14. To demonstrate lists 52-55 14-11- 20-11-24
and text fields with 24
proper events.
15. To demonstrate scroll 56-57 14-11- 20-11-24
bars with proper event 24

16. To create registration 58-62 14-11- 20-11-24


form. 24
Core Java Lab (CS-512)
22010203049
Practical- 7
Aim: To demonstrate exceptions.

Output :

a) Try catch with multiple catch.

b) Using throws keyword do multithreading by using sleep and join.

42
Core Java Lab (CS-512)
22010203049
Practical- 7
Aim: To demonstrate exceptions.

Program:
a)Try catch with multiple catch.

public class Customizedexp {


public static void main(String[] args) {
try{
int a=10; int b=0; int c=a/b;
System.out.println(" the value of c is: "+c);

int arr[]={1,2,3,4,5};
System.out.print(arr[5]);

String s="Amit";
System.out.println(s.charAt(4));
}
catch(ArithmeticException e){
System.out.println("trying to divide by 0 which gives error.");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("The Index you have entered in the array bound for extraction is
out of the maximum size!. So, ArrayIndexOutOfBoundsException error.");}
catch(StringIndexOutOfBoundsException e){
System.out.println("The Index you have entered in the string bound for extraction
is out of the maximum size of it!. So, StringIndexOutOfBoundsException error.");}}}

b) Using throws keyword do multithreading by using sleep and join.

class MyThread extends Thread {


public MyThread(String name) {
super(name);}
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + ": Count " + i);
Thread.sleep(1000); // Sleep for 1 second}
catch (InterruptedException e) {
System.out.println(getName() + " interrupted.");}}}
public class Main {
public static void main(String[] args) throws InterruptedException {
MyThread thread1 = new MyThread("Thread 1");
MyThread thread2 = new MyThread("Thread 2");
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("Both threads have finished.");}}

43
Core Java Lab (CS-512)
22010203049
c) Using throw keyword in user defined exceptions.

44
Core Java Lab (CS-512)
22010203049
c) Using throw keyword in user defined exceptions.

class CustomException extends Exception {


public CustomException(String message) {
super(message);}}
public class Main {
public static void main(String[] args) {
try {
int age = 15;
if (age < 18) {
throw new CustomException("You are too young to vote.");}
else {
System.out.println("You are eligible to vote.");}}
catch (CustomException e) {
System.out.println("Custom Exception Caught: " + e.getMessage());}}}

45
Core Java Lab (CS-512)
22010203049
Practical- 8
Aim: To demonstrate creation of a frame.

Output:

46
Core Java Lab (CS-512)
22010203049
Practical- 8

Aim: To demonstrate creation of a frame.

Program:

import javax.swing.*;

public class frame {

public static void main(String[] args) {


JFrame frame = new JFrame("Swing Frame Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello", JLabel.CENTER);
frame.add(label);
frame.setVisible(true);
}
}

47
Core Java Lab (CS-512)
22010203049
Practical- 9
Aim: To demonstrate labels and buttons with proper events.

Output:

48
Core Java Lab (CS-512)
22010203049

Practical- 9
Aim: To demonstrate labels and buttons with proper events.

Program:
import javax.swing.*;
import java.awt.event.*;

public class labelandbutton {

public static void main(String[] args) {


JFrame frame = new JFrame("Label and Button Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Click the button", JLabel.CENTER);


JButton button = new JButton("Click Me");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});

JPanel panel = new JPanel();


panel.add(label);
panel.add(button);

frame.add(panel);
frame.setVisible(true);
}}

49
Core Java Lab (CS-512)
22010203049
Practical- 10
Aim: To demonstrate menu bar and menu items.

Output:

50
Core Java Lab (CS-512)
22010203049
Practical- 10
Aim: To demonstrate menu bar and menu items.

Program:
import javax.swing.*;
import java.awt.event.*;

public class menu {

public static void main(String[] args) {


JFrame frame = new JFrame("Menu Bar Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");


JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");

JMenuItem newItem = new JMenuItem("New");


JMenuItem openItem = new JMenuItem("Open");
JMenuItem saveItem = new JMenuItem("Save");

JMenuItem cutItem = new JMenuItem("Cut");


JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");

JMenuItem aboutItem = new JMenuItem("About");

newItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("New File Created");
}
});

openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("File Opened");
}
});

saveItem.addActionListener(new ActionListener() {

51
Core Java Lab (CS-512)
22010203049

52
Core Java Lab (CS-512)
22010203049
public void actionPerformed(ActionEvent e) {
System.out.println("File Saved");
}
});

cutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Cut Action");
}
});

copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Copy Action");
}
});

pasteItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Paste Action");
}
});

aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a simple menu bar
example.");
}
});

fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);

editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);

helpMenu.add(aboutItem);

menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);

53
Core Java Lab (CS-512)
22010203049

54
Core Java Lab (CS-512)
22010203049

frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
}

55
Core Java Lab (CS-512)
22010203049

Practical- 12
Aim: To demonstrate checkboxes with proper events.

Output:

56
Core Java Lab (CS-512)
22010203049
Practical- 12
Aim: To demonstrate checkboxes with proper events.

Program:
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CheckboxGroupExample {
CheckboxGroupExample() {
JFrame f = new JFrame("CheckboxGroup Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400, 100);
ButtonGroup buttonGroup = new ButtonGroup();
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100, 100, 100, 50);
buttonGroup.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Java");
checkBox2.setBounds(100, 150, 100, 50);
buttonGroup.add(checkBox2);
f.add(checkBox1);
f.add(checkBox2);
f.add(label);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
checkBox1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (checkBox1.isSelected()){label.setText("C++ checkbox: Checked");}
else{label.setText("C++ checkbox: Unchecked");}}
});
checkBox2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (checkBox2.isSelected()){label.setText("Java checkbox: Checked");}
else{label.setText("Java checkbox: Unchecked");}}
});
}
public static void main(String args[]) {
new CheckboxGroupExample();
}
}

57
Core Java Lab (CS-512)
22010203049
Practical- 13
Aim: To demonstrate checkbox groups with proper event.

Output:

58
Core Java Lab (CS-512)
22010203049
Practical- 13
Aim: To demonstrate checkbox groups with proper event.
Program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CheckboxGroupExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Checkbox Group Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());

JCheckBox checkBox1 = new JCheckBox("Option 1");


JCheckBox checkBox2 = new JCheckBox("Option 2");
JCheckBox checkBox3 = new JCheckBox("Option 3");

frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);

JButton submitButton = new JButton("Submit");


frame.add(submitButton);

JLabel resultLabel = new JLabel("Selected Options: None");


frame.add(resultLabel);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedOptions = "Selected Options: ";

if (checkBox1.isSelected()) {
selectedOptions += "Option 1 ";
}
if (checkBox2.isSelected()) {
selectedOptions += "Option 2 ";
}
if (checkBox3.isSelected()) {
selectedOptions += "Option 3 ";
}

59
Core Java Lab (CS-512)
22010203049

60
Core Java Lab (CS-512)
22010203049
if (selectedOptions.equals("Selected Options: ")) {
selectedOptions = "Selected Options: None";
}

resultLabel.setText(selectedOptions);
}
});

frame.setVisible(true);
}
}

61
Core Java Lab (CS-512)
22010203049
Practical- 14
Aim: To demonstrate lists and text fields with proper events

Output:

62
Core Java Lab (CS-512)
22010203049
Practical- 14
Aim: To demonstrate lists and text fields with proper events

Program:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class lists {

public static void main(String[] args) {


JFrame frame = new JFrame("List and TextField Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

String[] items = { "Apple", "Banana", "Cherry", "Date", "Elderberry" };


JList<String> list = new JList<>(items);

JScrollPane listScrollPane = new JScrollPane(list);

JTextField textField = new JTextField(20);

JButton button = new JButton("Show Selected Item");

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedItem = list.getSelectedValue();
if (selectedItem != null) {

63
Core Java Lab (CS-512)
22010203049

64
Core Java Lab (CS-512)
22010203049
textField.setText(selectedItem);

} else {
textField.setText("No item selected");
}
}
});

frame.setLayout(new FlowLayout());
frame.add(listScrollPane);
frame.add(button);
frame.add(textField);

frame.setVisible(true);
}
}

65
Core Java Lab (CS-512)
22010203049

Practical- 15
Aim: To demonstrate scroll bars with proper events.

Output:

66
Core Java Lab (CS-512)
22010203049

Practical- 15
Aim: To demonstrate scroll bars with proper events.

Program:
import javax.swing.*;
//import java.awt.*;

public class scroll {

public static void main(String[] args) {

JFrame frame = new JFrame("Scroll Bar Example");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

JTextArea textArea = new JTextArea(20, 30);


textArea.setText("hello scroll for more content"

);

JScrollPane scrollPane = new JScrollPane(textArea);

frame.add(scrollPane);

frame.setVisible(true);
}
}

67
Core Java Lab (CS-512)
22010203049
Practical- 16
Aim: To create registration form.

Output:

68
Core Java Lab (CS-512)
22010203049
Practical- 16
Aim: To create registration form.

Program:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class register {

JFrame frame;
JLabel nameLabel, rollNoLabel, courseLabel, branchLabel, termsLabel;
JTextField nameField, rollNoField;
JComboBox<String> courseComboBox, branchComboBox;
JCheckBox termsCheckBox;
JButton submitButton;

public register() {
frame = new JFrame("College Registration Form");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(6, 2, 10, 10));

nameLabel = new JLabel("Name:");


rollNoLabel = new JLabel("Roll No:");
courseLabel = new JLabel("Course:");
branchLabel = new JLabel("Branch:");
termsLabel = new JLabel("Accept Terms:");

nameField = new JTextField();


rollNoField = new JTextField();

String[] courses = { "B.Tech", "M.Tech", "B.Sc", "M.Sc", "MBA" };


String[] branches = { "Computer Science", "Mechanical", "Electrical", "Civil",
"Biotechnology" };

courseComboBox = new JComboBox<>(courses);


branchComboBox = new JComboBox<>(branches);

termsCheckBox = new JCheckBox();

69
Core Java Lab (CS-512)
22010203049

70
Core Java Lab (CS-512)
22010203049

submitButton = new JButton("Submit");

frame.add(nameLabel);
frame.add(nameField);

frame.add(rollNoLabel);
frame.add(rollNoField);

frame.add(courseLabel);
frame.add(courseComboBox);

frame.add(branchLabel);
frame.add(branchComboBox);

frame.add(termsLabel);
frame.add(termsCheckBox);

frame.add(new JLabel());
frame.add(submitButton);

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String rollNo = rollNoField.getText();
String course = (String) courseComboBox.getSelectedItem();
String branch = (String) branchComboBox.getSelectedItem();
boolean termsAccepted = termsCheckBox.isSelected();

if (!termsAccepted) {
JOptionPane.showMessageDialog(frame, "You must accept the terms and
conditions.");
return;
}

String message = String.format("Name: %s\nRoll No: %s\nCourse: %s\nBranch:


%s", name, rollNo, course,
branch);
JOptionPane.showMessageDialog(frame, message);
}
});

frame.setVisible(true);

71
Core Java Lab (CS-512)
22010203049
}

public static void main(String[] args) {


new register();
}
}

72

You might also like