Repository navigation
Expand file tree
/
Copy pathOOPLearningGUI.java
More file actions
720 lines (654 loc) · 36.4 KB
/
Copy pathOOPLearningGUI.java
File metadata and controls
720 lines (654 loc) · 36.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package oop;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
/**
* OOPLearningGUI - Interactive JavaFX Learning Platform for Object-Oriented Programming
*
* EDUCATIONAL PURPOSE:
* This JavaFX GUI provides an interactive environment to learn and experiment with:
* - Classes and Objects
* - Inheritance and Polymorphism
* - Encapsulation and Abstraction
* - Interfaces and Abstract Classes
* - Design Patterns
* - SOLID Principles
*
* LEARNING APPROACH:
* - Interactive class design demonstrations
* - Real-time object creation and manipulation
* - Inheritance hierarchy visualizations
* - Design pattern implementations
* - Best practices examples
*
* @author Java Examples Project
* @version 1.0
*/
public class OOPLearningGUI extends Application {
private TextArea outputArea;
private TextField inputField;
private Label statusLabel;
private ComboBox<String> conceptSelector;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("OOP Learning Platform");
// Create main layout using GridPane
GridPane mainLayout = new GridPane();
mainLayout.setPadding(new Insets(10));
mainLayout.setHgap(10);
mainLayout.setVgap(10);
// Configure column constraints
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(50);
ColumnConstraints col2 = new ColumnConstraints();
col2.setPercentWidth(50);
mainLayout.getColumnConstraints().addAll(col1, col2);
// Create sections
createHeaderSection(mainLayout);
createCoreConceptsSection(mainLayout);
createAdvancedConceptsSection(mainLayout);
createInteractiveSection(mainLayout);
createOutputSection(mainLayout);
createControlSection(mainLayout);
Scene scene = new Scene(mainLayout, 1100, 750);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
appendOutput("Welcome to Object-Oriented Programming Learning Platform!");
appendOutput("Learn OOP concepts through interactive examples and demonstrations.");
appendOutput("Click any concept button to see detailed explanations and code examples.\n");
}
private void createHeaderSection(GridPane layout) {
Label titleLabel = new Label("Object-Oriented Programming Learning Platform");
titleLabel.setFont(Font.font("System", FontWeight.BOLD, 18));
statusLabel = new Label("Ready to learn OOP concepts!");
statusLabel.setStyle("-fx-text-fill: #2e7d32;");
VBox headerBox = new VBox(5);
headerBox.getChildren().addAll(titleLabel, statusLabel);
headerBox.setAlignment(Pos.CENTER);
layout.add(headerBox, 0, 0, 2, 1);
}
private void createCoreConceptsSection(GridPane layout) {
Label sectionLabel = new Label("1. Core OOP Concepts");
sectionLabel.setFont(Font.font("System", FontWeight.BOLD, 14));
Button classesBtn = new Button("Classes & Objects");
classesBtn.setMaxWidth(Double.MAX_VALUE);
classesBtn.setOnAction(e -> demonstrateClassesAndObjects());
Button encapsulationBtn = new Button("Encapsulation");
encapsulationBtn.setMaxWidth(Double.MAX_VALUE);
encapsulationBtn.setOnAction(e -> demonstrateEncapsulation());
Button inheritanceBtn = new Button("Inheritance");
inheritanceBtn.setMaxWidth(Double.MAX_VALUE);
inheritanceBtn.setOnAction(e -> demonstrateInheritance());
Button polymorphismBtn = new Button("Polymorphism");
polymorphismBtn.setMaxWidth(Double.MAX_VALUE);
polymorphismBtn.setOnAction(e -> demonstratePolymorphism());
Button abstractionBtn = new Button("Abstraction");
abstractionBtn.setMaxWidth(Double.MAX_VALUE);
abstractionBtn.setOnAction(e -> demonstrateAbstraction());
VBox coreBox = new VBox(5);
coreBox.getChildren().addAll(sectionLabel, classesBtn, encapsulationBtn,
inheritanceBtn, polymorphismBtn, abstractionBtn);
coreBox.setStyle("-fx-border-color: #cccccc; -fx-border-width: 1; -fx-padding: 10;");
layout.add(coreBox, 0, 1);
}
private void createAdvancedConceptsSection(GridPane layout) {
Label sectionLabel = new Label("2. Advanced OOP Concepts");
sectionLabel.setFont(Font.font("System", FontWeight.BOLD, 14));
Button interfacesBtn = new Button("Interfaces");
interfacesBtn.setMaxWidth(Double.MAX_VALUE);
interfacesBtn.setOnAction(e -> demonstrateInterfaces());
Button designPatternsBtn = new Button("Design Patterns");
designPatternsBtn.setMaxWidth(Double.MAX_VALUE);
designPatternsBtn.setOnAction(e -> demonstrateDesignPatterns());
Button solidBtn = new Button("SOLID Principles");
solidBtn.setMaxWidth(Double.MAX_VALUE);
solidBtn.setOnAction(e -> demonstrateSOLIDPrinciples());
Button compositionBtn = new Button("Composition vs Inheritance");
compositionBtn.setMaxWidth(Double.MAX_VALUE);
compositionBtn.setOnAction(e -> demonstrateComposition());
Button bestPracticesBtn = new Button("OOP Best Practices");
bestPracticesBtn.setMaxWidth(Double.MAX_VALUE);
bestPracticesBtn.setOnAction(e -> demonstrateBestPractices());
VBox advancedBox = new VBox(5);
advancedBox.getChildren().addAll(sectionLabel, interfacesBtn, designPatternsBtn,
solidBtn, compositionBtn, bestPracticesBtn);
advancedBox.setStyle("-fx-border-color: #cccccc; -fx-border-width: 1; -fx-padding: 10;");
layout.add(advancedBox, 1, 1);
}
private void createInteractiveSection(GridPane layout) {
Label sectionLabel = new Label("Interactive Examples");
sectionLabel.setFont(Font.font("System", FontWeight.BOLD, 14));
conceptSelector = new ComboBox<>();
conceptSelector.getItems().addAll(
"Create Simple Class",
"Demonstrate Inheritance",
"Show Polymorphism",
"Interface Implementation",
"Design Pattern Example"
);
conceptSelector.setValue("Create Simple Class");
conceptSelector.setMaxWidth(Double.MAX_VALUE);
Button executeBtn = new Button("Run Interactive Example");
executeBtn.setMaxWidth(Double.MAX_VALUE);
executeBtn.setOnAction(e -> runInteractiveExample());
Button simulateBtn = new Button("Simulate OOP Scenario");
simulateBtn.setMaxWidth(Double.MAX_VALUE);
simulateBtn.setOnAction(e -> simulateOOPScenario());
VBox interactiveBox = new VBox(5);
interactiveBox.getChildren().addAll(sectionLabel, conceptSelector, executeBtn, simulateBtn);
interactiveBox.setStyle("-fx-border-color: #cccccc; -fx-border-width: 1; -fx-padding: 10;");
layout.add(interactiveBox, 0, 2, 2, 1);
}
private void createOutputSection(GridPane layout) {
Label outputLabel = new Label("Output Console");
outputLabel.setFont(Font.font("System", FontWeight.BOLD, 14));
outputArea = new TextArea();
outputArea.setEditable(false);
outputArea.setPrefRowCount(15);
outputArea.setStyle("-fx-font-family: 'Courier New', monospace; -fx-font-size: 12px; " +
"-fx-background-color: #f4f4f4; -fx-text-fill: #000000;");
VBox outputBox = new VBox(5);
outputBox.getChildren().addAll(outputLabel, outputArea);
layout.add(outputBox, 0, 3, 2, 1);
}
private void createControlSection(GridPane layout) {
Label noteLabel = new Label("Notes:");
inputField = new TextField();
inputField.setPromptText("Enter your notes here...");
Button addNoteBtn = new Button("Add Note");
addNoteBtn.setOnAction(e -> addNote());
Button clearBtn = new Button("Clear Output");
clearBtn.setOnAction(e -> clearOutput());
HBox controlBox = new HBox(10);
controlBox.setAlignment(Pos.CENTER_LEFT);
controlBox.getChildren().addAll(noteLabel, inputField, addNoteBtn, clearBtn);
layout.add(controlBox, 0, 4, 2, 1);
}
private void appendOutput(String text) {
Platform.runLater(() -> {
outputArea.appendText(text + "\n");
outputArea.setScrollTop(Double.MAX_VALUE); // Auto-scroll to bottom
});
}
private void demonstrateClassesAndObjects() {
// Implementation for demonstrating classes and objects
appendOutput("Demonstrating Classes and Objects:");
appendOutput("1. Class Definition: A class is defined using the 'class' keyword.");
appendOutput("2. Creating Objects: Objects are created using the 'new' keyword.");
appendOutput("3. Accessing Members: Use '.' to access class members (attributes/methods).");
appendOutput("4. Example: See console for class and object example.");
// Example code (to be executed)
String exampleCode = "public class Car {\n" +
" String color;\n" +
" void displayColor() {\n" +
" System.out.println(\"Car color is \" + color);\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Car myCar = new Car(); // Creating an object\n" +
" myCar.color = \"Red\"; // Setting attribute value\n" +
" myCar.displayColor(); // Calling method\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateEncapsulation() {
// Implementation for demonstrating encapsulation
appendOutput("Demonstrating Encapsulation:");
appendOutput("1. What is Encapsulation? Bundling data (attributes) and methods that operate on the data within one unit (class).");
appendOutput("2. Why Encapsulation? To restrict direct access to some of an object's components, which is a means of preventing unintended interference and misuse of the methods and data.");
appendOutput("3. How? Using access modifiers (private, protected, public) to set visibility of class members.");
appendOutput("4. Example: See console for encapsulation example.");
// Example code (to be executed)
String exampleCode = "public class Account {\n" +
" private double balance; // private variable\n" +
" \n" +
" // public method to access private variable\n" +
" public double getBalance() {\n" +
" return balance;\n" +
" }\n" +
" \n" +
" // public method to modify private variable\n" +
" public void deposit(double amount) {\n" +
" if (amount > 0) {\n" +
" balance += amount;\n" +
" }\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Account myAccount = new Account();\n" +
" myAccount.deposit(1000);\n" +
" System.out.println(\"Account balance: \" + myAccount.getBalance());\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateInheritance() {
// Implementation for demonstrating inheritance
appendOutput("Demonstrating Inheritance:");
appendOutput("1. What is Inheritance? Mechanism where a new class is derived from an existing class.");
appendOutput("2. Why Inheritance? To promote code reusability and establish a relationship between classes.");
appendOutput("3. How? Using the 'extends' keyword to inherit attributes and methods from the parent class.");
appendOutput("4. Example: See console for inheritance example.");
// Example code (to be executed)
String exampleCode = "public class Animal {\n" +
" void eat() {\n" +
" System.out.println(\"This animal eats food.\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Dog extends Animal {\n" +
" void bark() {\n" +
" System.out.println(\"The dog barks.\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Dog myDog = new Dog();\n" +
" myDog.eat(); // Inherited method\n" +
" myDog.bark(); // Dog's own method\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstratePolymorphism() {
// Implementation for demonstrating polymorphism
appendOutput("Demonstrating Polymorphism:");
appendOutput("1. What is Polymorphism? Ability of a variable, function or object to take on multiple forms.");
appendOutput("2. Why Polymorphism? To allow methods to do different things based on the object it is acting upon.");
appendOutput("3. How? Method overriding and overloading.");
appendOutput("4. Example: See console for polymorphism example.");
// Example code (to be executed)
String exampleCode = "class Animal {\n" +
" void sound() {\n" +
" System.out.println(\"Animal makes a sound\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Cat extends Animal {\n" +
" void sound() {\n" +
" System.out.println(\"Cat meows\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Dog extends Animal {\n" +
" void sound() {\n" +
" System.out.println(\"Dog barks\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Animal myAnimal;\n" +
" myAnimal = new Cat(); // Cat object\n" +
" myAnimal.sound(); // Calls Cat's sound()\n" +
" myAnimal = new Dog(); // Dog object\n" +
" myAnimal.sound(); // Calls Dog's sound()\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateAbstraction() {
// Implementation for demonstrating abstraction
appendOutput("Demonstrating Abstraction:");
appendOutput("1. What is Abstraction? Hiding the complex implementation details and showing only the essential features of the object.");
appendOutput("2. Why Abstraction? To reduce complexity and increase efficiency.");
appendOutput("3. How? Using abstract classes and interfaces.");
appendOutput("4. Example: See console for abstraction example.");
// Example code (to be executed)
String exampleCode = "abstract class Shape {\n" +
" abstract void draw(); // abstract method\n" +
"}\n" +
"\n" +
"class Circle extends Shape {\n" +
" void draw() {\n" +
" System.out.println(\"Drawing a circle\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Square extends Shape {\n" +
" void draw() {\n" +
" System.out.println(\"Drawing a square\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Shape s1 = new Circle();\n" +
" s1.draw(); // Calls Circle's draw()\n" +
" Shape s2 = new Square();\n" +
" s2.draw(); // Calls Square's draw()\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateInterfaces() {
// Implementation for demonstrating interfaces
appendOutput("Demonstrating Interfaces:");
appendOutput("1. What are Interfaces? A reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.");
appendOutput("2. Why Interfaces? To achieve abstraction and multiple inheritance in Java.");
appendOutput("3. How? Using the 'interface' keyword to declare an interface, and 'implements' keyword in the class to implement the interface.");
appendOutput("4. Example: See console for interface example.");
// Example code (to be executed)
String exampleCode = "interface Animal {\n" +
" void eat(); // interface method (does not have a body)\n" +
"}\n" +
"\n" +
"class Dog implements Animal {\n" +
" public void eat() {\n" +
" System.out.println(\"Dog eats bones\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Cat implements Animal {\n" +
" public void eat() {\n" +
" System.out.println(\"Cat eats fish\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Animal myDog = new Dog();\n" +
" myDog.eat(); // Calls Dog's eat()\n" +
" Animal myCat = new Cat();\n" +
" myCat.eat(); // Calls Cat's eat()\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateDesignPatterns() {
// Implementation for demonstrating design patterns
appendOutput("Demonstrating Design Patterns:");
appendOutput("1. What are Design Patterns? General reusable solutions to commonly occurring problems within a given context in software design.");
appendOutput("2. Why Design Patterns? To speed up the development process by providing tested, proven development paradigms.");
appendOutput("3. How? By implementing standard design patterns like Singleton, Factory, Observer, etc.");
appendOutput("4. Example: See console for design pattern example.");
// Example code (to be executed)
String exampleCode = "// Singleton Pattern\n" +
"class Singleton {\n" +
" private static Singleton instance;\n" +
" \n" +
" private Singleton() {} // private constructor\n" +
" \n" +
" public static Singleton getInstance() {\n" +
" if (instance == null) {\n" +
" instance = new Singleton();\n" +
" }\n" +
" return instance;\n" +
" }\n" +
"}\n" +
"\n" +
"// Factory Pattern\n" +
"class Shape {\n" +
" String type;\n" +
" \n" +
" Shape(String type) {\n" +
" this.type = type;\n" +
" }\n" +
"}\n" +
"\n" +
"class ShapeFactory {\n" +
" static Shape getShape(String type) {\n" +
" return new Shape(type);\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" // Singleton\n" +
" Singleton s1 = Singleton.getInstance();\n" +
" Singleton s2 = Singleton.getInstance();\n" +
" System.out.println(s1 == s2); // true (same instance)\n" +
" \n" +
" // Factory\n" +
" Shape shape1 = ShapeFactory.getShape(\"Circle\");\n" +
" Shape shape2 = ShapeFactory.getShape(\"Square\");\n" +
" System.out.println(shape1.type + \" \" + shape2.type); // Circle Square\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateSOLIDPrinciples() {
// Implementation for demonstrating SOLID principles
appendOutput("Demonstrating SOLID Principles:");
appendOutput("1. What are SOLID Principles? A set of five design principles intended to make software designs more understandable, flexible, and maintainable.");
appendOutput("2. Why SOLID Principles? To avoid code smells and ensure a well-structured, maintainable codebase.");
appendOutput("3. How? By following each of the SOLID principles: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.");
appendOutput("4. Example: See console for SOLID principles example.");
// Example code (to be executed)
String exampleCode = "// Single Responsibility Principle\n" +
"class User {\n" +
" private String name;\n" +
" \n" +
" public void setName(String name) {\n" +
" this.name = name;\n" +
" }\n" +
" \n" +
" public String getName() {\n" +
" return name;\n" +
" }\n" +
"}\n" +
"\n" +
"// Open-Closed Principle\n" +
"abstract class Shape {\n" +
" abstract double area();\n" +
"}\n" +
"\n" +
"class Rectangle extends Shape {\n" +
" private double width, height;\n" +
" \n" +
" Rectangle(double width, double height) {\n" +
" this.width = width;\n" +
" this.height = height;\n" +
" }\n" +
" \n" +
" double area() {\n" +
" return width * height;\n" +
" }\n" +
"}\n" +
"\n" +
"class Circle extends Shape {\n" +
" private double radius;\n" +
" \n" +
" Circle(double radius) {\n" +
" this.radius = radius;\n" +
" }\n" +
" \n" +
" double area() {\n" +
" return Math.PI * radius * radius;\n" +
" }\n" +
"}\n" +
"\n" +
"// Liskov Substitution Principle\n" +
"class Bird {\n" +
" void fly() {\n" +
" System.out.println(\"Bird can fly\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Ostrich extends Bird {\n" +
" void fly() {\n" +
" throw new UnsupportedOperationException(\"Ostrich can't fly\");\n" +
" }\n" +
"}\n" +
"\n" +
"// Interface Segregation Principle\n" +
"interface Animal {\n" +
" void eat();\n" +
" void fly();\n" +
"}\n" +
"\n" +
"class Dog implements Animal {\n" +
" public void eat() {\n" +
" System.out.println(\"Dog eats\");\n" +
" }\n" +
" \n" +
" public void fly() {\n" +
" throw new UnsupportedOperationException(\"Dog can't fly\");\n" +
" }\n" +
"}\n" +
"\n" +
"// Dependency Inversion Principle\n" +
"class LightBulb {\n" +
" void turnOn() {\n" +
" System.out.println(\"LightBulb turned on\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Switch {\n" +
" private LightBulb bulb;\n" +
" \n" +
" Switch(LightBulb bulb) {\n" +
" this.bulb = bulb;\n" +
" }\n" +
" \n" +
" void operate() {\n" +
" bulb.turnOn();\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" // Single Responsibility\n" +
" User user = new User();\n" +
" user.setName(\"John Doe\");\n" +
" System.out.println(user.getName());\n" +
" \n" +
" // Open-Closed\n" +
" Shape rect = new Rectangle(10, 5);\n" +
" System.out.println(\"Rectangle area: \" + rect.area());\n" +
" Shape circ = new Circle(7);\n" +
" System.out.println(\"Circle area: \" + circ.area());\n" +
" \n" +
" // Liskov Substitution\n" +
" Bird bird = new Bird();\n" +
" bird.fly();\n" +
" Bird ostrich = new Ostrich();\n" +
" ostrich.fly(); // Exception\n" +
" \n" +
" // Interface Segregation\n" +
" Animal dog = new Dog();\n" +
" dog.eat();\n" +
" dog.fly(); // Exception\n" +
" \n" +
" // Dependency Inversion\n" +
" LightBulb bulb = new LightBulb();\n" +
" Switch sw = new Switch(bulb);\n" +
" sw.operate();\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateComposition() {
// Implementation for demonstrating composition vs inheritance
appendOutput("Demonstrating Composition vs Inheritance:");
appendOutput("1. Composition: A 'has-a' relationship where a class contains references to objects of other classes as part of its state.");
appendOutput("2. Inheritance: An 'is-a' relationship where a class derives from another class.");
appendOutput("3. When to use Composition over Inheritance? Prefer composition when you want to use functionalities of other classes without inheriting their type.");
appendOutput("4. Example: See console for composition vs inheritance example.");
// Example code (to be executed)
String exampleCode = "class Engine {\n" +
" void start() {\n" +
" System.out.println(\"Engine starts\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Car {\n" +
" private Engine engine; // Composition\n" +
" \n" +
" Car() {\n" +
" engine = new Engine();\n" +
" }\n" +
" \n" +
" void start() {\n" +
" engine.start();\n" +
" }\n" +
"}\n" +
"\n" +
"class Vehicle {\n" +
" void honk() {\n" +
" System.out.println(\"Vehicle honks\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Truck extends Vehicle {\n" +
" void load() {\n" +
" System.out.println(\"Truck loads goods\");\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" Car myCar = new Car();\n" +
" myCar.start();\n" +
" \n" +
" Truck myTruck = new Truck();\n" +
" myTruck.honk();\n" +
" myTruck.load();\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
private void demonstrateBestPractices() {
// Implementation for demonstrating OOP best practices
appendOutput("Demonstrating OOP Best Practices:");
appendOutput("1. Follow SOLID principles for object-oriented design.");
appendOutput("2. Prefer composition over inheritance.");
appendOutput("3. Use interfaces to define contracts for classes.");
appendOutput("4. Keep classes focused and manageable (Single Responsibility Principle).");
appendOutput("5. Example: See console for best practices example.");
// Example code (to be executed)
String exampleCode = "// Best Practice 1: Follow SOLID principles\n" +
"// Best Practice 2: Prefer composition over inheritance\n" +
"// Best Practice 3: Use interfaces to define contracts\n" +
"// Best Practice 4: Keep classes focused\n" +
"\n" +
"// Example class following best practices\n" +
"interface PaymentProcessor {\n" +
" void processPayment(double amount);\n" +
"}\n" +
"\n" +
"class PayPalProcessor implements PaymentProcessor {\n" +
" public void processPayment(double amount) {\n" +
" System.out.println(\"Processing payment of \" + amount + \" through PayPal\");\n" +
" }\n" +
"}\n" +
"\n" +
"class Order {\n" +
" private PaymentProcessor processor;\n" +
" \n" +
" Order(PaymentProcessor processor) {\n" +
" this.processor = processor;\n" +
" }\n" +
" \n" +
" void completeOrder(double amount) {\n" +
" processor.processPayment(amount);\n" +
" }\n" +
"}\n" +
"\n" +
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" PaymentProcessor processor = new PayPalProcessor();\n" +
" Order order = new Order(processor);\n" +
" order.completeOrder(250.75);\n" +
" }\n" +
"}";
appendOutput(exampleCode);
}
public static void main(String[] args) {
launch(args);
}
}