Object passing as a argument in Java Program

How to object passing works in java program?

– In some example, we need merge calculation of more than one object data, that time we can use object passing feature in our program.

In this blog program we calculate one company’s two years profit, investment and total income calculation.

 

 

Let’s see example on Object passing.

class Company

{

            double totalInc;

            double profit,inv;

 

            void setData(double totalInc,double inv)

            {

                        this.totalInc  = totalInc;

                        this.inv = inv;

                        this.profit = this.totalInc – this.inv;

            }

            void display()

            {

                        System.out.println(“Total Income : ” + this.totalInc);

                        System.out.println(“Inverstment : ” + this.inv);

                        System.out.println(“Profit : ” + this.profit);

            }

            void TotalProfitOfTwoYear(Company obj1,Company obj2)

            {

                        double TotalProfit = 0;

                        TotalProfit = obj1.profit + obj2.profit; // Calculation of two year profits

                        System.out.println(“Profit Of Two Year – ” + TotalProfit);

                        // You can also find Maximum Profit , Min , Average   

            }

}

class clEx7

{

            public static void main(String args[])

            {

                        Company obj2014 = new Company();

                        Company obj2015 = new Company();

 

                        obj2014.setData(1200000,400000);

                        obj2015.setData(1500000,450000);

 

                        System.out.println(“=== Record Of 2014 ===”);

                        obj2014.display();

                        System.out.println(“=== Record Of 2015 ===”);

                        obj2015.display();

 

                        // Now Convert into Object Passing Program

                        Company TotalTemp = new Company();

                        TotalTemp.TotalProfitOfTwoYear(obj2014,obj2015);  // Pass the object using methods        

 

            }

}

 

Output:

=== Record of 2014 ===

Total Income : 1200000

Investment : 400000

Profit : 800000

=== Record of 2015 ===

Total Income : 1500000

Investment : 450000

Profit : 1050000

Profit of Two Year – 1850000

 

In this blog, we use obj2014 and obj2015 as an argument passing in TotalProfitOfTwoYear(obj2014,obj2015) method.

 

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

575total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint