So i got this assignment
Here what i did, i am not sure if this is what the assignment want, but this is the best method i could come up withWrite a Java program that calculates the area and volume of a cube, sphere, cylinder, and regular tetrahedron. Your program should obtain the necessary input for each of the objects from the console and output the input, area, and volume of the object. Use appropriate messages to guide the user of your program.
//Khang Le import java.util.Scanner; public class InputAreaVolume { public static void main(String[] args) { // Cube System.out.print("Enter edge measurement of cube: "); double edge = extracted().nextDouble(); double CubeArena = 6 * edge * edge; double CubeVolume = edge * edge * edge; System.out.println(" The area of the cube is: " + CubeArena); System.out.println(" The volume of the cube is: " + CubeVolume); // Sphere System.out.print("Enter radius measurement of sphere: "); double radius = extracted().nextDouble(); double SphereArena = 4 * 3.14 * radius * radius; double SphereVolume = Math.round(3.14 * radius * radius * radius * 4/3) ; System.out.println(" The area of the sphere is: " + SphereArena); System.out.println(" The volume of the sphere is: " + SphereVolume); // Cylinder System.out.print("Enter radius measurement of cylinder: "); double cylinder = extracted().nextDouble(); System.out.print("Enter height measurement of cylinder: "); double height = extracted().nextDouble(); double CylinderArena = 2 * 3.14 * cylinder * height + 2 * 3.14 * cylinder * cylinder; double CylinderVolume = 3.14 * cylinder * cylinder * height; System.out.println(" The area of the cylinder is: " + CylinderArena); System.out.println(" The volume of the cylinder is: " + CylinderVolume); // Tetrahedron System.out.print("Enter edge measurement of tetrahedron: "); double tetrahedron = extracted().nextDouble(); double TetrahedronArena = Math.round( Math.sqrt(3) * tetrahedron * tetrahedron); double TetrahedronVolume =Math.round((tetrahedron * tetrahedron * tetrahedron) / (6 * Math.sqrt(2))); System.out.println(" The area of the tetrahedron is: " + TetrahedronArena); System.out.println(" The volume of the tetrahedron is: " + TetrahedronVolume); } private static Scanner extracted() { return new Scanner(System.in); } }
What do you guys think? Did i do it right? Is there anything i could do to improve?