Results 1 to 2 of 2
  1. #1
    GregoryNeal is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default 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);
       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!");

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default 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

  1. shortening a java program?
    By exeye0h in forum New To Java
    Replies: 3
    Last Post: 02-22-2012, 03:29 AM
  2. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts