0% found this document useful (0 votes)
2 views5 pages

OOP Unit3 Java Assignment-1

The document contains multiple Java programming examples demonstrating various concepts including access modifiers, exception handling, multithreading, IO streams, file handling, socket programming for client-server applications, a calculator application, GUI development using Swing, and establishing JDBC connections. Each example is presented with code snippets illustrating the implementation of the respective concepts. These examples serve as practical applications of Java programming techniques.
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)
2 views5 pages

OOP Unit3 Java Assignment-1

The document contains multiple Java programming examples demonstrating various concepts including access modifiers, exception handling, multithreading, IO streams, file handling, socket programming for client-server applications, a calculator application, GUI development using Swing, and establishing JDBC connections. Each example is presented with code snippets illustrating the implementation of the respective concepts. These examples serve as practical applications of Java programming techniques.
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

8. Implement the java package with various access modifiers.

// File: mypackage/MyClass.java
package mypackage;

public class MyClass {


public int publicVar = 1;
protected int protectedVar = 2;
int defaultVar = 3; // default
private int privateVar = 4;

public void showAccess() {


System.out.println("Public: " + publicVar);
System.out.println("Protected: " + protectedVar);
System.out.println("Default: " + defaultVar);
System.out.println("Private: " + privateVar);
}
}

// File: TestAccess.java
import mypackage.MyClass;

public class TestAccess {


public static void main(String[] args) {
MyClass obj = new MyClass();
obj.showAccess();
System.out.println("Public Var: " + obj.publicVar);
// System.out.println(obj.protectedVar); // Error if outside package
// System.out.println(obj.defaultVar); // Error
// System.out.println(obj.privateVar); // 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) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("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() {
System.out.println("Thread using Thread class is running");
}
}

// Using Runnable interface


class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread using Runnable interface is running");
}
}

public class MultiThreadDemo {


public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();

Thread t2 = new Thread(new MyRunnable());


t2.start();
}
}

11. Implement the IO streams for java.


import java.io.*;

public class IOExample {


public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("demo.txt");
fos.write("Hello, IO Stream!".getBytes());
fos.close();

FileInputStream fis = new FileInputStream("demo.txt");


int i;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
fis.close();
}
}

12. Creating an empty File and perform various file handling operations.
import java.io.*;

public class FileExample {


public static void main(String[] args) throws IOException {
File file = new File("newfile.txt");

if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
}

FileWriter fw = new FileWriter(file);


fw.write("Writing to the new file.");
fw.close();

BufferedReader br = new BufferedReader(new FileReader(file));


String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}

13. Develop a client server Application using Socket Programming in java.


// Server.java
import java.net.*;
import java.io.*;

public class Server {


public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1234);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
System.out.println("Message: " + str);
ss.close();
}
}

// Client.java
import java.net.*;
import java.io.*;

public class Client {


public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 1234);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Hello Server");
dos.close();
}
}

14. Develop a calculator application in java.


import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
double a = sc.nextDouble();
double b = sc.nextDouble();

System.out.println("Select Operation (+, -, *, /): ");


char op = sc.next().charAt(0);

switch (op) {
case '+': System.out.println("Result: " + (a + b)); break;
case '-': System.out.println("Result: " + (a - b)); break;
case '*': System.out.println("Result: " + (a * b)); break;
case '/': System.out.println("Result: " + (a / b)); break;
default: System.out.println("Invalid operation");
}
sc.close();
}
}

15. Develop GUI applications using Swing components.


import javax.swing.*;

public class MySwingApp {


public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click Me");
b.setBounds(130, 100, 100, 40);

f.add(b);
f.setSize(400, 300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

16. Establish JDBC Connection in Java.


import java.sql.*;

public class JDBCDemo {


public static void main(String[] args) {
try {
// Load driver
Class.forName("com.mysql.cj.jdbc.Driver");

// Connection string
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");

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();
} catch (Exception e) {
System.out.println(e);
}
}
}

You might also like