-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_java.js
More file actions
34 lines (30 loc) · 9.29 KB
/
Copy pathupdate_java.js
File metadata and controls
34 lines (30 loc) · 9.29 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
require('dotenv').config();
const mongoose = require('mongoose');
const Course = require('./backend/models/Course');
mongoose.connect(process.env.MONGODB_URI)
.then(async () => {
console.log('DB Connected for updating Java OOP deeply');
const javaNotes = [
"# **Java & OOP: Complete Notes**\n\nWelcome to the complete Java Object-Oriented Programming guide. Java is an enterprise-level, class-based, object-oriented language.",
"## **1. Variables & Data Types**\nJava is a statically-typed language, meaning all variables must be declared with a type.\n```java\npublic class Main {\n public static void main(String[] args) {\n // Primitive Types\n int age = 25;\n double price = 19.99;\n boolean isActive = true;\n char grade = 'A';\n \n // Reference Type\n String name = \"Alice\";\n \n System.out.println(name + \" is \" + age + \" years old.\");\n }\n}\n```",
"## **2. Control Flow (If-Else & Switch)**\nJava uses curly braces `{}` to define block structures explicitly.\n```java\nint score = 85;\n\nif (score >= 90) {\n System.out.println(\"A Grade\");\n} else if (score >= 80) {\n System.out.println(\"B Grade\");\n} else {\n System.out.println(\"C Grade\");\n}\n\n// Switch Statement\nint day = 3;\nswitch (day) {\n case 1:\n System.out.println(\"Monday\"); break;\n case 3:\n System.out.println(\"Wednesday\"); break;\n default:\n System.out.println(\"Other\");\n}\n```",
"## **3. Loops (For, While, Do-While)**\nLoops execute code repeatedly. Java's syntax is nearly identical to C/C++.\n```java\n// For loop\nfor(int i = 0; i < 3; i++) {\n System.out.print(i + \" \"); // Output: 0 1 2\n}\nSystem.out.println();\n\n// While loop\nint count = 0;\nwhile(count < 3) {\n System.out.print(count + \" \");\n count++;\n}\n\n// Do-While loop (Runs at least once!)\nint j = 0;\ndo {\n System.out.print(j + \" \");\n j++;\n} while(j < 3);\n```",
"## **4. Arrays & ArrayLists**\nStandard Arrays are fixed in size, while `ArrayList` can resize dynamically (like Vectors in C++).\n```java\nimport java.util.ArrayList;\nimport java.util.Collections;\n\npublic class Main {\n public static void main(String[] args) {\n // Static Array\n int[] numbers = {10, 20, 30};\n \n // Dynamic ArrayList\n ArrayList<String> fruits = new ArrayList<>();\n fruits.add(\"Apple\");\n fruits.add(\"Banana\");\n fruits.add(1, \"Mango\"); // Insert at index 1\n \n fruits.remove(\"Apple\"); // Removes \"Apple\"\n \n // Traversing elements\n for(String fruit : fruits) {\n System.out.println(fruit);\n }\n \n // Built-in sorting algorithm natively\n Collections.sort(fruits);\n }\n}\n```",
"## **5. HashMaps (Dictionaries)**\n`HashMap` stores elements in key-value pairs.\n```java\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class Main {\n public static void main(String[] args) {\n HashMap<String, Integer> ages = new HashMap<>();\n \n ages.put(\"Alice\", 28);\n ages.put(\"Bob\", 34);\n \n // Traversing heavily relies on map.entrySet()\n for (Map.Entry<String, Integer> entry : ages.entrySet()) {\n System.out.println(entry.getKey() + \" : \" + entry.getValue());\n }\n }\n}\n```",
"## **6. HashSets**\n`HashSet` stores unique elements implicitly leveraging a HashMap under the hood.\n```java\nimport java.util.HashSet;\n\npublic class Main {\n public static void main(String[] args) {\n HashSet<Integer> uniqueNums = new HashSet<>();\n \n uniqueNums.add(10);\n uniqueNums.add(20);\n uniqueNums.add(10); // Ignored securely\n \n // Searching internally\n if (uniqueNums.contains(20)) {\n System.out.println(\"20 is present!\");\n }\n }\n}\n```",
"## **7. OOP Pillar 1: Encapsulation**\n**Encapsulation** is the mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. It is achieved using varying access modifiers (`private`, `public`, `protected`) to hide internal state and prevent direct unauthorized modification.\n```java\nclass Employee {\n // 1. Private fields (hidden from other classes)\n private String name;\n private double salary;\n\n public Employee(String name) {\n this.name = name;\n this.salary = 50000; // Default secure salary\n }\n\n // 2. Public Getter Method (Read access)\n public double getSalary() {\n return this.salary;\n }\n\n // 3. Public Setter Method (Write access safely controlled)\n public void giveRaise(double amount) {\n if(amount > 0) {\n this.salary += amount;\n } else {\n System.out.println(\"Invalid raise!\");\n }\n }\n}\n\npublic class Main {\n public static void main(String[] args) {\n Employee emp = new Employee(\"John\");\n // emp.salary = 100000; => ERROR: salary has private access\n emp.giveRaise(5000);\n System.out.println(\"Salary is: \" + emp.getSalary());\n }\n}\n```",
"## **8. OOP Pillar 2: Inheritance**\n**Inheritance** enables a new class to absorb the attributes and behavior of an existing class using the `extends` keyword. This establishes an \"IS-A\" relationship, enhancing huge code reusability.\n```java\n// Parent (Superclass)\nclass Vehicle {\n protected String brand = \"Ford\"; // Accessible to subclasses\n \n public void honk() {\n System.out.println(\"Tuut, tuut!\");\n }\n}\n\n// Child (Subclass) inherits from Vehicle\nclass Car extends Vehicle {\n private String modelName = \"Mustang\";\n\n public void showDetails() {\n // Can access inherited parent property directly\n System.out.println(brand + \" \" + modelName);\n }\n}\n\npublic class Main {\n public static void main(String[] args) {\n Car myCar = new Car();\n myCar.honk(); // Inherited naturally from Vehicle class\n myCar.showDetails();\n }\n}\n```",
"## **9. OOP Pillar 3: Polymorphism**\n**Polymorphism** means \"many forms\". It occurs when we have many classes related by inheritance. Java supports **Compile-time Polymorphism** (Method Overloading) and **Runtime Polymorphism** (Method Overriding based on the object's dynamic type).\n```java\nclass Animal {\n public void animalSound() {\n System.out.println(\"The animal makes a sound\");\n }\n}\n\nclass Pig extends Animal {\n // Method Overriding (Same signature as parent)\n @Override\n public void animalSound() {\n System.out.println(\"The pig says: wee wee\");\n }\n}\n\nclass Dog extends Animal {\n @Override\n public void animalSound() {\n System.out.println(\"The dog says: bow wow\");\n }\n}\n\npublic class Main {\n // Compile-time Polymorphism (Method Overloading using different parameters)\n public static int add(int a, int b) { return a + b; }\n public static double add(double a, double b) { return a + b; }\n\n public static void main(String[] args) {\n // RUNTIME POLYMORPHISM (Dynamic Method Dispatch)\n // Parent reference holding child object\n Animal myAnimal = new Animal();\n Animal myPig = new Pig();\n Animal myDog = new Dog();\n \n myAnimal.animalSound(); // Executes Animal's method\n myPig.animalSound(); // Executes Pig's method (Runtime decision)\n myDog.animalSound(); // Executes Dog's method\n }\n}\n```",
"## **10. OOP Pillar 4: Abstraction**\n**Abstraction** hides complex implementation details and shows only the essential operational features of an object. Achieved using **Abstract Classes** (`abstract` keyword, can hold normal and abstract methods) and **Interfaces** (100% abstract contract natively). \n```java\n// 1. ABSTRACT CLASS (Cannot be instantiated directly)\nabstract class Shape {\n // Abstract method (no body, child MUST implement this)\n public abstract void draw(); \n \n // Regular method\n public void sleep() {\n System.out.println(\"Zzz\");\n }\n}\n\nclass Circle extends Shape {\n // Forced to implement the abstract method\n @Override\n public void draw() {\n System.out.println(\"Drawing a circle o\");\n }\n}\n\n// 2. INTERFACE (A contract guaranteeing actions)\ninterface Relocatable {\n void move(int x, int y); // Implicitly public and abstract\n}\n\nclass Rectangle extends Shape implements Relocatable {\n public void draw() {\n System.out.println(\"Drawing a rectangle []\");\n }\n \n public void move(int x, int y) {\n System.out.println(\"Moving to \" + x + \", \" + y);\n }\n}\n\npublic class Main {\n public static void main(String[] args) {\n // Shape myObj = new Shape(); // ERROR: Shape is abstract;\n Shape myCircle = new Circle();\n myCircle.draw();\n myCircle.sleep();\n \n Rectangle rect = new Rectangle();\n rect.move(10, 50);\n }\n}\n```"
];
await Course.updateOne(
{ title: 'Java & OOP' },
{ $set: { notes: javaNotes } }
);
console.log('Deep Java OOP Notes Updated in DB!');
process.exit();
})
.catch(err => {
console.error(err);
process.exit(1);
});