In Java, Need to show output as well Return volume as a value (not String) from
ID: 3570484 • Letter: I
Question
In Java, Need to show output as well
Return volume as a value (not String) from a method which has 3 parameters
1)Write a program which passes 3 arguments (height, width, length) into a method.
2)The above method then computes the volume, and returns the volume to the main.
3) Show all related values on the screen.
For example, if the sides are 3 by 3 by 6, then your output should be something like:
"the volume of a 3x3x6 is 54 cubic feet."
Remember: the volume must be returned from a method with 3 parameters.
So to call the method from the main, one would write something resembling:
v = vol(sideA, sideB, sideC);
where all variables above are of the "double" data type
Explanation / Answer
//VolumeCalculator.java
import java.util.Scanner;
public class VolumeCalculator
{
public static void main(String[] args) {
//reader for reading input
Scanner reader=new Scanner(System.in);
//read three side values
System.out.println("Enter lenght :");
double sideA=reader.nextDouble();
System.out.println("Enter width :");
double sideB=reader.nextDouble();
System.out.println("Enter height :");
double sideC=reader.nextDouble();
//calling the method vol with side values
double volume=vol(sideA, sideB, sideC);
System.out.println("The volume of a "+sideA+"*"+sideB+"*"+sideC+"is "+volume+
"cubic feet.");
}
//The method vol accepts three arguments and returns the volume
private static double vol(double length, double width, double height)
{
return length*width*height;
}
}
------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter lenght :
3
Enter width :
3
Enter height :
6
The volume of a 3.0*3.0*6.0is 54.0cubic feet.
Hope this helps
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.