This is a Java Program to Show the Nesting of Methods. When a method in java calls another method in the same class, it is called Nesting of methods.
Enter length, breadth and height as input. After that we first call the volume method. From volume method we call area method and from area method we call perimeter method. Hence we get perimeter, area and volume of cuboid as output.
Here is the source code of the Java Program to Show the Nesting of Methods. 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 Nesting_Methods
{
int perimeter(int l, int b)
{
int pr = 12 * (l + b);
return pr;
}
int area(int l, int b)
{
int pr = perimeter(l, b);
System.out.println("Perimeter:"+pr);
int ar = 6 * l * b;
return ar;
}
int volume(int l, int b, int h)
{
int ar = area(l, b);
System.out.println("Area:"+ar);
int vol ;
vol = l * b * h;
return vol;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter length of cuboid:");
int l = s.nextInt();
System.out.print("Enter breadth of cuboid:");
int b = s.nextInt();
System.out.print("Enter height of cuboid:");
int h = s.nextInt();
Nesting_Methods obj = new Nesting_Methods();
int vol = obj.volume(l, b, h);
System.out.println("Volume:"+vol);
}
}
Output:
$ javac Nesting_Methods.java $ java Nesting_Methods Enter length of cuboid:5 Enter breadth of cuboid:6 Enter height of cuboid:7 Perimeter:132 Area:180 Volume:210
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Check Java Books