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

Java_Programs_List

The document contains multiple Java code examples demonstrating various programming concepts such as creating a menu bar, handling user-defined exceptions, type casting, using vectors, action listeners, result sets, constructors, grid layouts, threading, JTree, switch cases, summing digits, and URL handling. Each example is self-contained and illustrates a specific feature or functionality within Java. The code snippets are designed for educational purposes, showcasing fundamental programming techniques.

Uploaded by

sajansteve33
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)
6 views

Java_Programs_List

The document contains multiple Java code examples demonstrating various programming concepts such as creating a menu bar, handling user-defined exceptions, type casting, using vectors, action listeners, result sets, constructors, grid layouts, threading, JTree, switch cases, summing digits, and URL handling. Each example is self-contained and illustrates a specific feature or functionality within Java. The code snippets are designed for educational purposes, showcasing fundamental programming techniques.

Uploaded by

sajansteve33
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/ 5

1.

Menu Bar

import javax.swing.*;

public class MenuBarExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Menu Bar Example");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem item = new JMenuItem("Open");

menu.add(item);
menuBar.add(menu);
frame.setJMenuBar(menuBar);

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

2. User Defined Exception (Age < 18)

class UnderAgeException extends Exception {


UnderAgeException(String message) {
super(message);
}
}

public class AgeCheck {


public static void main(String[] args) {
int age = 16;
try {
if (age < 18)
throw new UnderAgeException("Age is less than 18");
else
System.out.println("Eligible");
} catch (UnderAgeException e) {
System.out.println(e.getMessage());
}
}
}

3. Type Casting

public class TypeCastingExample {


public static void main(String[] args) {
int a = 10;
double b = a;
double x = 9.5;
int y = (int)x;
System.out.println("b = " + b);
System.out.println("y = " + y);
}
}

4. Vector Methods

import java.util.*;

public class VectorExample {


public static void main(String[] args) {
Vector<String> v = new Vector<>();
v.add("A");
v.add("B");
v.add("C");
v.remove("B");
System.out.println(v);
System.out.println("Size: " + v.size());
}
}

5. Action Listener

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

public class ButtonClick {


public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("Click Me");

button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Button Clicked"));

frame.add(button);
frame.setSize(200, 150);
frame.setLayout(null);
button.setBounds(50, 50, 100, 30);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

6. ResultSet

import java.sql.*;

public class ResultSetExample {


public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root",
"pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}

con.close();
}
}

7. Constructor

class Student {
String name;

Student(String n) {
name = n;
}

void show() {
System.out.println("Name: " + name);
}

public static void main(String[] args) {


Student s = new Student("John");
s.show();
}
}

8. Calculator with GridLayout

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

public class GridCalculator {


public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setLayout(new GridLayout(4, 4));
String[] buttons = {"1", "2", "3", "+", "4", "5", "6", "-",
"7", "8", "9", "*", "C", "0", "=", "/"};

for (String text : buttons)


frame.add(new JButton(text));

frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

9. Even & Odd Threads

public class EvenOddThread {


public static void main(String[] args) {
Thread even = new Thread(() -> {
for (int i = 0; i <= 10; i += 2) {
System.out.println("Even: " + i);
try { Thread.sleep(500); } catch (Exception e) {}
}
});

Thread odd = new Thread(() -> {


for (int i = 1; i <= 10; i += 2) {
System.out.println("Odd: " + i);
try { Thread.sleep(500); } catch (Exception e) {}
}
});

even.start();
odd.start();
}
}

10. JTree

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child2");

root.add(child1);
root.add(child2);

JTree tree = new JTree(root);


frame.add(tree);
frame.setSize(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

11. Switch Case

public class SwitchExample {


public static void main(String[] args) {
int day = 2;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
}
}

12. Sum of Digits

public class SumDigits {


public static void main(String[] args) {
int num = 1234, sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum: " + sum);
}
}

13. URL Class

import java.net.*;

public class URLExample {


public static void main(String[] args) throws Exception {
URL url = new URL("https://example.com");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Path: " + url.getPath());
}
}

You might also like