Lab Assignments – VI
[Link].(CSE) Semester II
Object-oriented Programming using Java Lab (CS1205)
1. Create an interface Validator with:
• an abstract method boolean validate(String input)
• a default method printResult(String input) that prints
• "Valid input" or "Invalid input" based on return value of
validate()
• a private helper method isEmpty(String input) used inside printResult()
• a static factory method getValidator(String type) that returns:
• EmailValidator if type is "email"
• PhoneValidator if type is "phone"
Create both implementing classes.
Neither class should override printResult().
In main, invoke both validators using the factory method and test them.
2. Create two interfaces Printable and Loggable. Both should contain:
• a default method show() that prints different messages.
Create a class Report that implements both interfaces.
Compilation should fail. Why?
Now:
1. Resolve the conflict by overriding show()
2. Inside overridden show(), call:
• [Link]()
• [Link]()
Observe and report the output.
3. Create a functional interface Operation with:
• one abstract method int apply(int a, int b)
• one default method displayResult(int a, int b)
• one static factory method getOperation(String op)
The factory method returns different lambda implementations:
• "add" → addition
• "mul" → multiplication
• "max" → maximum of two numbers
Use the factory method and lambdas to perform all operations.
4. Create interface Device with methods:
• start()
• stop()
Create interface SmartDevice that extends Device and adds:
• default method connect()
Create class SmartFan that implements SmartDevice but implements only start().
Show that compilation fails. Explain why.
Then fix the class correctly.
5. Create interface Payment with:
• method pay(double amount)
• default method printReceipt(double amount)
Create classes:
• UPIPayment
• CardPayment
• CashPayment
Create a PaymentFactory class with a static method:
public static Payment getPaymentMode(String mode)
Return appropriate object based on input.
Use Dynamic Method Dispatch to:
• invoke pay()
• invoke printReceipt()
without knowing the actual class type.