Java Codes By Rajan Sir
1. Constructor and Methods
class Demo {
int num;
Demo() {
num = 10;
}
void display()
{ [Link]("Number: " + num);
}
public static void main(String[] args)
{ Demo obj = new Demo(); [Link]();
}
}
2. Constructor Overloading
class Demo {
int num;
Demo() {
num = 10;
}
Demo(int n)
{ num = n;
}
void display()
{ [Link]("Number: " +
num);
}
public static void main(String[] args)
{ Demo obj1 = new Demo();
Demo obj2 = new Demo(20);
[Link]();
[Link]();}}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
3. Method Overloading
class Demo {
void show(int a)
{ [Link]("Integer: " + a);
}
void show(int a, int b)
{ [Link]("Sum: " + (a +
b));
}
public static void main(String[] args)
{ Demo obj = new Demo();
[Link](10);
[Link](10, 20);
}
}
4. Method Overriding
class Parent
{ void
show() {
[Link]("Parent class method");
}
}
class Child extends Parent
{ @Override
void show() {
[Link]("Child class method");
}
public static void main(String[] args)
{ Parent obj = new Child();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](); }
}
5. Command Line Arguments
class CommandLineDemo {
public static void main(String[] args)
{ if ([Link] > 0) {
[Link]("Command Line Argument: " + args[0]);
} else {
[Link]("No arguments provided!");
}
}
}
6. Logical Operators
class LogicalOperators {
public static void main(String[] args)
{ boolean a = true, b = false;
[Link]("AND (a && b): " + (a && b));
[Link]("OR (a || b): " + (a || b));
[Link]("NOT (!a): " + (!a));
}
}
7. Looping Statements
a) For Loop
class ForLoop {
public static void main(String[] args)
{ for (int i = 1; i <= 5; i++) {
[Link]("Iteration: " + i);}}}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
b) While Loop
class WhileLoop {
public static void main(String[] args)
{ int i = 1;
while (i <= 5)
{ [Link]("Iteration: " +
i); i++;
}
}
}
c) Do-While Loop
class DoWhileLoop {
public static void main(String[] args)
{ int i = 1;
do {
[Link]("Iteration: " + i);
i++;
} while (i <= 5);
}
8. Decision-Making Statements
If-Else Statement
class IfElseDemo {
public static void main(String[] args)
{ int num = 10;
if (num > 0)
{ [Link]("Positive
Number");
} else {
[Link]("Negative Number");
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
}
}
}
Switch Statement
class SwitchDemo {
public static void main(String[] args)
{ int day = 2;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
default:
[Link]("Other Day");
}
}
}
9. Single Inheritance
class Parent {
void display() {
[Link]("This is Parent class");
}
}
class Child extends Parent {
public static void main(String[] args)
{ Child obj = new Child();
[Link]();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
}
}
10. Multilevel Inheritance
class Grandparent {
void show()
{ [Link]("Grandparent class");
}
}
class Parent extends Grandparent
{ void display() {
[Link]("Parent class");
}
}
class Child extends Parent {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
[Link]();
}
}
11. Hierarchical Inheritance
class Parent {
void show()
{ [Link]("Parent class");
}
}
class Child1 extends Parent
{ void child1Method() {
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link]("Child1 class");
}
}
class Child2 extends Parent
{ void child2Method() {
[Link]("Child2 class");
}}
public class Hierarchical {
public static void main(String[] args)
{ Child1 obj1 = new Child1();
[Link]();
obj1.child1Method();
Child2 obj2 = new Child2();
[Link]();
obj2.child2Method();
}}
12. Multiple Inheritance (Using Interface)
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B
{ public void methodA() {
[Link]("Method from Interface A");
}
public void methodB() {
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link]("Method from Interface B");
}
public static void main(String[] args)
{ C obj = new C();
[Link]();
[Link]();
}}
13. Hybrid Inheritance (Combination of Multiple and Hierarchical, achieved
using Interfaces)
interface A {
void methodA();
}
interface B extends A {
void methodB();
}
class C implements A
{ public void methodA() {
[Link]("Method from Interface A");
}
}
class D extends C implements B
{ public void methodB() {
[Link]("Method from Interface B");
}
public static void main(String[] args)
{ D obj = new D();
[Link]();
[Link]();
}
}
14. Try catch and finally
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
class TryCatchFinallyDemo {
public static void main(String[] args)
{ try {
int a = 10, b = 0;
int result = a / b;
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Exception Caught: Division by zero is not allowed.");
} finally {
[Link]("Finally block executed.");
}}}
15. Creating thread using Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread running using Thread class");
}
public static void main(String[] args)
{ MyThread t1 = new MyThread();
[Link]();
}}
16. Creating thread using runnable interface
class MyRunnable implements Runnable
public void run() {
[Link]("Thread running using Runnable interface");
}
public static void main(String[] args)
{ MyRunnable obj = new MyRunnable();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
Thread t2 = new Thread(obj);
[Link]();
}
}
17. this keyword
class Demo {
int num;
Demo(int num) {
[Link] = num;
}
void display() {
[Link]("Number: " + [Link]);
}
public static void main(String[] args)
{ Demo obj = new Demo(10);
[Link]();
}
}
[Link] operator
class Demo {
int num = 10;
void show() {
[Link]("Using dot operator: " + num);
}
public static void main(String[] args)
{ Demo obj = new Demo();
[Link](); }
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
}
[Link] of operator
class Parent {}
class Child extends Parent {
public static void main(String[] args)
{ Child obj = new Child();
[Link]("Is obj an instance of Child? " + (obj instanceof Child));
[Link]("Is obj an instance of Parent? " + (obj instanceof
Parent));
}
}
[Link] of StringBuffer
class StringBufferDemo {
public static void main(String[] args)
{ StringBuffer sb = new StringBuffer("Hello");
[Link](" World");
[Link](sb);
[Link](5, " Java");
[Link](sb);
[Link](6, 10, "C++");
[Link](sb);
[Link](6, 10);
[Link](sb);
[Link]();
[Link](sb);
}
}
21. Vector Class
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
import [Link];
class VectorDemo {
public static void main(String[] args)
{ Vector<Integer> vec = new Vector<>();
[Link](10);
[Link](20);
[Link](30);
[Link]("Vector elements: " + vec);
}
}
22. Wrapper Classes
class WrapperDemo {
public static void main(String[] args)
{ int num = 10;
Integer obj = [Link](num);
int val = [Link]();
[Link]("Boxed: " + obj);
[Link]("Unboxed: " + val);
}
}
[Link] Collection
public class GC {
protected void finalize() throws Throwable
{ [Link]("Garbage collector called. Object is deleted.");
}
public static void main(String[] args) {
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
GC a = new GC();
GC b = new GC();
a = null;
b = null;
[Link]();
}}
[Link] Program to Demonstrate Declaration and Importing of User-Defined
Packages.
Step 1. Declare package
// File: [Link]
package myPackage; // Declaring the package
public class MyClass {
public void displayMessage() {
[Link]("Hello from MyClass in myPackage!");
}
}
Step 2. Import and Use the User-Defined Package
// File: [Link]
import [Link]; // Importing the class from user-defined package
public class Main {
public static void main(String[] args) {
MyClass myObject = new MyClass(); // Creating an object of MyClass
[Link](); // Calling the method
}
}
25. Final variable and method.
class A {
final int SIZE = 100; // Final variable
final void meth() { // Final method
[Link]("This is a final method.");
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link]("Final variable SIZE = " + SIZE)
}}
class B extends A {
// This will cause an error because final methods cannot be overridden
/*
void meth()
{ [Link]("Illegal!");
}
}
class Test {
public static void main(String[] args) {
B obj = new B();
[Link]();
}
}
26. Super keyword
class Person {
void message() {
[Link]("This is person class");
}
}
class Student extends Person
{ int mobileno;
void message() {
[Link]("This is student class");
}
void display()
{ message();
[Link]();
}
}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
class Test {
public static void main(String args[])
{ Student s = new Student();
[Link]();
}
}
[Link] a simple program to create a GUI application using Frame class.
import [Link].*;
import [Link].*;
public class SimpleFrameExample
{ public static void main(String[] args)
{
Frame f = new Frame("Simple Frame Example");
Button b = new Button("Click Me");
[Link](100, 100, 100, 50);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e)
{ [Link]("Button was clicked!");
}
});
[Link](b);
[Link](null);
[Link](300, 200);
[Link](true);
[Link](new WindowAdapter()
{ public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
28. Write a program to use class CheckBoxGroup to create Radiobutton
import [Link].*;
public class RadioButtonExample
{ public static void main(String[] args)
{
Frame frame = new Frame("Radio Button Example");
CheckboxGroup group = new CheckboxGroup();
Checkbox rb1 = new Checkbox("Option 1", group, true);
Checkbox rb2 = new Checkbox("Option 2", group, false);
[Link](50, 80, 100, 30);
[Link](50, 120, 100, 30);
[Link](rb1);
[Link](rb2);
[Link](200, 200);
[Link](null);
[Link](true);
[Link](new [Link]()
{ public void windowClosing([Link] e) {
[Link](0);
}
});
}
}
[Link] a program to demonstrate constructors of AWT class TextField and
TextArea.
import [Link].*;
public class TextFieldTextAreaExample
{ public static void main(String[] args) {
Frame frame = new Frame("AWT TextField and TextArea Example");
TextField tf1 = new TextField();
TextField tf2 = new TextField(20);
TextField tf3 = new TextField("Hello, World!");
[Link](50, 50, 200, 30);
[Link](50, 100, 200, 30);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](50, 150, 200, 30);
TextArea ta1 = new TextArea();
TextArea ta2 = new TextArea("Initial Text", 5, 20);
[Link](50, 200, 200, 100);
[Link](50, 320, 200, 100);
[Link](tf1);
[Link](tf2);
[Link](tf3);
[Link](ta1);
[Link](ta2);
[Link](300, 500);
[Link](null);
[Link](true);
[Link](new [Link]()
{ public void windowClosing([Link] e) {
[Link](0);
}
});
}
}
30. Write a program to demonstrate BorderLayout.
import [Link].*;
public class BorderLayoutExample
{ public static void main(String[] args)
{
Frame frame = new Frame("BorderLayout Example");
[Link](new BorderLayout());
[Link](new Button("North"), [Link]);
[Link](new Button("South"), [Link]);
[Link](new Button("East"), [Link]);
[Link](new Button("West"), [Link]);
[Link](new Button("Center"), [Link]);
[Link](300, 200);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](true);
[Link](new [Link]()
{ public void windowClosing([Link] e) {
[Link](0);
}
});
}
}
31. Write a program to demonstrate GridLayout.
import [Link].*;
public class GridLayoutExample
{ public static void main(String[]
args) {
Frame frame = new Frame("GridLayout Example");
[Link](new GridLayout(3, 2));
[Link](new Button("Button 1"));
[Link](new Button("Button 2"));
[Link](new Button("Button 3"));
[Link](new Button("Button 4"));
[Link](new Button("Button 5"));
[Link](new Button("Button 6"));
[Link](300, 200);
[Link](true);
[Link](new [Link]()
{ public void windowClosing([Link] e) {
[Link](0);
}
});
}
}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
32. Write a program to demonstrate swing class JComboBox
import [Link].*;
import [Link].*;
public class JComboBoxExample
{ public static void main(String[] args)
{
JFrame frame = new JFrame("JComboBox Example");
String[] items = {"Apple", "Banana", "Cherry", "Date", "Grape"};
JComboBox<String> comboBox = new JComboBox<>(items);
[Link](0);
JLabel label = new JLabel("Selected Item: " +
[Link]());
[Link](e ->
[Link]("Selected Item: " + [Link]())
);
[Link](new FlowLayout());
[Link](comboBox);
[Link](label);
[Link](300, 150);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
33. JRadioButton Example Using Swing
import [Link].*;
import [Link].*;
public class JRadioButtonExample
{ public static void main(String[] args)
{
JFrame frame = new JFrame("JRadioButton Example");
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");
JRadioButton radioButton3 = new JRadioButton("Option 3", true);
ButtonGroup group = new ButtonGroup();[Link](radioButton1);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](radioButton2);
[Link](radioButton3);
JLabel label = new JLabel("Selected Option: Option 3");
[Link](e -> [Link]("Selected Option:
Option 1"));
[Link](e -> [Link]("Selected Option:
Option 2"));
[Link](e -> [Link]("Selected Option:
Option 3"));
[Link](new FlowLayout());
[Link](radioButton1);
[Link](radioButton2);
[Link](radioButton3);
[Link](label);
[Link](300, 150);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
[Link] a Example of use of setToolTipNext() method to set Tooltip text for
swing component.
import [Link].*;
import [Link].*;
public class ToolTipExample {
public static void main(String[] args) {
JFrame frame = new JFrame("ToolTip Example");
JButton button = new JButton("Click Me");
[Link]("This button performs an action when clicked.");
JLabel label = new JLabel("Hover over the button to see the tooltip.");
[Link](new FlowLayout());
[Link](label);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](button);
[Link](300, 150);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
35. Write a program to design a form using the components List and Choice.
import [Link].*;
public class Practical2 {
public static void main(String[] args) {
Frame f = new Frame("Form with List and Choice Components");
[Link](400, 400);
[Link](null);
[Link](true);
Label c = new Label("Select Country");
Choice c1 = new Choice();
[Link]("India");
[Link]("America");
[Link]("England");
[Link]("Russia");
Label w = new Label("Weekdays");
List l = new List(7, true);
[Link]("Sunday");
[Link]("Monday");
[Link]("Tuesday");
[Link]("Wednesday");
[Link]("Thursday");
[Link]("Friday");
[Link]("Saturday");
[Link](100, 100, 100, 20);
[Link](100, 130, 100, 100);
[Link](100, 200, 100, 20);
[Link](100, 250, 100, 100);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](c);
[Link](c1);
[Link](w);
[Link](l);
[Link](new WindowAdapter()
{ public void windowClosing(WindowEvent we) {
[Link](0);
}
});
}
}
[Link] a program using AWT to create a menubar where menubar contains
menu items such as File, Edit, View and create a submenu under the File
menu: New and Open.
import [Link].*;
public class practical5 extends Frame
{ MenuBar mb;
Menu m,m1,m2;
MenuItem mi,mi2;
practical5()
{
Frame f = new Frame();
setVisible(true);
setSize(400,400);
setLayout(null);
MenuBar mb =new MenuBar();
setMenuBar(mb);
Menu m = new Menu("File");
Menu m1 = new Menu("Edit");
Menu m2 = new Menu("View");
MenuItem mi = new MenuItem("New");
MenuItem mi2 = new MenuItem("Open");
[Link](m);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link](m1);
[Link](m2);
[Link](mi);
[Link](mi2);
}
public static void main(String args[])
{
practical5 p = new practical5();
[Link](true);
}
}
37. Write a program to create a JTable.
import [Link].*;
import [Link].*;
public class JTableExample {
JFrame f; JTableExample()
{
String rows[][]={ {"Dnyaneshwari","2001160374","IF5I"},
{"Hariprasad","2001160372","IF5I"},
{"Soham","2001160376","IF5I"},
{"Vaishnavi","2001160368","IF5I"}
};
String col[]={"NAME","ENROLLMENT NO","DEPARTMENT"};
JTable jt = new JTable(rows,col);
[Link](30,40,200,300);
JScrollPane sp = new JScrollPane(jt);
JFrame f = new JFrame();
[Link](sp);
[Link](300,400);
[Link](true);
}
public static void main(String args[])
{
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
new JTableExample();
}
}
[Link] a program to demonstrate the use of JTextField and JPasswordField
using Listener Interface.
import [Link].*;
import [Link].*;
import [Link].*;
public class Pra12_1 extends JFrame implements ActionListener
{ JTextField t;
JPasswordField p;
JButton b;
public Pra12_1() {
JLabel l1 = new JLabel("Username:");
JLabel l2 = new JLabel("Password:");
t = new JTextField(20);
p = new JPasswordField(20);
[Link]('#');
b = new JButton("Login");
[Link](this);
setLayout(new FlowLayout());
add(l1);
add(t);
add(l2);
add(p);
add(b);
setTitle("Login Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ String username = [Link]();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
String password = new String([Link]());
if ([Link]("admin") && [Link]("1234"))
{ [Link](this, "Login Successful!");
} else {
[Link](this, "Invalid Credentials!");
}
}
public static void main(String[] args)
{ new Pra12_1();
}
}
[Link] a program to demonstrate the use of AWT components like Label,
Textfield, TextArea, Button, Checkbox, RadioButton etc.
import [Link].*;
import [Link].*;
public class Practical1 {
public static void main(String args[]) {
Frame f = new Frame("AWT Components Demo");
[Link](500, 600);
[Link](null);
Label n = new Label("Enter your Name:");
TextField t = new TextField();
Label a = new Label("Enter Your Address:");
TextArea ta = new TextArea();
Label g = new Label("Select Gender:");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox c1 = new Checkbox("Male", cbg, true);
Checkbox c2 = new Checkbox("Female", cbg, false);
Label s = new Label("Select Subject:");
Checkbox c3 = new Checkbox("AJP");
Checkbox c4 = new Checkbox("CSS");
Checkbox c5 = new Checkbox("OSY");
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
Button b = new Button("Submit");
[Link](100, 50, 150, 20);
[Link](100, 75, 250, 25);
[Link](100, 110, 150, 20);
[Link](100, 135, 250, 60);
[Link](100, 210, 150, 20);
[Link](100, 235, 100, 20);
[Link](200, 235, 100, 20);
[Link](100, 270, 150, 20);
[Link](100, 295, 100, 20);
[Link](100, 320, 100, 20);
[Link](100, 345, 100, 20);
[Link](100, 390, 100, 30);
[Link](n); [Link](t);
[Link](a); [Link](ta);
[Link](g); [Link](c1); [Link](c2);
[Link](s); [Link](c3); [Link](c4); [Link](c5);
[Link](b);
[Link](new WindowAdapter()
{ public void windowClosing(WindowEvent e) {
[Link]();
}
});
[Link](true);
}
}
[Link] a program to demonstrate the use of InetAddress class and its
factory methods.
package practicals;
import [Link].*;
import [Link].*;
public class inet_address {
public static void main(String[] args) {
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
try {
InetAddress ip = [Link]("[Link]");
[Link]("Host Name:"+[Link]());
[Link]("IP Address:"+[Link]());
} catch (Exception e)
{ [Link](e);
}
}
}
41. Write a program for use of URL class method.
import [Link].*;
public class URLExample {
public static void main(String[] args) throws MalformedURLException
{ URL url = new URL("[Link]
[Link]("Host: " + [Link]());
[Link]("Protocol: " + [Link]());
[Link]("Path: " + [Link]
[Link]("Port: " + [Link]());
}
}
[Link] a program using URL class to retrieve the host,protocol,port and file
of URL [Link]
import [Link].*;
public class URLDetails {
public static void main(String[] args)
{ try {
URL url = new URL("[Link]
[Link]("Protocol: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("File: " + [Link]());
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
} catch (MalformedURLException e)
{ [Link]("Invalid URL: " + [Link]());
}
}
}
[Link] a program using InetAddress class to retrieve IP address of
computer when hostname is entered by the user.
import [Link].*;
import [Link];
public class HostnameToIP {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);
[Link]("Enter hostname: ");
String hostname = [Link]();
try {
InetAddress address = [Link](hostname);
[Link]("IP Address: " + [Link]());
} catch (UnknownHostException e)
{ [Link]("Host not found: " +
[Link]());
} finally
{ [Link]();
}
}
}
44. Write a program for URL Class with constructor.
import [Link];
import [Link];
public class URLExample {
public static void main(String[] args)
{ try { URL url = new
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
URL("[Link]
[Link]("URL: " + [Link]());
[Link]("Protocol: " + [Link]());
[Link]("Authority: " + [Link]());
[Link]("File: " + [Link]());
[Link]("Host: " + [Link]());
[Link]("Path: " + [Link]());
[Link]("Port: " + [Link]());
[Link]("Default Port: " + [Link]());
[Link]("Query: " + [Link]());
[Link]("Reference (Fragment): " + [Link]());
} catch (IOException e)
{ [Link]();
}
}
}
45. Write a program for URL connection class method.
import [Link].*;
import [Link].*;
public class URLConnectionExample
{ public static void main(String[] args) {
try {
URL url = new URL("[Link]
URLConnection urlcon = [Link]();
InputStream stream = [Link]();
int i;
while ((i = [Link]()) != -1)
{ [Link]((char) i);
}
} catch (Exception e)
{ [Link]("Error: " +
e);
}
}}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
[Link] a program using Socket and ServerSocket to create an chat
application.
It consists of two programs:
1. Server Program (Receives and sends messages)
2. Client Program (Connects to the server and communicates)
Server program:
import [Link].*;
import [Link].*;
public class ChatServer {
public static void main(String[] args)
{ try {
ServerSocket serverSocket = new ServerSocket(12345);
[Link]("Server started. Waiting for client...");
Socket socket = [Link]();
[Link]("Client connected!");
BufferedReader input = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](),
true);
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader([Link]));
String message;
while (true) {
message = [Link]();
if ([Link]("exit")) break;
[Link]("Client: " + message);
[Link]("You: ");
[Link]([Link]());
}
[Link]();
[Link]();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
} catch (IOException e)
{ [Link]();
}
}
}
Client program:
import [Link].*;
import [Link].*;
public class ChatClient {
public static void main(String[] args)
{ try {
Socket socket = new Socket("localhost", 12345);
[Link]("Connected to server!");
BufferedReader input = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](),
true);
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader([Link]));
String message;
while (true)
{ [Link]("You: ");
message = [Link]();
[Link](message);
if ([Link]("exit")) break;
[Link]("Server: " + [Link]());
}
[Link]();
} catch (IOException e)
{ [Link]();
}
}}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
47. Write a program to insert a record in table.
import [Link].*;
import [Link].*;
public class ChatClient {
public static void main(String[] args)
{ try {
Socket socket = new Socket("localhost", 12345);
[Link]("Connected to server!");
BufferedReader input = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter output = new PrintWriter([Link](),
true);
BufferedReader consoleInput = new BufferedReader(new
InputStreamReader([Link]));
String message;
while (true)
{ [Link]("You: ");
message = [Link]();
[Link](message);
if ([Link]("exit")) break;
[Link]("Server: " + [Link]());
}
[Link]();
} catch (IOException e)
{ [Link]();
}
}
}
48. Write a program to update record in a database.
import [Link].*;
public class UpdateData {
public static void main(String[] args) {
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "";
try {
[Link]("[Link]");
Connection conn = [Link](url, user,
password);
[Link]("Connected to database!");
String updateSQL = "UPDATE login SET emailid = ? WHERE
emailid = ?";
PreparedStatement pstmt = [Link](updateSQL);
[Link](1, "rajan@[Link]");
[Link](2, "rajan@[Link]");
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Student record updated successfully!");
} else {
[Link]("No matching record found.");
}
[Link]();
[Link]();
} catch (Exception e)
{ [Link]();
}
}
}
49. Write a program to implement delete statement.
import [Link].*;
public class DeleteData {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "";
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
try {
[Link]("[Link]");
Connection conn = [Link](url, user,
password);
String deleteSQL = "DELETE FROM login WHERE username = ?";
PreparedStatement pstmt = [Link](deleteSQL);
[Link](1, "John Doe");
int rowsDeleted = [Link]();
[Link](rowsDeleted > 0 ? "Record deleted successfully!"
: "No matching record found.");
[Link]();
[Link]();
} catch (Exception e)
{ [Link]();
}
}
}
[Link] JDBC program to retrieve data from table using ResultSet
interface.
import [Link].*;
public class RetrieveStudentRecords
{ public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/school"; // Replace 'school'
with your database name
String user = "root";
String password = "";
try {
[Link]("[Link]");
Connection conn = [Link](url, user,
password);
String selectSQL = "SELECT * FROM student";
Statement stmt = [Link]();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
ResultSet rs = [Link](selectSQL);
[Link]("Student Records:");
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
int age = [Link]("age");
String grade = [Link]("grade");
[Link](id + " | " + name + " | " + age + " | " + grade);
}
[Link]();
[Link]();
[Link]();
} catch (Exception e)
{ [Link]();
}
}
}
51. Write a program to use KeyListener to display characters entered by the user
Ans. import [Link].*;
import [Link].*;
public class KeyListenerExample extends Frame implements KeyListener {
Label label;
TextField textField;
public KeyListenerExample() {
setLayout(new FlowLayout());
label = new Label("Entered Characters:");
add(label);
textField = new TextField(20);
[Link](this);
add(textField);
setSize(300, 150);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
setTitle("KeyListener Example");
setVisible(true);
// Close window
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
[Link](0);
}
});
}
public void keyTyped(KeyEvent e) {
char ch = [Link]();
[Link]("Entered Characters: " + [Link]() + ch);
}
public void keyPressed(KeyEvent e) {
// Not used
}
public void keyReleased(KeyEvent e) {
// Not used
}
public static void main(String[] args) {
new KeyListenerExample();
}
}
[Link] a program to accept age from user and throw an exception if age is less
than 18.
Ans. import [Link];
class AgeNotValidException extends Exception {
AgeNotValidException(String message) {
super(message);
}
}
public class AgeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
try {
if (age < 18) {
throw new AgeNotValidException("Age must be at least 18.");
} else {
[Link]("Age is valid.");
}
} catch (AgeNotValidException e) {
[Link]("Exception caught: " + [Link]());
}
[Link]();
}
}
53. Write a program to create two threads. Thread A should print even numbers
from 1 to 50 and thread B should print odd numbers from 1 to 50. Each thread
should relinquish control to the other frequently.
Ans. class EvenThread extends Thread {
public void run() {
for (int i = 2; i <= 50; i += 2) {
[Link]("Even: " + i);
[Link]();
}
}
}
class OddThread extends Thread {
public void run() {
for (int i = 1; i <= 50; i += 2) {
[Link]("Odd: " + i);
[Link]();
}
}
}
public class TwoThreads {
public static void main(String[] args) {
EvenThread t1 = new EvenThread();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
OddThread t2 = new OddThread();
[Link]();
[Link]();
}
}
54. Write a program to read and display content from the URL “http://
[Link]”
Ans. import [Link].*;
import [Link].*;
public class ReadURL {
public static void main(String[] args) {
try {
URL url = new URL("[Link]
BufferedReader reader = new BufferedReader(
new InputStreamReader([Link]())
);
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (Exception e) {
[Link]("Error: " + e);
}
}
}
55. Write a program to list five states using JComboBox. Display selected
item in JTextField
Ans. import [Link].*;
import [Link].*;
public class StateSelector {
public static void main(String[] args) {
JFrame frame = new JFrame("State Selector");
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
String[] states = {"Maharashtra", "Gujarat", "Punjab", "Kerala", "Tamil
Nadu"};
JComboBox<String> comboBox = new JComboBox<>(states);
[Link](50, 50, 150, 30);
JTextField textField = new JTextField();
[Link](50, 100, 150, 30);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String selected = (String) [Link]();
[Link](selected);
}
});
[Link](comboBox);
[Link](textField);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
56. Write a program to use JTextField, JLabel and JButton to add two numbers
and display the output. Use GridLayout
Ans. import [Link].*;
import [Link].*;
import [Link].*;
public class AddNumbersGrid {
public static void main(String[] args) {
JFrame frame = new JFrame("Add Two Numbers");
[Link](new GridLayout(4, 2));
JLabel label1 = new JLabel("Enter First Number:");
JTextField text1 = new JTextField();
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
JLabel label2 = new JLabel("Enter Second Number:");
JTextField text2 = new JTextField();
JButton addButton = new JButton("Add");
JTextField resultField = new JTextField();
[Link](false);
[Link](label1);
[Link](text1);
[Link](label2);
[Link](text2);
[Link](new JLabel(""));
[Link](addButton);
[Link](new JLabel("Result:"));
[Link](resultField);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
int sum = num1 + num2;
[Link]([Link](sum));
} catch (Exception ex) {
[Link]("Invalid Input");
}
}
});
[Link](300, 200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
57. Write a program to use Label, TextField, and Button to create a login form.
When the user clicks the button, a login successful/unsuccessful message should
be displayed
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
Ans. import [Link].*;
import [Link].*;
public class LoginForm extends JFrame implements ActionListener {
JTextField userText;
JPasswordField passText;
JLabel messageLabel;
public LoginForm() {
setLayout(null);
JLabel userLabel = new JLabel("Username:");
[Link](50, 50, 100, 30);
add(userLabel);
userText = new JTextField();
[Link](150, 50, 150, 30);
add(userText);
JLabel passLabel = new JLabel("Password:");
[Link](50, 100, 100, 30);
add(passLabel);
passText = new JPasswordField();
[Link](150, 100, 150, 30);
add(passText);
JButton loginButton = new JButton("Login");
[Link](150, 150, 100, 30);
[Link](this);
add(loginButton);
messageLabel = new JLabel();
[Link](50, 200, 300, 30);
add(messageLabel);
setSize(400, 300);
setVisible(true);
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String username = [Link]();
String password = new String([Link]());
if ([Link]("admin") && [Link]("1234")) {
[Link]("Login Successful!");
} else {
[Link]("Login Unsuccessful. Try again.");
}
}
public static void main(String[] args) {
new LoginForm();
}
}
58. Write a program to display the list of employees of deptno 10 from the table
Employee (eno, ename, salary, deptno). Use PreparedStatement
Ans. import [Link].*;
public class EmployeeList {
public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/yourDB", "root", "password");
String sql = "SELECT eno, ename, salary, deptno FROM employee
WHERE deptno = ?";
PreparedStatement ps = [Link](sql);
[Link](1, 10);
ResultSet rs = [Link]();
while ([Link]()) {
[Link]([Link]("eno") + " "
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |
Java Codes By Rajan Sir
+ [Link]("ename") + " "
+ [Link]("salary") + " "
+ [Link]("deptno"));
}
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]("Error: " + e);
}
}
}
Contact No : 9326050669 / 9326881428 | Youtube : @v2vedtechllp | Instagram : v2vedtech
| Download Application | Join Whatsapp Group |