Practice Code

Problem 1: Encapsulation

Scenario: Create a class BankAccount that encapsulates the balance of a bank account. Provide methods to deposit and withdraw money, ensuring that the balance cannot be set directly.

Problem 2: Inheritance

Scenario: Create a base class Animal with a method makeSound(). Derive two classes Dog and Cat from it, each overriding the makeSound() method.

Problem 3: Polymorphism

Scenario: Create an interface Shape with a method calculateArea(). Implement this interface in two classes Circle and Rectangle.

Problem 4: Abstraction

Scenario: Create an abstract class Vehicle with an abstract method move(). Derive two concrete classes Car and Bicycle from it, each implementing the move() method.

Problem 5: Encapsulation

Scenario: Create a class Person that encapsulates the name, age, and address of a person. Provide appropriate constructors, getters, and setters.

Problem 6: Abstraction

Scenario: Create an abstract class Appliance with an abstract method operate(). Derive two concrete classes WashingMachine and Microwave from it, each implementing the operate() method.

Advanced Problem 1: Encapsulation and Validation

Scenario: Create a class User that encapsulates a username and password. Implement methods to set the username and password with validation (e.g., the password must be at least 8 characters long).

Advanced Problem 2: Inheritance and Interfaces

Scenario: Create a base class Employee and an interface Payable. The Employee class should implement the Payable interface and include subclasses FullTimeEmployee and PartTimeEmployee with their own salary calculation methods.

Code:

Advanced Problem 3: Polymorphism with Overloaded Methods

Scenario: Create a class Calculator that demonstrates method overloading by implementing multiple add methods to handle different types and numbers of parameters.

Code:

Advanced Problem 4: Abstraction with Template Method Pattern

Scenario: Create an abstract class Game with a template method playGame(). Implement concrete subclasses Football and Chess with their own specific implementations.

Code:

Additional Advanced Problem Ideas:

  1. Encapsulation and Dependency Injection: Create a Car class with encapsulated fields for the engine, wheels, and transmission. Use dependency injection to provide these components.

  2. Inheritance and Abstract Classes: Create a base class Shape with abstract methods calculateArea() and calculatePerimeter(). Extend it with Circle, Rectangle, and Triangle classes, each providing their own implementations.

  3. Polymorphism with Interfaces and Generics: Create a generic interface Repository<T> with methods for adding, removing, and finding items. Implement this interface with InMemoryRepository and DatabaseRepository classes.

  4. Abstraction with Factory Pattern: Create an abstract class AnimalFactory with a method createAnimal(). Implement concrete factories DogFactory and CatFactory that create Dog and Cat objects, respectively.

These advanced problems will further challenge your understanding and application of OOP principles in Java. Good luck with your preparation!

Certainly! Here are four coding problems, each designed to test your understanding of the four principles of Object-Oriented Programming (OOP) in Java: encapsulation, inheritance, polymorphism, and abstraction. Along with each problem, I will provide a sample solution.

Problem 1: Encapsulation

Scenario: Create a class BankAccount that encapsulates the balance of a bank account. Provide methods to deposit and withdraw money, ensuring that the balance cannot be set directly.

Code:

Problem 2: Inheritance

Scenario: Create a base class Animal with a method makeSound(). Derive two classes Dog and Cat from it, each overriding the makeSound() method.

Code:

Problem 3: Polymorphism

Scenario: Create an interface Shape with a method calculateArea(). Implement this interface in two classes Circle and Rectangle.

Code:

Problem 4: Abstraction

Scenario: Create an abstract class Vehicle with an abstract method move(). Derive two concrete classes Car and Bicycle from it, each implementing the move() method.

Code:

Additional Exercise Ideas:

  1. Encapsulation: Create a Student class with fields for name, age, and grade. Ensure proper encapsulation with getters and setters.

  2. Inheritance: Create a Person class and extend it with Student and Teacher classes, each with additional specific attributes and methods.

  3. Polymorphism: Create a base class Employee with a method calculateSalary(). Derive classes Manager and Developer that override this method.

  4. Abstraction: Create an abstract class Appliance with an abstract method turnOn(). Implement this method in WashingMachine and Refrigerator classes.

These problems and solutions should help you prepare for your interview and demonstrate a strong understanding of the four principles of OOP in Java. Good luck!

2/24o

more questions

ChatGPT

Sure! Here are more questions that cover the four principles of OOP (encapsulation, inheritance, polymorphism, and abstraction), each with a scenario and a corresponding Java code solution.

Problem 5: Encapsulation

Scenario: Create a class Person that encapsulates the name, age, and address of a person. Provide appropriate constructors, getters, and setters.

Code:

Problem 6: Inheritance

Scenario: Create a base class Employee with fields for name and salary. Derive two classes Manager and Engineer, each with additional fields and methods.

Code:

Problem 7: Polymorphism

Scenario: Create a base class Media with a method play(). Derive two classes Audio and Video, each overriding the play() method.

Code:

Problem 8: Abstraction

Scenario: Create an abstract class Appliance with an abstract method operate(). Derive two concrete classes WashingMachine and Microwave from it, each implementing the operate() method.

Code:

Additional Problem Ideas:

  1. Encapsulation: Create a LibraryBook class with fields for title, author, and ISBN. Ensure these fields are properly encapsulated.

  2. Inheritance: Create a base class Shape with methods draw() and erase(). Extend it with classes Circle, Square, and Triangle, each with specific implementations.

  3. Polymorphism: Create an interface Vehicle with a method startEngine(). Implement this interface in classes Car, Motorcycle, and Truck.

  4. Abstraction: Create an abstract class Game with an abstract method play(). Derive concrete classes Soccer and Chess from it, each with its own implementation of play().

These additional problems will further solidify your understanding of OOP principles in Java. Good luck with your interview preparation!

4o

more advance

ChatGPT

Sure, let's delve into more advanced scenarios that demonstrate a deeper understanding of OOP principles in Java, including more complex interactions between classes and more detailed coding problems.

Advanced Problem 1: Encapsulation and Validation

Scenario: Create a class User that encapsulates a username and password. Implement methods to set the username and password with validation (e.g., the password must be at least 8 characters long).

Code:

Advanced Problem 2: Inheritance and Interfaces

Scenario: Create a base class Employee and an interface Payable. The Employee class should implement the Payable interface and include subclasses FullTimeEmployee and PartTimeEmployee with their own salary calculation methods.

Code:

Advanced Problem 3: Polymorphism with Overloaded Methods

Scenario: Create a class Calculator that demonstrates method overloading by implementing multiple add methods to handle different types and numbers of parameters.

Code:

Advanced Problem 4: Abstraction with Template Method Pattern

Scenario: Create an abstract class Game with a template method playGame(). Implement concrete subclasses Football and Chess with their own specific implementations.

Code:

Additional Advanced Problem Ideas:

  1. Encapsulation and Dependency Injection: Create a Car class with encapsulated fields for the engine, wheels, and transmission. Use dependency injection to provide these components.

  2. Inheritance and Abstract Classes: Create a base class Shape with abstract methods calculateArea() and calculatePerimeter(). Extend it with Circle, Rectangle, and Triangle classes, each providing their own implementations.

  3. Polymorphism with Interfaces and Generics: Create a generic interface Repository<T> with methods for adding, removing, and finding items. Implement this interface with InMemoryRepository and DatabaseRepository classes.

  4. Abstraction with Factory Pattern: Create an abstract class AnimalFactory with a method createAnimal(). Implement concrete factories DogFactory and CatFactory that create Dog and Cat objects, respectively.

These advanced problems will further challenge your understanding and application of OOP principles in Java. Good luck with your preparation!

4o

also add solid principle pratical question on this

ChatGPT

Certainly! Here are practical coding problems that incorporate the SOLID principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion) along with their corresponding Java solutions.

Problem 1: Single Responsibility Principle (SRP)

Scenario: Create a Book class with methods to handle the book details and another class to handle book printing.

Code:

Problem 2: Open/Closed Principle (OCP)

Scenario: Create a Shape class with a method to calculate the area. Extend the functionality by creating specific shape classes without modifying the existing code.

Code:

Problem 3: Liskov Substitution Principle (LSP)

Scenario: Create a base class Bird and a subclass FlyingBird that extends Bird. Ensure that replacing Bird with FlyingBird does not alter the correctness of the program.

Code:

Problem 4: Interface Segregation Principle (ISP)

Scenario: Create an interface Worker with methods work and eat. Separate these methods into different interfaces and implement them in respective classes.

Code:

Problem 5: Dependency Inversion Principle (DIP)

Scenario: Create a Keyboard and Monitor class. Implement a Computer class that depends on abstractions (interfaces) rather than concrete classes.

Code:

Additional Advanced Problem Ideas:

  1. Encapsulation and Builder Pattern: Create a House class with multiple optional parameters. Use the Builder pattern to construct House objects.

  2. Inheritance and Strategy Pattern: Create a base Transport class with a move method. Implement different movement strategies (DriveStrategy, FlyStrategy, SailStrategy) and use them in subclasses.

  3. Polymorphism and Command Pattern: Create an interface Command with an execute method. Implement different command classes (OpenCommand, CloseCommand, SaveCommand) and a CommandExecutor class to execute these commands.

  4. Abstraction and Observer Pattern: Create an abstract Subject class with methods to attach, detach, and notify observers. Implement concrete ConcreteSubject and Observer classes to demonstrate the Observer pattern.

  5. SOLID and Dependency Injection with Spring: Create a Spring-based application demonstrating Dependency Injection, where services and repositories are injected into controllers using annotations.

Last updated