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.
class Box
{
double width;
double height;
double depth;
double volume()
{
Scanner s = new Scanner(System.in);
System.out.print("enter width : ");
double width = s.nextDouble();
System.out.print("enter height : ");
double height = s.nextDouble();
System.out.print("enter depth: ");
double depth = s.nextDouble();
return width * height * depth;
}
}
class ReturnDemo
{
public static void main(String args[])
{
Box cuboid = new Box();
double vol;
vol = cuboid.volume();
System.out.println("Volume is " + vol);
}
}
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
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Programming Books
- Apply for Java Internship