patternjavaMinor
Geometry Calculations - Cube, Sphere, and Tetrahedron
Viewed 0 times
calculationscubegeometryandspheretetrahedron
Problem
This is the first code I've written. It's for an intro to Java course. Could you look over it and let me know of things I could improve before handing it in?
For reference, I've also provided what I'm being graded on at the bottom.
```
import java.util.Scanner;
public class LB01Smith {
public static void main(String[] args) {
System.out.println("This program computes the volume and surface area of a sphere, cube, and tetrahedron using a given dimension.");
System.out.println("Next, the program will compute the dimension of a sphere, cube, and tetrahedron using a given volume.");
Scanner input = new Scanner(System.in);
String usersName;
String upperCaseName;
System.out.print("Please enter your name, then press ENTER: ");
usersName = input.nextLine();
upperCaseName = usersName.toUpperCase();
System.out.println("Hello, " + upperCaseName + ", nice to meet you!");
System.out.print(" ");
double length = 0;
double volume = 0;
double cubeVolume = 0;
double cubeSurfaceArea = 0;
double sphereVolume = 0;
double sphereSurfaceArea = 0;
double tetVolume = 0;
double tetSurfaceArea = 0;
double cubeSide = 0;
double sphereDiameter = 0;
double tetSide = 0;
final double PI = Math.PI;
final double SQRT_2 = Math.sqrt(2);
final double SQRT_3 = Math.sqrt(3);
System.out.print("Please enter a dimension for volume calculation, then press ENTER: ");
length = input.nextDouble();
cubeVolume = (length length length);
cubeSurfaceArea = 6 (length length);
sphereVolume = PI / 6 (length length * length);
sphereSurfaceArea = 4 PI ((length / 2) * (length / 2));
For reference, I've also provided what I'm being graded on at the bottom.
```
import java.util.Scanner;
public class LB01Smith {
public static void main(String[] args) {
System.out.println("This program computes the volume and surface area of a sphere, cube, and tetrahedron using a given dimension.");
System.out.println("Next, the program will compute the dimension of a sphere, cube, and tetrahedron using a given volume.");
Scanner input = new Scanner(System.in);
String usersName;
String upperCaseName;
System.out.print("Please enter your name, then press ENTER: ");
usersName = input.nextLine();
upperCaseName = usersName.toUpperCase();
System.out.println("Hello, " + upperCaseName + ", nice to meet you!");
System.out.print(" ");
double length = 0;
double volume = 0;
double cubeVolume = 0;
double cubeSurfaceArea = 0;
double sphereVolume = 0;
double sphereSurfaceArea = 0;
double tetVolume = 0;
double tetSurfaceArea = 0;
double cubeSide = 0;
double sphereDiameter = 0;
double tetSide = 0;
final double PI = Math.PI;
final double SQRT_2 = Math.sqrt(2);
final double SQRT_3 = Math.sqrt(3);
System.out.print("Please enter a dimension for volume calculation, then press ENTER: ");
length = input.nextDouble();
cubeVolume = (length length length);
cubeSurfaceArea = 6 (length length);
sphereVolume = PI / 6 (length length * length);
sphereSurfaceArea = 4 PI ((length / 2) * (length / 2));
Solution
Your program declares and uses constants
Generally constants are declared at the class level rather that the method level.
followed the programming style ... spaces, indentation, and blank lines
I'm fairly certain the extra tabs halfway through your main method are side-effects of Markdown, otherwise fix them to match the rest of the method.
Your string literals in
If your class has not covered it, you can find a lot of useful information on formatting output here.
Some coding standards (and languages) require you to place all variable declarations at the top of the scope (class, method or block) while others (again, like my team's) prefer the declaration where it is first used. Find out which style the class prescribes and stick to it. Having a large block of declaration after all had output code is confusing.
Generally constants are declared at the class level rather that the method level.
followed the programming style ... spaces, indentation, and blank lines
I'm fairly certain the extra tabs halfway through your main method are side-effects of Markdown, otherwise fix them to match the rest of the method.
Your string literals in
System.out.print(ln) are excessively long. Older coding styles dictate 80 characters per line, personally, my team has agreed on 140 cpl. Going much beyond that leads to a lot of head shaking when trying the read the code. It's okay to split string literals with new lines and the + operator. javac will compile out the concatenation. Also, call System.out.println without the empty string to end a line or output a blank line. e.g:System.out.println("This is a really long string " +
"literal that the compiler will combine. " +
"Just don't forget the trailing spaces.");
System.out.println();If your class has not covered it, you can find a lot of useful information on formatting output here.
Some coding standards (and languages) require you to place all variable declarations at the top of the scope (class, method or block) while others (again, like my team's) prefer the declaration where it is first used. Find out which style the class prescribes and stick to it. Having a large block of declaration after all had output code is confusing.
Code Snippets
System.out.println("This is a really long string " +
"literal that the compiler will combine. " +
"Just don't forget the trailing spaces.");
System.out.println();Context
StackExchange Code Review Q#96042, answer score: 4
Revisions (0)
No revisions yet.