0% found this document useful (0 votes)
2 views2 pages

Assignmentvi Nitjsr Java

The document outlines a series of lab assignments for a B.Tech. course in Object-oriented Programming using Java. It includes tasks such as creating interfaces and implementing classes, resolving method conflicts, using functional interfaces with lambda expressions, and demonstrating dynamic method dispatch. Each task requires students to implement specific methods and handle compilation issues while adhering to Java programming principles.

Uploaded by

ncsupply007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Assignmentvi Nitjsr Java

The document outlines a series of lab assignments for a B.Tech. course in Object-oriented Programming using Java. It includes tasks such as creating interfaces and implementing classes, resolving method conflicts, using functional interfaces with lambda expressions, and demonstrating dynamic method dispatch. Each task requires students to implement specific methods and handle compilation issues while adhering to Java programming principles.

Uploaded by

ncsupply007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like