Results 1 to 2 of 2
Thread: Help Shortening Code
- 03-06-2012, 12:00 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Help Shortening Code
Hey guys this is my first time posting, I just need some feedback on shortening some java code that I wrote for my college course. The code is completed and compiles fine and we don't get extra credit if it's short or anything, I would just like some pointers on keeping my code short and succinct. Thanks for any advice. Here is the code:
Java Code:import javax.swing.JOptionPane; public class program_5 { public static void main (String [] args){ String buffer; String s1; String s2; String s3; double t1; double t2; double t3; double area; double pi = 3.14195265; //Greeting & input JOptionPane.showMessageDialog(null, "Welcome to the area calculation program! Press OK to continue", "Program", JOptionPane.INFORMATION_MESSAGE); buffer = JOptionPane.showInputDialog("Type the name of the shape you want the area of (triangle, square, or circle):"); //If triangle if (buffer.equalsIgnoreCase("triangle") || (buffer.charAt(0) == 't') || (buffer.charAt(0) == 'T')){ s1 = JOptionPane.showInputDialog("Enter the first side of the triangle:"); s2 = JOptionPane.showInputDialog("Enter the second side of the triangle:"); s3 = JOptionPane.showInputDialog("Enter the third side of the triangle:"); //convert strings to doubles t1 = Double.parseDouble(s1); t2 = Double.parseDouble(s2); t3 = Double.parseDouble(s3); //Calculate Semiperimeter and area double s = ((t1 + t2 + t3)/2); area = Math.pow((s*(s - t1)*(s - t2)*(s - t3)), .5); //errors if ((s*(s - t1)*(s - t2)*(s - t3)) <= 0) { JOptionPane.showMessageDialog(null, "Invalid input"); } //output area JOptionPane.showMessageDialog(null, "The area of the triangle specified is: " + area, "Triangle", JOptionPane.INFORMATION_MESSAGE); } //If square else if (buffer.equalsIgnoreCase("square") || (buffer.charAt(0) == 's') || (buffer.charAt(0) == 'S')) { s1 = JOptionPane.showInputDialog("Please enter the side length:"); //convert string to double t1 = Double.parseDouble(s1); //output area area = (t1 * t1); JOptionPane.showMessageDialog(null, "The area of the square with side length " + t1 + " is: " + area, "Square", JOptionPane.INFORMATION_MESSAGE); } //If circle else if (buffer.equalsIgnoreCase("circle") || (buffer.charAt(0) == 'c') || (buffer.charAt(0) == 'C')) { s1 = JOptionPane.showInputDialog("Please enter the radius"); //string to double t1 = Double.parseDouble(s1); //output area area = (pi * (t1 * t1)); JOptionPane.showMessageDialog(null, "The area of the circle with radius " + t1 + " is: " + area, "Circle", JOptionPane.INFORMATION_MESSAGE); } else { System.out.println("Not a valid input! Please try again!"); } } }
- 03-06-2012, 12:29 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Help Shortening Code
I don't know about shorter, but there are a few things I would do:
* Name the class Program5 in accordance with Java coding conventions.
* Have a separate method for each of the area calculations so that main() does not become too long
* Declare variables in the methods that use them, and as close as possible to their use.
* Not print any bogus results. For instance if s*(s-t1)*(s-t2)*(s-t3))<0 you report an error but nonetheless display an erroneous area. In fact you attempt to calculate that area which might not be nice.
* Test everything (that would have picked up the previous point)
Similar Threads
-
shortening a java program?
By exeye0h in forum New To JavaReplies: 3Last Post: 02-22-2012, 03:29 AM -
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks