Java Programming Labs

Master generics for type safety, enums for constants, and regular expressions for pattern matching.

Generics, Enums & Regular Expressions - Module 6

Learn type-safe generics, enum types, and powerful regex pattern matching.

Lab 16: Generics
Advanced
Coding Challenge
Your Task: Create type-safe generic classes and methods to understand how Java generics provide compile-time type checking.

Detailed Requirements:
1. Create a generic class Box<T>:
   • class Box<T> { private T value; }
   • Constructor that accepts T
   • Getter method: T getValue()
   • Setter method: void setValue(T value)

2. Create a generic method:
   • public static <T> void printArray(T[] array)
   • Prints all elements of any type array

3. Use bounded type parameters:
   • <T extends Number> - accepts Number or subclasses
   • Create method to sum numeric values

4. Demonstrate usage with String, Integer, Double types.

Key Concepts:
• Generics provide compile-time type safety
• <T> is a type parameter (placeholder)
• Bounded types restrict what types can be used
• Generic methods can have their own type parameters

Expected Output:
String Box: Hello Generics! Integer Box: 42 Double Box: 3.14 Array: 1 2 3 4 5 Array: A B C D Sum of numbers: 15.5

Requirements Checklist

Create generic class with <T>
Implement getValue() and setValue()
Create generic method with <T>
Use bounded type <T extends Number>
Instantiate Box with different types
Call generic methods with arrays
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Generic class: class Box<T> { T value; }
• Generic method: public static <T> void method(T param)
• Bounded: <T extends Number> limits to Number subclasses
• Instantiate: Box<String> box = new Box<>("text");
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 17: Enums (Enumerated Types)
Intermediate
Coding Challenge
Your Task: Create and use enum types to represent fixed sets of constants with type safety and additional functionality.

Detailed Requirements:
1. Create a simple enum:
   enum Day { MONDAY, TUESDAY, WEDNESDAY, ... }

2. Create an enum with fields and constructor:
   enum Planet { MERCURY(3.303e+23, 2.4397e6), ... }
   • Add private fields (mass, radius)
   • Add constructor
   • Add getter methods
   • Add a method to calculate surface gravity

3. Use enum methods:
   • values() - returns array of all enum constants
   • valueOf("NAME") - converts string to enum
   • ordinal() - returns position (0-based)
   • name() - returns name as string

4. Use enum in switch statement.

Expected Output:
Today is: WEDNESDAY Day number: 3 Is weekend? false All days: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Planet: EARTH Mass: 5.976E24 kg Radius: 6.378E6 m Surface Gravity: 9.80 m/s²

Requirements Checklist

Create simple enum with constants
Create enum with fields and constructor
Add getter methods to enum
Use values() to iterate all constants
Use ordinal() or name() method
Use enum in switch statement
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Simple enum: enum Color { RED, GREEN, BLUE }
• Enum with data: enum Size { SMALL(1), MEDIUM(2); int val; Size(int v){val=v;} }
• Get all values: MyEnum.values()
• Position: myEnum.ordinal() (0-based index)
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 18: Regular Expressions (Regex)
Expert
Coding Challenge
Your Task: Master regular expressions in Java for powerful pattern matching, validation, and text manipulation.

Detailed Requirements:
1. Import regex classes:
   import java.util.regex.*;

2. Use Pattern and Matcher:
   • Pattern pattern = Pattern.compile("regex");
   • Matcher matcher = pattern.matcher(input);
   • matcher.find() - find next match
   • matcher.matches() - match entire string
   • matcher.group() - get matched text

3. Common regex patterns:
   • Email: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}
   • Phone: \\d{3}-\\d{3}-\\d{4}
   • Digits: \\d+

4. String methods with regex:
   • str.matches(regex)
   • str.replaceAll(regex, replacement)
   • str.split(regex)

Expected Output:
Email Validation: test@example.com: valid invalid.email: invalid user.name@domain.org: valid Phone numbers found: 555-123-4567 555-987-6543 Numbers extracted: 42 100 7 Censored: Hello *****, how are you *****?

Requirements Checklist

Import java.util.regex.*
Create Pattern with compile()
Use Matcher with find() or matches()
Extract matches with group()
Use String.matches() for validation
Use replaceAll() with regex
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Pattern: Pattern p = Pattern.compile("\\\\d+");
• Matcher: Matcher m = p.matcher(text);
• Find all: while (m.find()) { m.group(); }
• Validate: str.matches("regex") - checks entire string
• Escape backslash: use \\\\d in Java strings for \\d
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below