Java Programming Labs

Master unit testing with JUnit, object serialization, and essential design patterns.

Unit Testing, Serialization & Design Patterns - Module 9

Learn professional Java development practices and architectural patterns.

Lab 25: Unit Testing with JUnit
Expert
Coding Challenge
Your Task: Write unit tests using JUnit 5 annotations and assertions to test a Calculator class.

Detailed Requirements:
1. Test annotations:
   • @Test - marks method as test
   • @BeforeEach - setup before each test
   • @AfterEach - cleanup after each test
   • @DisplayName - custom test name

2. Assertions:
   • assertEquals(expected, actual)
   • assertTrue(condition)
   • assertThrows(Exception.class, () -> ...)

3. Test a Calculator class with add, subtract, multiply, divide methods

Expected Output:
Running JUnit Tests... ✓ testAddition - PASSED ✓ testSubtraction - PASSED ✓ testMultiplication - PASSED ✓ testDivision - PASSED ✓ testDivisionByZero - PASSED Tests run: 5, Passed: 5, Failed: 0

Requirements Checklist

Use @Test annotation
Use @BeforeEach or @BeforeAll
Use assertEquals() assertion
Use assertTrue() or assertFalse()
Use assertThrows() for exceptions
Use @DisplayName annotation
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Test method: @Test void testMethod() { ... }
• Setup: @BeforeEach void setUp() { ... }
• Assert: assertEquals(expected, actual)
• Exception: assertThrows(Ex.class, () -> code)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 26: Object Serialization
Advanced
Coding Challenge
Your Task: Implement Java serialization to save and restore objects to/from files.

Detailed Requirements:
1. Implement Serializable:
   • class Person implements Serializable
   • Add serialVersionUID field

2. Serialize (write) object:
   • ObjectOutputStream oos = new ObjectOutputStream(fos);
   • oos.writeObject(object);

3. Deserialize (read) object:
   • ObjectInputStream ois = new ObjectInputStream(fis);
   • Object obj = ois.readObject();

4. Use transient keyword for fields that shouldn't be serialized

Expected Output:
--- Serialization Demo --- Original: Person{name='Alice', age=30, password='[HIDDEN]'} Serializing to file... Object serialized successfully! Deserializing from file... Restored: Person{name='Alice', age=30, password='null'} Note: password is null (transient field)

Requirements Checklist

Implement Serializable interface
Add serialVersionUID field
Use transient keyword
Use ObjectOutputStream to serialize
Use ObjectInputStream to deserialize
Use writeObject() and readObject()
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Interface: class X implements Serializable
• Version: private static final long serialVersionUID = 1L;
• Skip field: private transient String secret;
• Write: oos.writeObject(obj);
• Read: (Type) ois.readObject();
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 27: Design Patterns
Expert
Coding Challenge
Your Task: Implement common design patterns: Singleton, Factory, and Builder.

Detailed Requirements:
1. Singleton Pattern:
   • Private constructor
   • Private static instance
   • Public static getInstance() method

2. Factory Pattern:
   • Factory class creates objects
   • Returns interface/abstract type
   • createProduct(String type) method

3. Builder Pattern:
   • Fluent interface with method chaining
   • return this; for each setter
   • build() method returns final object

Expected Output:
--- Singleton Pattern --- Database instance 1: DB@123 Database instance 2: DB@123 Same instance? true --- Factory Pattern --- Dog says: Woof! Cat says: Meow! --- Builder Pattern --- User{name='John', email='john@example.com', age=25}

Requirements Checklist

Implement Singleton with private constructor
Add getInstance() method for Singleton
Create Factory class with create method
Factory returns interface type
Implement Builder with fluent setters
Builder has build() method
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Singleton: private static instance; public static getInstance()
• Factory: static Animal create(String type)
• Builder: setX(val) { this.x = val; return this; }
• Build: public User build() { return new User(this); }
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below