Java Program to Create a Method without Parameters and with Return Type

This is a Java Program to Illustrate a Method without Parameters and With Return Type.

We have made the method to calculate the volume of a cuboid which takes the dimensions length, breadth and height as input and return the volume as output back to the main method.

Here is the source code of the Java Program to Illustrate a Method without Parameters and With Return Type. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. class Box 
  2. {
  3.     double width;
  4.     double height;
  5.     double depth;
  6.     double volume() 
  7.     {
  8.         Scanner s = new Scanner(System.in);
  9.         System.out.print("enter width : ");
  10.         double width = s.nextDouble();
  11.         System.out.print("enter height : ");
  12.         double height = s.nextDouble();
  13.         System.out.print("enter depth: ");
  14.         double depth = s.nextDouble();
  15.         return width * height * depth;
  16.     }
  17. }
  18. class ReturnDemo 
  19. {
  20.     public static void main(String args[]) 
  21.     {
  22.         Box cuboid = new Box();
  23.         double vol;
  24. 	vol = cuboid.volume();
  25.         System.out.println("Volume is " + vol);
  26.     }
  27. }

Output:

$ javac ReturnDemo.java
$ java ReturnDemo
 
enter width : 10
enter height : 2
enter depth: 23
Volume is 460.0

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.