Java Programming Labs

Master networking with sockets, Java modules system, and modern features like records and sealed classes.

Networking, Modules & Modern Features - Module 10

Learn advanced networking, modular architecture, and Java 14+ features.

Lab 28: Networking with Sockets
Expert
Coding Challenge
Your Task: Learn Java networking fundamentals using Socket and ServerSocket for client-server communication.

Detailed Requirements:
1. URL and HTTP connections:
   • URL url = new URL("http://...");
   • HttpURLConnection conn = (HttpURLConnection) url.openConnection();

2. Server Socket:
   • ServerSocket server = new ServerSocket(port);
   • Socket client = server.accept();

3. Client Socket:
   • Socket socket = new Socket(host, port);
   • Use InputStream/OutputStream for communication

4. BufferedReader/PrintWriter for text communication

Expected Output:
--- URL Connection Demo --- Connecting to: https://api.example.com Response Code: 200 Content Type: application/json --- Socket Concepts --- Server would listen on port: 8080 Client would connect to: localhost:8080 --- InetAddress Demo --- Localhost: 127.0.0.1 Hostname: localhost

Requirements Checklist

Create URL object
Use HttpURLConnection or URLConnection
Use ServerSocket class
Use Socket class for client
Use InetAddress for IP operations
Handle IOException properly
Output
// Click "Run Code" to compile and execute
Hints & Tips
• URL: new URL("http://example.com")
• HTTP: (HttpURLConnection) url.openConnection()
• Server: new ServerSocket(port)
• Client: new Socket(host, port)
• IP: InetAddress.getLocalHost()
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 29: Java Module System
Expert
Coding Challenge
Your Task: Learn the Java Platform Module System (JPMS) introduced in Java 9 for better encapsulation and dependency management.

Detailed Requirements:
1. Module declaration (module-info.java):
   • module com.myapp { }

2. Export packages:
   • exports com.myapp.api;
   • exports com.myapp.util to com.other;

3. Require dependencies:
   • requires java.sql;
   • requires transitive java.logging;

4. Services:
   • provides MyService with MyServiceImpl;
   • uses MyService;

Expected Output:
--- Java Module System Demo --- Module Declaration Example: module com.myapp { requires java.base; requires java.sql; requires transitive java.logging; exports com.myapp.api; exports com.myapp.util to com.other; opens com.myapp.internal to com.framework; provides com.myapp.spi.Service with com.myapp.impl.ServiceImpl; uses com.myapp.spi.Plugin; } Current Module: unnamed module

Requirements Checklist

Declare module with 'module' keyword
Use 'requires' for dependencies
Use 'exports' to expose packages
Use 'opens' for reflection access
Use 'provides...with' for services
Use 'uses' to consume services
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Declare: module com.myapp { }
• Depend: requires java.sql;
• Export: exports com.myapp.api;
• Reflect: opens com.myapp to framework;
• Service: provides X with Y;
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 30: Records & Sealed Classes
Expert
Coding Challenge
Your Task: Master modern Java features: Records (Java 14+) for immutable data and Sealed Classes (Java 17+) for restricted inheritance.

Detailed Requirements:
1. Records (immutable data carriers):
   • record Person(String name, int age) { }
   • Auto-generates constructor, getters, equals, hashCode, toString
   • Can add compact constructors and methods

2. Sealed Classes (restricted inheritance):
   • sealed class Shape permits Circle, Rectangle { }
   • Subclasses must be: final, sealed, or non-sealed

3. Pattern matching with sealed classes:
   • Exhaustive switch expressions

Expected Output:
--- Records Demo --- Person: Person[name=Alice, age=30] Name: Alice, Age: 30 Equals: true HashCode match: true --- Sealed Classes Demo --- Circle area: 78.54 Rectangle area: 24.00 Triangle area: 6.00 --- Pattern Matching --- Processing Circle with radius 5.0 Processing Rectangle 4.0 x 6.0 Processing Triangle with base 3.0

Requirements Checklist

Create a record with 'record' keyword
Access record components (auto-getters)
Create sealed class with 'permits'
Use 'final' on permitted subclass
Use 'non-sealed' on permitted subclass
Use pattern matching with instanceof or switch
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Record: record Point(int x, int y) { }
• Access: point.x() not point.getX()
• Sealed: sealed class X permits A, B { }
• Subclass: final class A extends X { }
• Pattern: if (obj instanceof Type t) { use t }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below