critique my code and see if i can make improvements please
package project8;
import javax.swing.JOptionPane;
/**
*
Title: Project 8
*
*
Description: Find the area of the shaded space inside an ellipse using methods
*
*
10/23/05
*
*
ITEC 1301
*
* @Robert O'Mallon
*
*/
publicclass Project8{
publicstaticvoid main(String[] args){
// Gather information for the two triangle eyes
String input = JOptionPane.showInputDialog(
"Enter side A of the eye triangles");
double a = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side B of the eye triangles");
double b = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side C of the eye triangles");
double c = Double.parseDouble(input);
// Call the triangle formula method
double triA = triangle(a, b, c);
// Gather the information for the nose triangle
input = JOptionPane.showInputDialog(
"Enter side A of the nose triangle");
double d = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side B of the nose triangle");
double e = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side C of the nose triangle");
double f = Double.parseDouble(input);
// Call the triangle method to calculate the area
double triB = triangle(a, b, c);
// Gather the information for the mouth ellipse
input = JOptionPane.showInputDialog(
"Enter side A of the mouth ellipse");
double ellipseA = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side B of the mouth ellipse");
double ellipseB = Double.parseDouble(input);
// Call the ellipse method to calculate the ellipse area
double mouthArea = ellipse(ellipseA, ellipseB);
// Gather the information for the face ellipse
input = JOptionPane.showInputDialog(
"Enter side A of the ellipse");
double elliA = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter side B of the ellipse");
double elliB = Double.parseDouble(input);
// Call the ellipse method to calculate the ellipse area
double faceArea = ellipse(elliA, elliB);
// Calculate the area of the shaded region of the face
double output = (faceArea - (triA + triA + triB + mouthArea));
// Display the final output
System.out.println(output);
System.exit(0);
}
// Create a method using user-defined values to calculate the area of a triangle
publicstaticdouble triangle(double a,double b,double c){
double s = (a + b + c) / 2;
double finalA = Math.sqrt(s * (s - a) * (s - b) * (s - c));
return finalA;
}
// Create a method using user-defined values to calculate the area of an ellipse
publicstaticdouble ellipse(double a,double b){
double ellipseArea = 3.14 * a * b;
return ellipseArea;
}
}
[5175 byte] By [
zalmoxisa] at [2007-10-2 3:28:16]

public class Project8 {
// prompt for th euser input of a double and check to make sure the input is a double
/*
* Prompt the user for an input of type double and returns the user input as a double.Note: the user will be forced to enter a double value.
* @param prompt the prompt of the Input Dialog
* @return the user input
*/
public double getInput(String prompt){
boolean invalidInput = true;
double value = 0.0d;
while (invalidInput){
try{
String input = JOptionPane.showInputDialog(prompt);
value = Double.parseDouble(input);
invalidInput = false;
}
catch (NumberformatException ne){
JOptionPane.showMessageDialog(null, "Invalid input", "Invalid input.", JOptionPane.ERROR_MESSAGE);
}
}
return value;
}
public static void main(String[] args) {
double sideA, sideB, sideC;
// eye triangle calculation
sideA = getUserInput("Enter side A of the eye triangles");
sideB = getUserInput("Enter side B of the eye triangles");
sideC = getUserInput("Enter side C of the eye triangles");
double eyeTriangleArea = calcTriangleArea(sideA, sideB, sideC);
// nose triangle calculation
sideA = getUserInput("Enter side A of the nose triangle");
sideB = getUserInput("Enter side B of the nose triangle");
sideC = getUserInput("Enter side C of the nose triangle");
double noseTriangleArea = calcTriangleArea(sideA, sideB, sideC);
// mouth ellipse calculation
sideA = getUserInput( "Enter side A of the mouth ellipse");
sideB = getUserInput("Enter side B of the mouth ellipse");
double mouthEllipseArea = calcEllipseArea(sideA, sideB);
// face ellipse calculation
sideA = getUserInput( "Enter side A of the ellipse");
sideB = getUserInput("Enter side B of the ellipse");
double faceEllipseArea = calcEllipseArea(sideA, sideB);
// Calculate the area of the shaded region of the face
double shadedRegionArea = (faceEllipseArea - (eyeTriangleArea * 2 + noseTriangleArea + mouthEllipseArea));
System.out.println("Area of the shaded region of the face: " + shadedRegionArea);
}
/*
* Calculates and returns the area of the triangle
* @param a the side of the triangle
* @param b the side of the triangle
* @param c the side of the triangle
* @return the area of the triangle basd on the specified sides given.
*/
public static double triangle(double a, double b, double c) {
double s = (a + b + c) / 2;
return (Math.sqrt(s * (s - a) * (s - b) * (s - c)));
}
/*
* Calculate and returns the area of an ellipse.
* @param a the side of the triangle
* @param b the side of the triangle
* @return the area of the ellipse based on the sides given.
*/
private static double ellipse(double a, double b) {
return (3.14 * a * b);
}
}
just an example. basically, you want to name your variables and method meaningfull. this will make it much more easier to read and understand your code. Also, good method name and variables often will eliminated may comments.
like calcTriangleArea() as oppse to triangle();
triangle()...are you returning a triangle, displaying a triangle, calculate triangle perimeter or area? you don't really know unless you look at the code or API( if the java doc has any description of the method)
by refactoring out common code, you make it more modularize (put it in a method)..and the code become more cleanner and easier to read...just make sure the method name matches what the method is doing.
usually, you should only comment a complex algorithm or part of the code that is not easily understand by you or someone else. or something like..NOTE: this method may throw NumberFormattedException if the user input is not a double.
Since this is a simple little application..i don't think it is necessarily to create new classes..although in larger application, you would create a Triangle, Ellipse class etc..
your main() method is usually just to kick of the application..not doing everything in it.