-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOoopppS.java
More file actions
222 lines (180 loc) · 6.03 KB
/
OoopppS.java
File metadata and controls
222 lines (180 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package TimeAndSpace.DSA;
import java.util.*;
// Abstract class for demonstration of abstraction
abstract class Shape {
// Abstract method
public abstract void draw();
// Non-abstract method
public void display() {
System.out.println("This is a shape.");
}
}
// Interface for demonstration of interface implementation
interface Colorable {
void color(String color);
}
// Base class for inheritance demonstration
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void speak() {
System.out.println(name + " makes a sound.");
}
}
// Derived class for inheritance demonstration
class Dog extends Animal {
public Dog(String name) {
super(name);
}
// Overriding the speak method (Polymorphism)
@Override
public void speak() {
System.out.println(name + " barks.");
}
}
// Class for encapsulation demonstration
class Person {
private String name;
private int age;
// Constructor for initialization
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter and Setter methods for encapsulation
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// Method to display information
public void displayPerson() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Derived class from Shape to implement drawing functionality
class Circle extends Shape implements Colorable {
int radius;
public Circle(int radius) {
this.radius = radius;
}
// Implementing the abstract method of Shape class
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
// Implementing the color method from Colorable interface
@Override
public void color(String color) {
System.out.println("Coloring the circle with " + color);
}
}
// Demonstrating Composition
class Engine {
public void start() {
System.out.println("Engine starting...");
}
public void stop() {
System.out.println("Engine stopping...");
}
}
// Car class demonstrates Composition (Car "has" an Engine)
class Car {
private Engine engine;
public Car() {
this.engine = new Engine(); // Car "has" an engine
}
public void startCar() {
engine.start(); // Using the engine's start method
System.out.println("Car is now running.");
}
public void stopCar() {
engine.stop(); // Using the engine's stop method
System.out.println("Car is now stopped.");
}
}
// Demonstrating Aggregation (Department "has" employees but they exist independently)
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public void display() {
System.out.println("Employee Name: " + name);
}
}
// Department class demonstrates Aggregation
class Department {
private String departmentName;
private List<Employee> employees;
public Department(String departmentName) {
this.departmentName = departmentName;
employees = new ArrayList<>();
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void displayDepartmentDetails() {
System.out.println("Department: " + departmentName);
for (Employee employee : employees) {
employee.display();
}
}
}
// Main class to take user input and demonstrate OOPS concepts
public class OoopppS {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Demonstrating Encapsulation
System.out.print("Enter name of the person: ");
String name = scanner.nextLine();
System.out.print("Enter age of the person: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume the newline left-over
Person person = new Person(name, age);
person.displayPerson();
// Demonstrating Inheritance and Polymorphism
System.out.print("\nEnter name of the dog: ");
String dogName = scanner.nextLine();
Dog dog = new Dog(dogName);
dog.speak(); // Demonstrating Polymorphism (Overriding)
// Demonstrating Abstraction and Interfaces
System.out.print("\nEnter radius of the circle: ");
int radius = scanner.nextInt();
scanner.nextLine(); // Consume the newline left-over
Circle circle = new Circle(radius);
circle.draw(); // Demonstrating abstraction (abstract method)
System.out.print("Enter color for the circle: ");
String color = scanner.nextLine();
circle.color(color); // Demonstrating interface implementation
// Demonstrating Composition
System.out.println("\nCreating a car...");
Car car = new Car();
car.startCar(); // Car uses Engine to start
car.stopCar(); // Car uses Engine to stop
// Demonstrating Aggregation
System.out.println("\nCreating a department...");
Department department = new Department("Software Development");
System.out.print("Enter number of employees: ");
int numEmployees = scanner.nextInt();
scanner.nextLine(); // Consume newline left-over
for (int i = 0; i < numEmployees; i++) {
System.out.print("Enter employee name: ");
String employeeName = scanner.nextLine();
Employee employee = new Employee(employeeName);
department.addEmployee(employee);
}
department.displayDepartmentDetails();
// Closing the scanner
scanner.close();
}
}