import java.util.Scanner; /** * This program takes a number from the user and calculates the volume of a * sphere with that radius * * @author Steven Karas */ public class Volume { public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* * I could have declared all the variables at the top. This is a * convention thing. So long as you are consistent (only doing one or * the other), it is acceptable. * * Styling conventions are used to enhance the readability of our code. * For example, single line comments are actual comments for this class. * Block comments, like these, describe alternative ways to complete the * section, or provide an indepth explanation. */ // Get the radius from the user System.out.print("Enter a radius: "); double radius = scan.nextDouble(); /* * You could have used Math.pow instead of multiplying three times. * * Note that 4.0 and 3.0 include the decimals. Otherwise it is treated * by Java as integers, and 4 / 3 as integers is 1. */ // formula for the volume of a sphere (as given) double volume = (4.0 / 3.0) * Math.PI * radius * radius * radius; /* * This could have been split up over several lines, with multiple calls * to System.out.print, instead. */ System.out.println("The volume of a sphere with radius " + radius + " is " + volume); } }