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

Java_Final_Exam_Notes_with_Code_Samples_Final

The document provides an overview of essential Java concepts including project folder structure, collection classes, threading, database connectivity, and WebSocket communication. It outlines the purpose of key files in a Java project and demonstrates the use of various Java features with code samples. Each section is designed to help understand practical applications of Java in real-world scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java_Final_Exam_Notes_with_Code_Samples_Final

The document provides an overview of essential Java concepts including project folder structure, collection classes, threading, database connectivity, and WebSocket communication. It outlines the purpose of key files in a Java project and demonstrates the use of various Java features with code samples. Each section is designed to help understand practical applications of Java in real-world scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Final Exam Notes with Code Samples

1. Explain the Project's Folder Structure

Understand where to store and find specific files in a Java project:


- pom.xml: Contains project configurations, dependencies, and plugins for Maven projects.
- .gitignore: Specifies files and directories to exclude from version control.
- src/main/java/../controller: Contains classes responsible for handling requests and
directing application flow.
- src/main/java/../model: Stores classes representing the application's data structure or
logic.
- src/main/java/shared: Holds shared utilities or helper classes used across the project.
- src/main/resources: Keeps non-code resources like configuration files and templates.
- src/main/webapp: Contains the frontend resources such as HTML, CSS, JavaScript, and JSP
files.
- src/main/webapp/WEB-INF: Stores configuration files and server-side components like
web.xml.
- src/test: Holds test classes to ensure code reliability.

2. Demonstrate the Use of Collection Classes and Methods

Java Collections Framework provides reusable data structures:


- Collections: Utility class with static methods for operations like sorting and searching.
- List: An ordered collection allowing duplicates (e.g., ArrayList).
- ArrayList: Resizable array implementation of List.
- Set: A collection that does not allow duplicate elements.
- HashSet: A hash table-based implementation of Set.

Example:

import java.util.*;

public class CollectionsExample {


public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Apple"); // Duplicates allowed in List
System.out.println("List: " + list);
Set<String> set = new HashSet<>(list);
System.out.println("Set: " + set); // Removes duplicates
}
}

3. Create and Use Threads

Steps to work with Threads:


- Runnable: Implement to define the task.
- Thread: Instantiate and start the thread.
- synchronized keyword: Ensures thread-safe access to resources.

Example:

class MyRunnable implements Runnable {


public void run() {
System.out.println("Thread is running.");
}
}

public class ThreadExample {


public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new MyRunnable());
thread.start();
thread.join(); // Wait for the thread to finish
}
}

4. Connect to and Query Databases

Classes and methods needed:


- Connection: Establishes a database connection.
- AutoCloseable and try-with-resources: Ensures connections are closed automatically.
- CallableStatement: Executes stored procedures using parameter indexes.
- ResultSet: Retrieves query results.

Example:
import java.sql.*;

public class DatabaseExample {


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/school_db";
String user = "root";
String password = "password";

try (Connection connection = DriverManager.getConnection(url, user, password)) {


CallableStatement stmt = connection.prepareCall("{CALL GetStudents()}");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

5. Streams and Sockets

For communication with files and sockets:


- ServerEndpoint: Requires JSON, Encoder, and Decoder for WebSocket communication.
- JavaScript WebSocket: Communicates with the server via WebSockets.

Example for WebSocket:

// Java WebSocket
@ServerEndpoint(value = "/chat")
public class ChatServer {
@OnMessage
public void onMessage(String message, Session session) throws IOException {
session.getBasicRemote().sendText("Echo: " + message);
}
}

// JavaScript WebSocket Client


const socket = new WebSocket("ws://localhost:8080/chat");
socket.onmessage = (event) => console.log(event.data);

You might also like