-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes.txt
More file actions
414 lines (301 loc) · 19.6 KB
/
Notes.txt
File metadata and controls
414 lines (301 loc) · 19.6 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
Geting Input from the User :-
Import java.util.Scanner;
Scanner sc= new Scanner(System.in);
String str= sc.nextLine();
float fnum= sc.nextFloat();
Int number= sc.nextInt();
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Programming Methodologies :-
Procedure Oriented Programming (POP)
Object Oriented Programming (OOP)
Aspect Oriented Programming (AOP)
⭐🌟⭐-_-_-_-_-_-_-_-_-_-_OOP's concept-_-_-_-_-_-_-_-_-_-_⭐🌟⭐
OOP's concept is used in very larg and complex programs.
OOP's concept is based on 4 Principles :-
🥇Encapsulation
-The main goal of Encapsulation is to restrict dirct access to some of an object's information compononents direclt and to make a controlled way to access/to interact with them.
-Eg:Banking System
🥈Abstraction
-Abstraction means showing only the important details while hiding the complex parts. For example, when using an ATM, you only see simple options like withdrawing money, but you don’t need to know how the machine processes it internally. In programming, abstraction helps users focus on what a system does, not how it does it, like querying a database without knowing how it stores data.
🥉Inheritence
-The main goal Inhritence is to allow a Child class to reuse some of the data, behaviors or properties from a Parent class.
-Eg: Hierarchy: Vehicles -> Cars , Trains, Planes.
-Types of Inheritence :-
1 Single Inheritence
-1 Parent class and 1 Child Class
-Eg: Lion -> Cubs
2 Multi Level Inheritence
-When a Parent class makes 1 Child class and then that Child class becomes the Parent class for another child class.
-Eg: Grandfather -> Father -> Son
3 Hierarchial Inheritence
-When a Parent class makes many Child classes.
-Eg: Vehicles -> Water-Based vehicles, 2-Wheelers, 4-Wheelers, 6-Wheelers
4 Hybrid Inheritence
-Combination of 2 or more types of Inheritence.
-Eg: Animals -> (Mammals -> Cats ->Panthors, Tigers, Lions) & (Egg Layers -> Birds -> Penguins, Parrots, Pigeons)
🔥-_-_-_CODE EXAMPLE_-_-_-_🔥
class PARENT{
String 1;
String 2;
String 3;
String 4;
String 5;
void Info(){
System.out.println("This is how to use Inheritence.")
}
}
class CHILD extends PARENT{
void Data(1,2,3,4,5){
1= this.1;
2= this.2;
3= this.3;
4= this.4;
5= this.5;
}
}
class MAIN{
public static void main(String[] args){
CHILD s= new CHILD();
s.Data(My, Name, Is , Amulya , Gupta);
}
}
👨💻-_-_-_CODE SUMMARY_-_-_-_👨💻
Parents class has the intial data and methods , it can contain alot of things, but in this case it has been only used for intializing the characters.
Child class used the keyword, "extends" to inherit all the data and methods from the parent class and combine with code of itself.
Keyword, "this" is used to access the data given by the user or any data which is enterred in that class.
In the main class, we hav created an Object which is an instance of the Child class, when we use any of the methods/functions of the Child class, it will perform the action, based on data given in Parenthesis -> (My, Name, Is , Amulya , Gupta)
4️⃣Polymorphism
- The main goal of Polymorphism is to let the same action behave differently based on the object that is performing the action.
-Eg:Imagine you tell different animals to "speak."
A dog will "bark."
A cat will "meow."
A cow will "moo."
Constructor is a spcial type of method of a class, it's name is sam as the Class name. A Constructor does not have a return type.
Static keyword allows a variable or an object to be accessed in all the Instances in a class.
🔥A static variable is also known as a "GLOBAL VARIABLE".
Method Overloading :- When there are many method with the same name but different parameters OR different data types, this is called Method Overloading.
- Eg:
void Codingal();
void Codingal(int A);
void Codingal(String A);
float Codingal(String B);
Method Overriding :- When Child class and Parent class has the method named same, we use the keyword "@Override" to override the method in the Parent class.
- Eg:
🔥-_-_-_CODE EXAMPLE_-_-_-_🔥
class Ashish {
void name() {
System.out.println("This is Amulya's Father");
}}
class Amulya extends Ashish {
@Override
void name() {
super.name(); // Calls the parent class method
System.out.println("This is Amulya");
}}
public class Main {
public static void main(String[] args) {
Ashish a = new Ashish();
a.name(); // Outputs: This is Amulya's Father
Amulya b = new Amulya();
b.name();
// Outputs:
// This is Amulya's Father
// This is Amulya
}}
👨💻-_-_-_CODE ENDS_-_-_-_👨💻
Super Keyword :- It is used to access the same data from the Parent class into Child class.
- Eg: Above Code Example.⬆️
Access modifiers(See the Access Modifiers.png) (All the files with _am are Access Modifiers Files) :- They are keywords used to set the visibility and accessibility of classes, methods, constructors, and variables. They help control how code is accessed within and across classes or packages.
-> There are four types of access modifiers in Java :-
-> Private: It is commonly used to implement encapsulation by restricting direct access to the fields and methods.
- Example: Banking System
-> Default (No Modifier): It is package-private, meaning classes in the same package can access them, but classes outside the package cannot.
- Example: Consider a package containing different types of animals (Dog, Cat, Bird) in a zoo system. Default access ensures that only the animals within the same package can share certain internal behaviors or data.
-> Protected: It is often used in object-oriented programming to allow controlled access to parent class members for subclasses while restricting access from other classes.
- Example: Think of a parent class Vehicle with a protected method startEngine. This method can be inherited and used by subclasses like Car or Bike even if they are in different packages, but it won’t be accessible to unrelated classes.
-> Public: Public access is typically used for APIs, utility functions, or any members intended for universal access.
- Example: In a library system, the public method checkoutBook allows users to borrow books, while the private method calculateFine is used internally for calculating late fees.
Getters and Setters
-> These are used to set and retrieve the data from a class in java.
🔥-_-_-_CODE EXAMPLE_-_-_-_🔥
class Student{
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
public class private_am {
public static void main(String[] args){
Student s= new Student();
s.setName("Amulya Gupta");
System.out.println("We have successfully set your name to :- " + s.getName());
}
}
👨💻-_-_-_CODE SUMMARY_-_-_-_👨💻
Function setName is a setter as it is getting the value from the user and entering it into a variable.
Function getName is a getter as it allows to retrieve the name of the Student from the Class.
👨💻-_-_-_CODE ENDS_-_-_-_👨💻
Polymorphism
->Breaking polymorphism into 2 words - poly+morphisms ,Poly means many, and morphism means forms.
->Polymorphism is an essential concept of OOPS. It means having various forms. In simple words, it means the same entity(method or object) can perform different operations in different scenarios.
There are mainly 2 types of polymorphism:
->Runtime Polymorphism(Same function name in different classes.)
->It is achieved while running a program. It is done using overriding. We’ll learn more about it using an activity.
->Compile-time Polymorphism(Same function name with different parameters inside same class)
->It is achieved while compiling a program. It is done usually done using method overloading. We’ll learn more about it using an activity.
Differentiate between Polymorphism and Abstraction :-
->Polymorphism means "many forms." It allows the same action or method to work differently based on the object. For example, a person can act as a teacher, parent, or friend depending on the situation. In programming, this means a single function like draw() can work for different shapes like circles or squares, each with its own behavior.
->Abstraction means showing only the important details while hiding the complex parts. For example, when using an ATM, you only see simple options like withdrawing money, but you don’t need to know how the machine processes it internally. In programming, abstraction helps users focus on what a system does, not how it does it, like querying a database without knowing how it stores data.
The benefit of Interfaces:
->It helps Java Achieve multiple inheritances.
->It makes an application more secure.
->It helps in achieving 100% abstraction.
Exception
An exception is an unanticipated event that occurs during the execution of a program. For example, it disrupts the flow of software instructions, perhaps causing the program to crash.
THE METHOD OF HANDLING THESE METHODS IS KNOWN AS EXCEPTIONAL HANDLING.
Checked Exception:
->They are checked at compile time.
Unchecked Exception
->They are not checked at compile time.
Error
->Error is irrecoverable e.g. OutOfMemoryError, VirtualMachine Error.
To understand more about Exception, let’s understand the hierarchy of Exception.
Exceptional Handling(Exceptions.java)
->Try-catch
->The try blocks execute the code that could arise an exception.
->Catch block contains the logic of what needs to be done if that exception has occurred.
->There could be multiple catch blocks, but only one try block.
->Finally block
->Finally block is usually used to print something after try-catch in all the situations. We’ll understand visually in the activity.
Throw
->Throw statement is used for throwing user-defined exceptions(We can create our own exceptions too)
Throws
->Throws is used at the method level to declare that there are chances of exception in this method.
Learn :- https://www.geeksforgeeks.org/interfaces-in-java/
Objects :
->An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
->An object has three characteristics:
->State: represents the data (value) of an object.
->Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
->Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
Classes :
->A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
Constructor :
->It is a special type of method which is used to initialize the object.
->Every time an object is created using the new() keyword, at least one constructor is called.
->It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
Rules of Creating Constructors in Java:
There are two rules defined for the constructor.
->Constructor name must be the same as its class name
->A Constructor must have no explicit return type
->A Java constructor cannot be abstract, static, final, and synchronized
Array
->It is a data structure which can hold a fixed number of elements with same data type.
Kadane's Algorithm
-> Kadane's Algorithm majorly focuses on finding out the maximum subarray of the entir array
-> E.g. = [ -1, 2, -2, 5, 7,-3, 1]
-> [ -1, 2, -2, 5]= 6
-> [2, -2, 5, 7]= 12 ✅ Subarray
-> [-2, 5, 7,-3,]= 7
-> [5, 7,-3, 1]= 10
Co'ordinate for first value in 2D-Array :- (0,0)
Co'ordinate for last value in 2D-Array :- (rows.length-1,columns.length-1)
ArrayList
->It is just an Array with undefined limit.
->Whenever the Initial commit is finished, it's capacity increases by 50% of the initial size whenever the user tries to enter the values further.
->Syntax of ArrayList :-
-> ArrayList<Data Types> Array_name= new ArrayList<>(Values);
-> Eg. = ArrayList<Integer> al= new ArrayList<>();
->To add Elements
->add(element)
->add(index,element)
->addAll(collection)
->addAll(index,collection)
->To access Elements
->get(index)
->set(index,element)
->To remove Elements
->remove(index)
->remove(element)
->clear()
->Checking Size and Existence
->size()
->isEmpty()
->contains(element)
->Iterating over Elements
->
https://www.tpointtech.com/different-types-of-recursions-in-java
Recursion
-> It is the function which calls itself directly or indirectly inside it.
-> It helps in reducing the code and making it easier to write and makes it less complicated.
-> There are 2 types of Recursions :-
-> Direct Recursion
->Tails Recursion
-> In this recursion, the recursion statement is written at the last code line of the function.
->Heads Recursion
-> In this recursion, the recursion statement is written at the first code line of the function.
->Linear Recursion
-> In this Recursion, the Recursion is carried out only one time.
->Tree Recursion
-> In this Recursion, the Recursion is carried out more than one time.
->Nested Recursion
-> In this Recursion, the Recursion is carried out inside of another recursion.
->In-Direct Recursion
-> In this type of recursion, one function carries out another function and then it carries out another function and this becomes a loop when the first function is carried out by another function in this loop.
Tower of Hanoi
-> It is a tricky game in which there are many disks and 3 poles, pole 1 is sourse tower, pole 2 is an auxilary pole, pole 3 is the destination tower.
-> Larger Ring can never be placed over a smaller ring.
-> Move n-1 Discs from A to B using C.
-> Move a Disc from A to C.
-> Move n-1 Discs from B to C using A.
-> Reference :- https://www.youtube.com/watch?v=q6RicK1FCUs
-> Image :- (Tower_of_Hanoi.png)
Binary Search :- https://www.geeksforgeeks.org/binary-search/
Bubble Sort Algorithm :- https://www.geeksforgeeks.org/bubble-sort-algorithm/
Quick-Sort :- https://www.youtube.com/shorts/MeBYqiehwyQ
nextInt() V/s nextLine() :- https://chatgpt.com/share/67ff7d83-01e8-8002-af0c-e6bddcc78996
String Functions :-
-> length(): Returns the length of the string.
-> charAt(int index): Returns the character at the specified index.
-> substring(int beginIndex): Returns a new string that is a substring of the original string, starting from the specified index.
-> substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string, starting from the beginIndex and ending at the endIndex - 1.
-> toLowerCase(): Returns a new string with all characters converted to lowercase.
-> toUpperCase(): Returns a new string with all characters converted to uppercase.
-> trim(): Returns a new string with leading and trailing whitespace removed.
-> startsWith(String prefix): Checks if the string starts with the specified prefix.
-> endsWith(String suffix): Checks if the string ends with the specified suffix.
-> contains(CharSequence sequence): Checks if the string contains the specified sequence of characters.
Knuth Morris Pratt (KMP) Algorithm for pattern searching :- https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
Rabin Karp Algorithm for pattern searching :-https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/
Set in Java :-
-> The set interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts the insertion of the duplicate elements.
Set creation : Set<Obj> set = new HashSet<Obj> ();
add(element)
This method is used to add a specific element to the set. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set.
addAll(collection)
This method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order.
clear()
This method is used to remove all the elements from the set but not delete the set. The reference for the set still exists.
contains(element)
This method is used to check whether a specific element is present in the Set or not.
containsAll(collection)
This method is used to check whether the set contains all the elements present in the given collection or not. This method returns true if the set contains all the elements and returns false if any of the elements are missing.
hashCode()
This method is used to get the hashCode value for this instance of the Set. It returns an integer value which is the hashCode value for this instance of the Set.
isEmpty()
This method is used to check whether the set is empty or not.
iterator()
This method is used to return the iterator of the set. The elements from the set are returned in a random order.
remove(element)
This method is used to remove the given element from the set. This method returns True if the specified element is present in the Set otherwise it returns False.
removeAll(collection)
This method is used to remove all the elements from the collection which are present in the set. This method returns true if this set changed as a result of the call.
retainAll(collection)
This method is used to retain all the elements from the set which are mentioned in the given collection. This method returns true if this set changed as a result of the call.
size()
This method is used to get the size of the set. This returns an integer value which signifies the number of elements.
toArray()
This method is used to form an array of the same elements as that of the Set.