AUA | Intro to CS | HW2
Published: 2023-11-30 10:35:25

With this post I will introduce classes, objects, and loops in Java, as well as cover some basic OOP ideas and its four pillars.
OOP:
Firstly, lets understand what is OOP and what are the 4 pillars of it.  
OOP stands for object-oriented programming and is about creating objects that contain both data and methods. Its packages are structured around objects rather than functions and logic. OOP provides a clear structure for the programs and is faster to execute. However, Java is not a 100% object-oriented language as it supports primitive data types like int, byte, long, short, etc., which are not objects. 
The four pillars of OOP are inheritance, encapsulation, abstraction, and polymorphism.
1. Abstraction
Abstraction refers to showing only the most essential details while hiding the rest. It is the process of revealing only the data that is strictly required, with implementation or background information hidden. This is done by making the methods for accessing the data public and limiting the variables' access by setting the access modifier to private. Because of abstraction, we may give a user a straightforward interface without requiring complex details in order to complete an action. In other words, it enables the driver to drive the car without requiring to be aware of further details about "how the engine works."
2. Encapsulation
The definition of encapsulation is the grouping of information and data into a single entity. It also leads to data abstraction or hiding. 

3. Inheritance
The process by which objects of one class inherit characteristics and attributes of another class is known as inheritance. The class from which the features are inherited is referred to as the parent class; the class from which the features are inherited is referred to as the child class.


4. Polymorphism
The word polymorphism means having many forms. It is a feature that allows us to perform an action in multiple or different ways. It provides a function or an operator with more than one definition and can be implemented using function overloading, operator overload, function overriding, etc. An operation may show off different behaviors at different times. And that system is known as operator overloading. Meanwhile, function overloading uses a single function name to perform different tasks.

Next, lets talk about classes and strings in Java. 
Strings:
String is a sequence of characters, which are immutable, meaning that they are constant and cannot be changed once created. 
There are two ways to create a string in Java:
1.	String literal:
String s = “AUA”
2.	Using the keyword new:
String s = new String (“AUA”)
Classes:
Java is an object-oriented programming language, which means that all of its features, including classes and objects, are related to each other. For instance, in the real world a car is an object. The car contains features like color and weight, as well as methods like brake and drive. An object is an instance of a class, and a class is a template for objects. When the individual objects are created, they inherit all the variables and methods from the class. A class must always start with an uppercase first letter, and the name of the java file should math the class name. 
Here is an example of a class:
To create it we use the keyword class.
public class Aua {
     int x = 2026;
}
This is a class named “Aua” and it contains only one number – 2026(which is my year of graduation:)).
Objects:
An object is created from a class. We have already created the class named Aua, so now we can use this to create objects.
To create an object of Aua, specify the class name, followed by the object name, and use the keyword new:
Here is an example of creating an object called “collegeOfEng”:
public class Aua {
   int x = 2026;
   public static void main(String[] args) {
    Aua collegeOfEng = new Aua();
   System.out.println(collegeOfEng.x);
   }
}
This will the print the value of x. 
Loops:
With the help of looping a series of instructions or functions can be executed repeatedly while a condition is evaluated to true. 
while loop: 
A while loop enables code to be repeatedly run in response to a specified Boolean condition. It can be thought of as a repeating if statement. The loop starts with the checking of Boolean condition. If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. 
Once the condition is evaluated to true, the statements in the loop body are executed. Normally the statements contain an update value for the variable being processed for the next iteration.
When the condition becomes false, the loop terminates which marks the end of its life cycle.


Syntax :
while (boolean condition)
{
   loop statements...
}

Example:
 
class Main {
    public static void main (String[] args) {
      int i=0;
      while (i<=4)
      {
        System.out.println(i);
        i++;
      }
    }
}
Outputs.
0
1
2
3
4

for loop: 
A for statement, in contrast to a while loop, combines the initialization, condition, and increment/decrement into a single line, making the looping structure shorter and simpler to debug.
Syntax:
for (initialization condition; testing condition;increment/decrement)
{
    statement(s)
}
Example:
 
class Main {
    public static void main (String[] args) {
       for (int i=0;i<=4;i++)
       {
         System.out.println(i);
       }
    }
}

Output
0
1
2
3
4


Initialization condition:
Here, we initialize the variable in use. It marks the start of a for loop. An already declared variable can be used or a variable can be declared, local to loop only.
Testing Condition: 
It is used for testing the exit condition for a loop. It must return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements.
Statement execution:
Once the condition is evaluated to true, the statements in the loop body are executed.
Increment/ Decrement: 
It is used for updating the variable for next iteration.
Loop termination:
When the condition becomes false, the loop terminates marking the end of its life cycle.

do while loop:
do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop. 

Syntax:
do
{
    statements..
 }
    while (condition);

Example:


class Main {
    public static void main (String[] args) {
      int i=0;
      do
      {
        System.out.println(i);
        i++;
      }while(i<=4);
    }
}
Output
0
1
2
3
4
•	do while loop starts with the execution of the statement(s). There is no checking of any condition for the first time.
•	After the execution of the statements, and update of the variable value, the condition is checked for true or false value. If it is evaluated to true, next iteration of loop starts.
•	When the condition becomes false, the loop terminates which marks the end of its life cycle.
•	It is important to note that the do-while loop will execute its statements atleast once before any condition is checked, and therefore is an example of exit control loop.

Video link: https://youtu.be/dbC-i0tqbEg