This is a Java Program to Implement the Concept of Passing the Object and Return Object.
Enter length and breadth of rectangle as input. After that we create object of that class and pass the reference variable to two different methods. Now we calculate area for two different objects and return them to main method. Hence we get two different areas as output.
Here is the source code of the Java Program to Implement the Concept of Passing the Object and Return Object. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Object_Pass_Return
{
int length, breadth, area;
Object_Pass_Return area1(Object_Pass_Return object1)
{
object1 = new Object_Pass_Return();
object1.length = this.length;
object1.breadth = this.breadth;
object1.area = object1.length * object1.breadth;
return object1;
}
Object_Pass_Return area2(Object_Pass_Return object2)
{
object2 = new Object_Pass_Return();
object2.length = this.length + 5;
object2.breadth = this.breadth + 6;
object2.area = object2.length * object2.breadth;
return object2;
}
public static void main(String[] args)
{
Object_Pass_Return obj = new Object_Pass_Return();
Scanner s = new Scanner(System.in);
System.out.print("Enter length:");
obj.length = s.nextInt();
System.out.print("Enter breadth:");
obj.breadth = s.nextInt();
Object_Pass_Return a = obj.area1(obj);
Object_Pass_Return b = obj.area2(obj);
System.out.println("Area:"+a.area);
System.out.println("Area:"+b.area);
}
}
Output:
$ javac Object_Pass_Return.java $ java Object_Pass_Return Enter length:5 Enter breadth:6 Area:30 Area:120
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check Java Books
- Practice Programming MCQs
- Apply for Java Internship
- Practice BCA MCQs
- Apply for Computer Science Internship