Develop programs based on variation in methods i.e. passing by value, passing by reference, returning values and returning objects from methods.

 

AIM: Develop programs based on variation in methods i.e. passing by value, passing by reference, returning values and returning objects from methods.

CODE:

class Time{

            int h;

            Time(){

                        h=0;

            }

            Time(int h){

                        this.h=h;

            }

            Time Timeobj(Time t){

                        Time t1 = new Time();

                        t1.h = h+t.h;

                        return t1;

            }

            static int sum(int h1,int h2){

                        return h1+h2;

            }

            void display(){

                        System.out.println("Time: "+h);

            }

}

public class p18{

            public static void main(String args[]){

                        Time t1,t2,t3,t4;

                        t1 = new Time(5);

                        t2 = new Time(1);

                        t3 = new Time();

                        t4 = new Time();

                        t3 = t1.Timeobj(t2);

                        t4.h=Time.sum(t1.h,t2.h);

                        t3.display();

                        t4.display();

            }

}

                       

OUTPUT:



Comments

Popular posts from this blog

Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.

Write a program in Java to demonstrate use of final class.

Write a java program to create 3 threads using Thread class. Three threads should calculate the sum of 1 to 5, 6 to 10 and 11to 15 respectively. After all thread finishes main thread should print the sum and average.