Thursday, November 10, 2022

CONSTRUCTURE IN PARENT CLASS


class Parent

{

    public Parent()

    {

        System.out.println("Parent Constrcutor");

    }

            

}


class Child extends Parent

{

    public Child()

    {

        System.out.println("Child Constructor");

    }

}


class GrandChild extends Child

{

    public GrandChild()

    {

        System.out.println("Grand Child Constructor");

    }


public static void main(String[] args) 

    {

        GrandChild c=new GrandChild();        

    }

    

} 

ARRAY OF OBJECTS

 import java.lang.*;

class Subject

{

    private String subID;

    private String name;

    private int maxMarks;

    private int marksObtains;

    

    public Subject(String subID,String name,int maxMarks)

    {

        this.subID=subID;

        this.name=name;

        this.maxMarks=maxMarks;

    }

    

    public String getSubID(){return subID;}

    public String getName(){return name;}

    public int getMaxMarks(){return maxMarks;}

    public int getMarksObtains(){return marksObtains;}

    

    public void setMaxMarks(int mm)

    {

        maxMarks=mm;

    }

    

    public void setMarksObtain(int m)

    {

        marksObtains=m;

    }

    

    boolean isQualified()

    {

        return marksObtains>=maxMarks/10*4;

    }

    

    public String toString()

    {

        return"\n SubjectID: "+subID+"\n Name "+name+"\n MarksObtained "+marksObtains;

    }

    

}


public class SCLoops {


    public static void main(String[] args) 

    {

        Subject subs[]=new Subject[3];

        subs[0]=new Subject("s101","DS",100);

        subs[1]=new Subject("s102","Algorithms",100);

        subs[2]=new Subject("s103","Operating Systems",100);

        

        for(Subject s:subs)

            System.out.println(s);

    }

    

}

Wednesday, November 9, 2022

DATA HANDLING

 class Rectangle

{

    private double length;

    private double breadth;

    public double getLength()

    {

        return length;

    }

    public double getBreadth()

    {

        return breadth;

    }

    public void setLength(double l)

    {

        if(l>=0)

            length=l;

        else

            length=0;

    }

    public void setBreadth(double b)

    {

        if(b>=0)

            breadth=b;

        else 

            breadth=0;

    }

    public double area()

    {

        return getLength()*getBreadth();

    }

    public double perimeter()

    {

        return 2*(length+breadth);

    }

    public boolean isSquare()

    {

        if(length==breadth)

            return true;

        else 

            return false;

    }

    public static void main(String[] args) {

        Rectangle r=new Rectangle();

        r.setLength(10.5);//check with negative values.

        r.setBreadth(5.5);

        System.out.println("Area "+r.area());

        

        System.out.println("Perimeter "+r.perimeter());

        

        System.out.println("Is Square "+r.isSquare());

        

        System.out.println("Length "+r.getLength());

        

        System.out.println("Breadth "+r.getBreadth());

    }

}

CLASS AS CIRCLE

 


class Circle 

{

    public double radius;

    

    public double area()

    {

        return Math.PI*radius*radius;

    }

    public double perimeter()

    {

        return 2*Math.PI*radius;

    }

    public double circumference()

    {

        return perimeter();

    }

            

    



    public static void main(String[] args) {

     Circle c1=new Circle();

     c1.radius=7;

     System.out.println("Area:"+c1.area());

     System.out.println("Perimeter:"+c1.perimeter());

     System.out.println("Circumference:"+c1.circumference());

    }

    

}

RECURSION

 public class Main {

  public static void main(String[] args) {

    int result = sum(10);

    System.out.println(result);

  }

  public static int sum(int k) {

    if (k > 0) {

      return k + sum(k - 1);

    } else {

      return 0;

    }

  }

}

VARIABLE ARGUMENTS

 


public class MethodPractice {

    

    /*static int max(int x,int y)

    {

        if(x>y)

            return x;

        else 

            return y;

    }*/

    

    static void inc(int x)

    {

        x++;

        System.out.println(x);

    }

    

    public static void main(String[] args) {

    

        /*int a=10,b=15;

        //System.out.println(max(a,b));

        

        MethodPractice mp=new MethodPractice();

        System.out.println(mp.max(a,b));*/

        

        int a=10,b=15;

        inc(a);

        System.out.println(a);

        

        

    }

    }

    

Tuesday, November 8, 2022

PASSING OBJECTS

 


public class SCMethod1 

{

    

     int gcd(int m,int n)

    {

        while(m!=n)

        {

            if(m>n)m=m-n;

            else n=n-m;

        }

        return m;

    }

            

    public static void main(String[] args) 

    {

        SCMethod1 x=new SCMethod1();

        System.out.println(x.gcd(35,56));

        

        

        

        

    } 

        

    static boolean isPrime(int n)

    {

        for(int i=2;i<n/2;i++)

        {

            if(n%i==0)

                return false;

        }

        return true;

        

    }


}

Saturday, November 5, 2022

PASSING AS A OBJECT PARAMETERS

 public class MethodPractice1 {

    static void change(int A[],int index,int value)

    {

        A[index]=value;

    }

    static void change2(int x,int value)

    {

        x=value;

    }

    public static void main(String[] args) {

        int A[]={2,4,6,8,10};

        

        change(A,2,20);

        

        for(int x:A)

        {

            System.out.println(x);

        }

        

        int x=10;

        

        change2(x,20);

        

        System.out.println("Value of primitive :"+x);

    }

    

}

METHODS 1

 class test

{

 static int max(int x, int y)

 {

 x++;

 if(x>y)

 return x;

 else

 return y;

 }

 public static void main(String args[])

 {

 int a=10,b=15,c;

 c=max(a,b);

 System.out.println(c);

 }

}

METHODS

 


public class MethodPractice {

    

    /*static int max(int x,int y)

    {

        if(x>y)

            return x;

        else 

            return y;

    }*/

    

    static void inc(int x)

    {

        x++;

        System.out.println(x);

    }

    

    public static void main(String[] args) {

    

        /*int a=10,b=15;

        //System.out.println(max(a,b));

        

        MethodPractice mp=new MethodPractice();

        System.out.println(mp.max(a,b));*/

        

        int a=10,b=15;

        inc(a);

        System.out.println(a);

        

        

    }

    }

    

TWO DIMENSIONAL ARRAY

 public static void main(String[] args) 

    {

        int A[][]=new int[5][5];

        int B[][];

        B=new int[5][5];

        int [][]C=new int[5][5];

        int []D[]=new int[5][5];

        int[] E[],F;

        E=new int[5][5];

        F=new int[5];

        int[] G,H,I;

        

        int M[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

        int X[][];

        X=new int[3][];

        X[0]=new int[5];

        X[1]=new int[3];

        X[2]=new int[8];

        for(int x[]:M)

        {

            for(int y:x)

            {

                System.out.print(y+" ");

            }

            System.out.println("");

        }

    }

}

INSERTING A ELEMENT IN ARRAY

 


public class Array 

{

    public static void main(String[] args) 

    {

        int A[]=new int[10];

        A[0]=3;A[1]=9;A[2]=7;A[3]=8;A[4]=12;A[5]=6;

        

        int n=6;

        

        for(int i=0;i<n;i++)

            System.out.print(A[i]+",");

        System.out.println("");

     

        int x=20;

        int index=2;

        

        for(int i=n;i>index;i--)

            A[i]=A[i-1];

        A[index]=x;

        

        for(int i=0;i<n;i++)

            System.out.print(A[i]+",");

        System.out.println("");

    }

    }

ONE DIMESNISINAL ARRAY

 


public class SCArray1 

{

    public static void main(String[] args) 

    {

        int A[]={3,9,7,8,12,6,15,5,4,10};

        int max1,max2;

        max1=max2=A[0];

        for(int i=0;i<A.length;i++)

        {

            if(A[i]>max1)

            {

                max2=max1;

                max1=A[i];

            }

            else if(A[i]>max2)

            {

                max2=A[i];

            }

        }

        System.out.println("Second Largest is "+max2);

        

    }   

        

    }

Friday, November 4, 2022

LOOPS

 public class Patterns1 {


    public static void main(String[] args) {

        for(int i=1;i<5;i++)

            {

                for(int j=1;j<5;j++)

                {

                    if(i+j>5)

                        System.out.print("* ");

                    else

                        System.out.print("  ");

                }

                System.out.println("");

            }

    }

    

}

CONDITIONAL OPERATORS USING

 


import java.util.*;


public class SCCondition3 

{

    

     public static void main(String[] args) 

    {

        Scanner scan=new Scanner(System.in);

        

        System.out.println("Enter a URL");

        String url=scan.nextLine();

     

        String protocol=url.substring(0, url.indexOf(":"));

        

        if(protocol.equals("http"))

            System.out.println("Hypertext Transfer Protocol");

        else if(protocol.equals("ftp"))

            System.out.println("File Transfer Protocol");

        

        

        String ext=url.substring(url.lastIndexOf(".")+1);

        

        if(ext.equals("com"))

            System.out.println("Commercial");

        else if(ext.equals("org"))

            System.out.println("Organisation");

        else if(ext.equals("net"))

            System.out.println("Network");

            

    }   

        

    }

STRING IN JAVA


public class StringPractice2 {


    public static void main(String[] args) {

        

        /*

        String str1="Mr. Shahrukh Khan";

        System.out.println(str1.startsWith("Mr."));

        System.out.println(str1.startsWith("Shah" , 4));

        System.out.println(str1.endsWith("Khan"));

        System.out.println(str1.charAt(4));

        

        for(int i=0;i<str1.length();i++)

            System.out.print(str1.charAt(i));

        */

        

        String str1="www.udemy.co.in";

        System.out.println(str1.indexOf("."));

        System.out.println(str1.indexOf(".",4));

        System.out.println(str1.indexOf("udemy"));

        System.out.println(str1.lastIndexOf("."));

    }

    

}


HOW MANY WAYS WE CAN PRINT OUTPUT

 import java.util.*;


public class Printing {

    public static void main(String[] args) {

        

        /*int a=10;

        float b=12.55f;

        char c='A';

        String str="Hello";

        

        System.out.println(a);

        System.out.println(b);

        System.out.println(c);

        System.out.println(str);*/

        

        

        int x=10,y=20;

        System.out.println(x+y);

        System.out.println("Number is " +y);

        System.out.println(x+y+" sum");

        System.out.println("sum "+(x+y));

        System.out.println("sum of "+x+" and "+y+" is "+(x+y));

    }

    

}

Thursday, November 3, 2022

MASKING AND SWAPPING

public class Bitwise {


    /*

    public static void main(String[] args) 

    {

      int a=10, b=15;

      

      a=a^b;

      b=a^b;

      a=a^b;

      

      System.out.println(a+ " "+b);

    }

    */

    

    /*

    public static void main(String[] args) 

    {

      byte a=9, b=12;

      

      byte c;

      

      c=(byte)(a<<4);

      c=(byte)(c|b);

      

      System.out.println((c&0b11110000)>>4);

      System.out.println((c&0b00001111)>>4);

      

    }

    */

    

    

    public static void main(String[] args) 

    {

      

      

      byte c;

      

      c=(byte)(9<<4);

      c=(byte)(c|12);

      

      System.out.println((c&0b11110000)>>4);

      System.out.println((c&0b00001111)>>4);

      

    }

}


OPERATORS AND EXPRESSION

 public class Arithematic {


    public static void main(String[] args) {

        /* for integer values

        int a=14,b=5;

        int c=a/b;

        int r=a%b;

        System.out.println(c);

        System.out.println(r);*/

        /*for float values

        float a=14.3f,b=3.2f;

        float c=a/b;

        float r=a%b;

        System.out.println(c);

        System.out.println(r);*/

        /* for different data types

        byte a=10;

        short b=15;

        int c=a+b;

        float a=12.5f;

        long b=1231;

        float c=a*b;

        float a=12.5f;

        double b=123;

        double c=a*b;

        char a=40;

        int b=30;

        int c=a-b;

        System.out.println(c);

        */

        System.out.println((10+20)/2);

        

        System.out.println(10/(2*5));

    }

    

}

RANGE OF VARIABLE

 public class DataSizeRange {


    public static void main(String[] args) {

        System.out.println("FLoat Min: "+Float.MIN_VALUE);

        System.out.println("Float Max: "+Float.MAX_VALUE);

        System.out.println("Float Bytes :"+Float.BYTES);

    }

    

}

JAVA FIRST PROGRAM

 import java.util.*;


public class First {


    public static void main(String[] args) {

        String name;

        

        Scanner sc=new Scanner(System.in);

        System.out.println("May I know your Name: ");

        name=sc.nextLine();

        

        System.out.println("Welcome "+name);

    }

    

}

Wednesday, November 2, 2022

BINARY TREE

 //Build a Tree from its Preorder traversal

 

public class BinaryTreesYT {

   static class Node {

       int data;

       Node left;

       Node right;

 

       Node(int data) {

           this.data = data;

           this.left = null;

           this.right = null;

       }

   }

 

   static class BinaryTree {

       static int idx = -1;

       public static Node buildTree(int nodes[]) {

           idx++;

           if(nodes[idx] == -1) {

               return null;

           }

           Node newNode = new Node(nodes[idx]); 

           newNode.left = buildTree(nodes);

           newNode.right = buildTree(nodes);

           return newNode;

       }

   }

 

   public static void main(String args[]) {

       int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1};

       BinaryTree tree = new BinaryTree();

      

       Node root = tree.buildTree(nodes);

       System.out.println(root.data);

   }

}

public static void preorder(Node root) {

       if(root == null) {

           System.out.print(-1+" ");

           return;

       }

       System.out.print(root.data+" ");

       preorder(root.left);

       preorder(root.right);

   }

public static void inorder(Node root) {

       if(root == null) {

           System.out.print(-1+" ");

           return;

       }

       inorder(root.left);

       System.out.print(root.data+" ");

       inorder(root.right);

   }


public static void postorder(Node root) {

       if(root == null) {

           System.out.print(-1+" ");

           return;

       }

       postorder(root.left);

       postorder(root.right);

       System.out.print(root.data+" ");

   }

public static void levelOrder(Node root) {

       if(root == null) {

           return;

       }

       Queue<Node> q = new LinkedList<>();

       q.add(root);

       q.add(null);

       while(!q.isEmpty()) {

           Node curr = q.remove();

           if(curr == null) {

               System.out.println();

               //queue empty

               if(q.isEmpty()) {

                   break;

               } else {

                   q.add(null);

               }

           } else {

               System.out.print(curr.data+" ");

               if(curr.left != null) {

                   q.add(curr.left);

               }

               if(curr.right != null) {

                   q.add(curr.right);

               }

           }

       }

   }

public static int height(Node root) {

       if(root == null) {

           return 0;

       }

 

       int leftHeight = height(root.left);

       int rightHeight = height(root.right);

       return Math.max(leftHeight, rightHeight) + 1;

}

public static int countOfNodes(Node root) {

       if(root == null) {

           return 0;

       }

 

       int leftNodes = countOfNodes(root.left);

       int rightNodes = countOfNodes(root.right);

       return leftNodes + rightNodes + 1;

   }

public static int sumOfNodes(Node root) {

       if(root == null) {

           return 0;

       }

 

       int leftSum = sumOfNodes(root.left);

       int rightSum = sumOfNodes(root.right);

       return leftSum + rightSum + root.data;

   }

public static int diameter(Node root) {

       if(root == null) {

           return 0;

       }

 

       int diam1 = height(root.left) + height(root.right) + 1;

       int diam2 = diameter(root.left);

       int diam3 = diameter(root.right);

 

       return Math.max(diam1, Math.max(diam2, diam3));

   }

public static TreeInfo diameter(Node root) {

       if(root == null) {

           return new TreeInfo(0, 0);

       }

 

       TreeInfo leftTI = diameter(root.left);

       TreeInfo rightTI = diameter(root.right);

 

       int myHeight = Math.max(leftTI.height, rightTI.height) + 1;

 

       int diam1 = leftTI.height + rightTI.height + 1;

       int diam2 = leftTI.diam;

       int diam3 = rightTI.diam;

 

       int myDiam = Math.max(diam1, Math.max(diam2, diam3));

 

       return new TreeInfo(myHeight, myDiam);

   }


public boolean isIdentical(TreeNode root,TreeNode subRoot){

       if(subRoot == null && root == null){

           return true;

       }

       if(root == null || subRoot == null){

           return false;

       }

       if(root.val == subRoot.val){

           return isIdentical(root.left, subRoot.left) && isIdentical(root.right, subRoot.right);

       }

       return false;

   }

  

   public boolean isSubtree(TreeNode root, TreeNode subRoot) {

       if(subRoot == null){

           return true;

       }

       if(root == null){

           return false;

 

       }

       if(isIdentical(root, subRoot)){

           return true;

       }

       return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);

   }


PREPORCESSOR DETECTIVE

 #include <iostream>

using namespace std;

#define max(x,y) (x>y?x:y)

#define msg(x) #x

#ifndef PI

 #define PI 3.1425

#endif

int main()

{

 cout<<PI;

 cout<<max(10,12)<<endl;

 cout<<msg(hello)<<endl;

}

SYNTAX OF TRY , CATCH AND THROW

 # include<iostream>

using namespace std;


class myexception1: exception

{

};

class myexception2:public my exception1

{

};

int main()

{

try

{

throw myexception1();

}

catch(myexception2 e)

{

cout<<"int catch"<<endl;

}

catch(myexception1 e)

{

cout<<double catch"<<endl;

}

catch(...)

{

cout<<all catch<<endl;

}

}

Try and catch

 # include<iostream>

using namespace std;


int main()

{

int x,y,z;

cout<<"enter x:";

cin>>x;

cout<<"enter y:";

cin>>y;

cout<<"enter z:";

cin>>z;

try

{

if(y==0)

throw 1;

z=x/y;

cout<<z<<endl;

}

catch(int e)

{

cout<<"division by zero :"<<e<<endl;

}

cout<<"bye"<<endl;

}

CONSTRUCTURE IN PARENT CLASS

class Parent {     public Parent()     {         System.out.println("Parent Constrcutor");     }              } class Child extend...