8. Implement the java package with various access modifiers.
// File: mypackage/[Link]
package mypackage;
public class MyClass {
public int publicVar = 1;
protected int protectedVar = 2;
int defaultVar = 3; // default
private int privateVar = 4;
public void showAccess() {
[Link]("Public: " + publicVar);
[Link]("Protected: " + protectedVar);
[Link]("Default: " + defaultVar);
[Link]("Private: " + privateVar);
}
}
// File: [Link]
import [Link];
public class TestAccess {
public static void main(String[] args) {
MyClass obj = new MyClass();
[Link]();
[Link]("Public Var: " + [Link]);
// [Link]([Link]); // Error if outside package
// [Link]([Link]); // Error
// [Link]([Link]); // Error
}
}
9. Implement the exception handling techniques.
public class ExceptionExample {
public static void main(String[] args) {
try {
int a = 5, b = 0;
int result = a / b; // ArithmeticException
} catch (ArithmeticException e) {
[Link]("Error: " + [Link]());
} finally {
[Link]("Always runs.");
}
}
}
10. Write a java program to achieve multithreading by using Thread class and Runnable
interface.
// Using Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread using Thread class is running");
}
}
// Using Runnable interface
class MyRunnable implements Runnable {
public void run() {
[Link]("Thread using Runnable interface is running");
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
Thread t2 = new Thread(new MyRunnable());
[Link]();
}
}
11. Implement the IO streams for java.
import [Link].*;
public class IOExample {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("[Link]");
[Link]("Hello, IO Stream!".getBytes());
[Link]();
FileInputStream fis = new FileInputStream("[Link]");
int i;
while ((i = [Link]()) != -1) {
[Link]((char) i);
}
[Link]();
}
}
12. Creating an empty File and perform various file handling operations.
import [Link].*;
public class FileExample {
public static void main(String[] args) throws IOException {
File file = new File("[Link]");
if ([Link]()) {
[Link]("File created: " + [Link]());
}
FileWriter fw = new FileWriter(file);
[Link]("Writing to the new file.");
[Link]();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
}
}
13. Develop a client server Application using Socket Programming in java.
// [Link]
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1234);
Socket s = [Link]();
DataInputStream dis = new DataInputStream([Link]());
String str = [Link]();
[Link]("Message: " + str);
[Link]();
}
}
// [Link]
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 1234);
DataOutputStream dos = new DataOutputStream([Link]());
[Link]("Hello Server");
[Link]();
}
}
14. Develop a calculator application in java.
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers: ");
double a = [Link]();
double b = [Link]();
[Link]("Select Operation (+, -, *, /): ");
char op = [Link]().charAt(0);
switch (op) {
case '+': [Link]("Result: " + (a + b)); break;
case '-': [Link]("Result: " + (a - b)); break;
case '*': [Link]("Result: " + (a * b)); break;
case '/': [Link]("Result: " + (a / b)); break;
default: [Link]("Invalid operation");
}
[Link]();
}
}
15. Develop GUI applications using Swing components.
import [Link].*;
public class MySwingApp {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click Me");
[Link](130, 100, 100, 40);
[Link](b);
[Link](400, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
16. Establish JDBC Connection in Java.
import [Link].*;
public class JDBCDemo {
public static void main(String[] args) {
try {
// Load driver
[Link]("[Link]");
// Connection string
Connection con = [Link](
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM students");
while ([Link]()) {
[Link]([Link](1) + " " + [Link](2));
}
[Link]();
} catch (Exception e) {
[Link](e);
}
}
}