Mukul Java File
Mukul Java File
22010203049
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
Output :
42
Core Java Lab (CS-512)
22010203049
Practical- 7
Aim: To demonstrate exceptions.
Program:
a)Try catch with multiple catch.
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.");}}}
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.
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
Program:
import javax.swing.*;
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.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});
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.*;
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;
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
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.*;
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.*;
);
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.*;
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));
69
Core Java Lab (CS-512)
22010203049
70
Core Java Lab (CS-512)
22010203049
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;
}
frame.setVisible(true);
71
Core Java Lab (CS-512)
22010203049
}
72