0
Can you help me with this code, it's confusing to me
The volume of objects are calculated differently depending on the shape of the object. a) The volume of rectangular prism is calculated using the formula: Create a Volumes application that prompts the user for the length, width, and height of a rectangular prism and then calculates the volume. b) The volume of a sphere is calculated using the formula:
13 Answers
+ 2
I really don't know why, but 'voilà':
import java.util.Scanner;
public class Volumes
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Rectangular prism size:\nLength?");
double L=sc.nextDouble();
System.out.println("- Width?");
double W=sc.nextDouble();
System.out.println("- Height?");
double H=sc.nextDouble();
System.out.println("Volume = "+rectPrismVol(L,W,H));
System.out.println("Sphere size:\nRadius?");
double R=sc.nextDouble();
System.out.println("Volume = "+sphereVol(R));
}
static public double rectPrismVol(double x, double y, double z) {
return x*y*z;
}
static public double sphereVol(double r) {
return (4.0/3)*Math.PI*Math.pow(r,3);
}
}
Code in action at: https://code.sololearn.com/cbU1ePs2mFSO/#java
+ 1
And what exactly is confusing you? Do you have any code so far? If not, you might want to start by making two functions, one for calculating the volume of the prism and the other one for the sphere.
+ 1
But you have basic knowledge of the Java syntax and how a program is structured, right?
+ 1
Ok, you should try to get to 'Classes and Objects', because this will teach you how to use functions. Or you can go the lazy rout and just put everything into the main method.
+ 1
But if you learn it now you will be able to write the code yourself in the future. It's really not that hard!
+ 1
@Roborine: "Ok, you should try to get to 'Classes and Objects', because this will teach you how to use functions. Or you can go the lazy rout and just put everything into the main method."
@Akon: "I don't have any, its a new topic to me"
...
@Akon: "I tried and its just not working"
@Roborine: "If you show me your code I can help you figure out what's the problem :D"
@Akon: "thats what im saying, i dont have a code yet"
Well, you mostly said that you "just need the code"... But you also said have trying and "its" just not working, meaning "the code": so you have at least one, not yet working... Show us, and we'll help you to succeed to make "its" work :P
0
I don't have any, its a new topic to me
0
A little bit I just started this course
0
I tried and its just not working
0
If you show me your code I can help you figure out what's the problem :D
0
thats what im saying, i dont have a code yet
- 1
??
I just need the code
- 1
I don't have it, its gone, I forgot to save it