Tuesday, 19 September 2023

JAVA PROGRAMMING - OOPS CHAPTER 1


Object-Oriented Programming Chapter 1:

Basics by Souradeep Roy Using IntelliJ IDEA Community version:-
___________________________________________________________________________________________


INTRODUCTION:
-------------
Java was developed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike
Sheridan at Sun Microsystems in 1991. Initially, it was called 'Oak' but was renamed 'Java'
in 1995. Java was influenced by C++.

 Characteristics:

1) Platform independent (architecture neutral)
2) Object-oriented
3) Multi-threaded
4) Distributed
5) Dynamic

 Bytecode:
------------

Bytecode is a highly optimized set of instructions designed to be executed by the Java run-
time system called the Java Virtual Machine (JVM). JVM is an interpreter for bytecode.

The output of a Java compiler is not executable code, rather it is bytecode. This bytecode
can be run in a wide variety of environments by JVM that will differ from platform to
platform.


The main motive of Java is to implement the concept of OOPS and solve all the real world problems with the help of object creation......
So, lets see how to implement object creation

Question 1: Write a program in Java to demostrate object creation
------------------------------------------------------------------------------------------------------------------------

Code:
-----
class Techno
{
    public void hello()
    {
        System.out.println("hello welcome to the demostration of Object Creation");
    }
}

public class objcreation
{
    public static void main(String[] args)
 {
        Techno m1=new Techno();//always class name is used then object name and then new keyword
        m1.hello();//calling the method by using dot specifier and with the help of the object

    }
}

Output:
--------
class Techno
{
    public void hello()
    {
        System.out.println("hello welcome to the demostration of Object Creation");
    }
}
public class objcreation {

    public static void main(String[] args) {

        Techno m1=new Techno();//always class name is used then object name and then new keyword

        m1.hello();//calling the method by using dot specifier and with the help of the object

    }
}

Output:-
--------
"C:\Program Files\Java\jdk-20\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1.2\lib\idea_rt.jar=55742:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\hp\IdeaProjects\justforfun\out\production\justforfun objcreation
hello welcome to the demostration of Object Creation

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
Question 2: //CREATING MORE THAN ONE OBJECTS

Code:
-----
class Techno
{
    public void hello()
    {
        System.out.println("hello welcome to the demostration of Object Creation");
    }
    public void add(int a,int b)
    {
        int c=a+b;
        System.out.println("Addition of 2 numbers\t"+c);
    }
}
public class objcreation {
    public static void main(String[] args)
{
        Techno m1=new Techno();//always class name is used then object name and then new keyword

        Techno m2=new Techno();//creating 2nd object using same class

        m1.hello();//calling the method by using dot specifier and with the help of the object

        m2.add(8,4); //passing two numbers as parameters to perform operation
    }
}


Output:-
--------
hello welcome to the demostration of Object Creation

Addition of 2 numbers    12

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
Question 3: //CLASS CONTAINING A METHOD


Code:
-----
import java.util.Scanner;
class Techno
{
    double w,h,d;

    public void result()
    {
        System.out.println("Volume is");

        System.out.println(w*h*d);
    }
}
public class objcreation
{
    public static void main(String[] args)
{
        Scanner input=new Scanner(System.in);

        Techno m1=new Techno();

        System.out.println("Enter the width , height , depth");

        m1.w= input.nextDouble();

        m1.h= input.nextDouble();

        m1.d= input.nextDouble();

        m1.result();

    }
}


Output:-
--------

Enter the width , height , depth

5
6
1
Volume is
30.0

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
Question 4://METHOD RETURNING A VALUE


Code:
-----
import java.util.Scanner;
class Techno
{
    double w,h,d;

    public double result()

    {
      return(w*h*d);
    }
}
public class objcreation {

    public static void main(String[] args) {

        Scanner input=new Scanner(System.in);

        Techno m1=new Techno();

        System.out.println("Enter the width , height , depth");

        m1.w= input.nextDouble();

        m1.h= input.nextDouble();

        m1.d= input.nextDouble();

        System.out.println("The volume is "+m1.result());

    }
}

Output:-
--------

Enter the width , height , depth

5
5
5
The volume is 125.0

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------

Question 5://PARAMETARIZED METHOD

Code:
-----
class Box
{
    double width,height,depth;
    double volume()
    {
        return (width * height * depth);
    }
    void getData(double w, double h, double d)
    {
        width = w; height = h; depth = d;
    }
}
public class Jop {
    public static void main(String[] args)
    {
        Box mybox1 = new Box();
        Box mybox2 = new Box();
        double vol;
        mybox1.getData(10,20,15);
        mybox2.getData(3,6,9);
        vol = mybox1.volume();
        System.out.println("Volume 1 is :" + vol);
        vol = mybox2.volume();
        System.out.println("Volume 2 is :" + vol);
    }
}

Output:-
--------
Volume 1 is :3000.0
Volume 2 is :162.0

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
Question 6://CONSTRUCTOR DEMO


Code:
-----
class Type
{

     Type() // Constructor Note: the constructor name and class name is always the same
     {
         System.out.println("This is simple constructor representing demo Constructor");
     }

    double w,h,d;

    Type(double w,double h,double d)//Parameterized Constructor
    {
        this.w =w;
        this.h = h;
        this.d = d;
    }

    double volume() //method
    {
        return (w * h * d);
    }
}

public class Jop {  //Main class

    public static void main(String[] args) //Main Function
    {
        Type m1=new Type(); // this syntax will automatically invoke the default constructor

        Type m2=new Type(10,10,10);//this syntax will automatically invoke the parameterized constructor

        double vol;

        vol=m2.volume();

        System.out.println("The volume is "+vol);
    }
}

Output:-
--------

This is simple constructor representing demo Constructor
The volume is 1000.0

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------

Question 7://OBJECTS MAY BE PASSED TO METHODS

Code:
-----
class Test {

    int a, b;

    Test(int i, int j) {

        a = i;
        b = j;

    }

    boolean equals(Test o)
    {
        if(o.a == a && o.b == b)
            return true;

        else
        {
            return false;
        }
    }
}

    public class Jop {
        public static void main(String[] args) {
            Test obj1 = new Test(100, 22);
            Test obj2 = new Test(100, 22);
            Test obj3 = new Test(15, 20);
            System.out.println("obj1 = = obj2 " + obj1.equals(obj2));
            System.out.println("obj1 == obj3 " + obj1.equals(obj3));
            System.out.println("obj2 == obj3 " + obj2.equals(obj3));

        }
    }

Output:-
--------
obj1 = = obj2 true
obj1 == obj3 false
obj2 == obj3 false

Process finished with exit code 0
-----------------------------------------------------------------------------------------------------------------------------
Question 8://STACK IMPLEMENTATION IN JAVA


class Stack
{
    int stck[] = new int[15];
    int top;
    Stack() //constructor to initialize instance variables
    {
        top = -1;
    }
    void push(int item)//method
    {
        if(top == 14)
            System.out.println("Stack is full");
else
    stck[++top] = item;
    }
    int pop()//method
    {
        if(top<0)
        {
            System.out.println("Stack underflow");
            return 0;
        }
        else return stck[top--];
    }
    void display()
    {
        if(top == -1) // IF THE STACK IS EMPTY
System.out.println("Stack is Empty");
else
        {
            System.out.println("\nStack elements are:");
            for(int i = 0;i < top;i++)
                System.out.print(stck[i] + ",");

        }
    }
}
    public class Jop {
        public static void main(String[] args) {
            Stack myStack = new Stack();//new object creation
            int val;
            myStack.push(10); myStack.push(20); myStack.push(30);
            myStack.push(40);myStack.push(50); myStack.push(60);
            myStack.push(70);
            myStack.display();
            val = myStack.pop();
            System.out.print("\nPoped element is:" + val);
            val = myStack.pop();
            System.out.print("\nPoped element is:" + val);
            myStack.display(); System.out.println();

        }
    }

Output:-
--------

"C:\Program Files\Java\jdk-20\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1.2\lib\idea_rt.jar=55351:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.1.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\hp\IdeaProjects\justforfun\out\production\justforfun Jop

Stack elements are:
10,20,30,40,50,60,
Poped element is:70
Poped element is:60
Stack elements are:
10,20,30,40,

Process finished with exit code 0
-----------------------------------------------------XXXXXXXX----------------------------------------------------------------
                    -------End of Chapter 1 Basics of OOPS-------

 

JAVA PROGRAMMING - OOPS CHAPTER 1

Object-Oriented Programming Chapter 1: Basics by Souradeep Roy Using IntelliJ IDEA Community version:- _____________________________________...