Java program to calculate the volume of a prism. we have been written the java code in three to four different formats. All of the formats are meant to be same just for a learning purpose. If you have any doubt related to the subject that we are shared over here do let us know by commenting or contacting us. We are glad to help you out #java program # #Volume of Prism #.
# Online compiler and execution tool # are added for each type of the program, under each example o code. check it out.
Table of contents :
- Example # 1 – A simple program universally applicable.
- Example # 2 – Using command line argument # may not universally applicable( subject purpose ).
- Example # 3 – Using a static method – # Universally applicable.
Before we get into the topic, we just decide to share a couple of lines about a volume of a prism. Here goes!
What is a prism?
Here is the actual and complete exact definition for you :
What is the formula in order to calculate the volume of a prism?
Since in math, there may be several ways to find the volume of a prism, the easiest formula as follows :
The volume of a prism = Area of base X Height
Note: Before calculating the volume of prism, you must calculate the Area of base
[wp_ad_camp_3]
Here is the example program # 1 with online compiler and execution tool. Make sure to check out the rest of the examples in different formats to know how the code actually works. If you were at starting level to learn the java then do subscribe to our blog. Our dedicated team will share the best of the information related to coding from a basic level to core level.
Example method # 1: Check out the execution tool as well # volume of prism java code #
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class VolumeOfPrism { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the area of base:"); double base=s.nextDouble(); System.out.println("Enter the height:"); double height=s.nextDouble(); double area=base*height ; System.out.println("Volume Of Prism is: " + area); } } |
Output :
1 2 3 4 5 |
Enter the area of base: 4.5 Enter the height: 6 Volume Of Prism is: 27.0 |
# Here is the online execution & Compiler tool for the above example program -1 #
You may get an idea. How the above code works in order to calculate the volume of a prism. Below is the complete explanation of that java code. Check only, if you were new to programming. If you know the basics of java programming or coding then skip this step and follow out to the next example that we are written as # Example method # 2.
Here is the step by step explanation :
1 |
import java.util.Scanner; |
– Scan the input variables and provide the necessary files from inbuilt JDK package. Used in the new scanner method or syntax.
1 |
class VolumeOfPrism |
– The class is a blueprint, where objects are created within a class.
1 |
public static void main(String args[]) |
– It’s a method, where your string input variables converted to arguments like as double, int, float, whatever you were written in the code.
1 |
Scanner s= new Scanner(System.in); |
– The scanner is a method that retrieves the data from the java.util.scanner and uses to read the input variables from your device(system.in).
1 |
System.out.println("Enter the area of base:"); |
– Returns the output value on the screen, whatever you have been written in the brackets.
1 |
double base=s.nextDouble(); |
– Double is a method, which can take the variables into decimal points either normal integers. Where ‘ base ‘ names to that memory, where s is a scanner function to let you know that it is an integer. Nextdouble(); represents to store the given value and cursor will move on to the second line until the program completes its execution.
1 |
double area=base*height ; |
– The formula to find out the volume of a prism in java.
1 |
System.out.println("volume of prism is: " + area); |
– The output value will be displayed over here.
So, you got an idea how the above java program to calculate the volume of a prism. Did you check out the online compiler tool? Anyways, here is another example method to calculate the volume of a prism in java. Consider the following Java code as an example method # 2.
The little difference, when compares with the above code is nothing but just a small change in syntax. Here a new method called ‘ parse ‘. Maybe the below program cannot execute on your cmd file. Check out the program.
Example program #2 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Scanner; class VolumeOfPrism2 { public static void main(String args[]) { double base=Double.parseDouble(args[0]); double height=Double.parseDouble(args[1]); double area=base*height; System.out.println("Area of prism is: " + area); } } |
Here is the online execution tool for the above program in order to calculate the volume of prism :
The above program may not work on your command line. Since the example program #1 is universally applicable to all and can execute anywhere on JDK. Below is the complete explanation of the above code, parse :
1 2 3 4 5 6 7 8 9 10 11 12 |
parse: parseXXX(variable or constant); it is a method which take a string as argument. types of parsemethod: 1:parseInt(); 2:parseDouble(); 3:parseFloat(); parseInt():it is a member function in Integer Class; parseDouble():it is a member function in Double Class; parseInt():it is a member function in Float Class; |
You may get an idea what is parsing and how the parse method works in the above java program to calculate the volume of a prism.
Here is another example program. Check it out, the following program can also universally applicable.
Example Method – # 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Scanner; class VolumeOfPrism { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the area of base:"); double base=s.nextDouble(); System.out.println("Enter the height:"); double height=s.nextDouble(); double a= VolumeOfPrism.Volume(base,height); System.out.println("Volume Of Prism is:" +a); } public static double Volume(double b,double h) { double a=b*h ; return a; } } |
How simple it was right! If you have any doubts related to the above examples and how the java program works. Do comment, we are glad to help you out related to this query.
# Java program to find the volume of prism ends #