Oops Practice

0
  1. // this is my first oops code try creat a objeact



    public class Oops {
        int a ;
        String rakesh;
        public static void main(String[] args) {
            Oops obj = new Oops();
            Oops obj1 = new Oops();
            System.out.println(obj.rakesh);
            System.out.println(obj.a);
            System.out.println(obj1.rakesh);
        }
       
    }

  2. // try to exchange object value in instance variable;



    public class oops1 {
            int Number = 25;
            String Name = "Rakesh";
            public static void main(String[] args) {
                oops1 obj = new oops1();
                obj.Name = "Pankaj";
                System.out.println(obj.Name);
                System.out.println(obj.Number);

            }
           
        }

  3. // i am using addition class in java code and its works



    class calc{
        int Number = 33;
        String Name = "rakesh";

    }
    public class oops2 {
        public static void main(String[] args) {
            calc obj = new calc();
            System.out.println(obj.Number);
            System.out.println(obj.Name);
           
        }
       
    }
  4. // this sesion i learn about over loding that how desgine a calculater and how to reuse 1 methode and how again and again perfome instance variable
    class calc {
        public int add (int a, int b){
            int result = a+b;
            return result;
        }

         public int add (int a, int b,int c ){
            int result = a+b+c;
            return result;
        }
        public double add (double a, double b, double c ){
            double result = a+b+c;
            return result;
        }

    }

    public class overloding {
        public static void main(String[] args) {
            calc obj = new calc();
       
            int result = obj.add(5,8);
            System.out.println(result);
            // this result is deffrent because instance variabel and local varial is difrrent and you use here result you shall no faca any issue
            int result1=obj.add(5,8,6);
            System.out.println(result1);
            // and this statement dedicate to perform double type you should use double type so you will allready declare double type data
            double result2 = obj.add (4.5, 8.5,8.5);
            System.out.println(result2);
           
        }
       
    }
  5. // i am using import methode using overloading
    import java.util.Scanner;
    class calculator{
        public int add(int a , int b,int c){
            int result = a+b+c;
            return result;
        }
    }
    public class overloding_input {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            int b=sc.nextInt();
            int c = sc.nextInt();
            calculator obj = new calculator();
            int result = obj.add(a,b,c);
            System.err.println(result);
            sc.close();
        }
       
    }

    //  input 85
    //        45
    //        85
    // output 215



 

Post a Comment

0Comments
Post a Comment (0)