# Shape Calculation Program

• January 29th, 2011, 12:32 PM
TH1
Shape Calculation Program
I am writing a java program where that calculates the area and perimeter for a rectangle, square, circle and triangle. The user enters the length and width for the rectangle, the sides for the square, radius for the circle, base and height for the triangle, and it will output 4 windows displaying the area and perimeter for each shape

I have got it working for the area and perimeter of a rectangle, but I cant get it to work for the others since for the square it gives the same value I enter in, any help would be appreciated

/**
* @(#)RectangleArea.java
*
*
* @author
* @version 1.00 2011/1/28
*/
import javax.swing.JOptionPane;

public class RectangleArea {
static String a,b,c,d;
static int length;
static int width;
static int area;
static int SquareSide;
static int SquareArea;
static int RectPerim;
static int CirlceArea;

public static int calculation() {
area = length *width;
RectPerim = length + length +width +width;
SquareArea= SquareSide * SquareSide;
return area; return CirlceArea ;
}

public static int SquareCalculation(){
SquareArea= SquareSide * SquareSide;
return SquareArea;
}
public static void main(String args[]) {
calculation();

a= JOptionPane.showInputDialog ("Enter Width");
b= JOptionPane.showInputDialog( "Enter length");
d= JOptionPane.showInputDialog( "Enter SquareSide");

calculation();

length = Integer.parseInt( a );
width = Integer.parseInt( b );

SquareArea = Integer.parseInt( c );
calculation();

JOptionPane.showMessageDialog(null,"Area of rectangle is "+ area);
JOptionPane.showMessageDialog(null,"Perimeter of rectangle is "+ RectPerim);
JOptionPane.showMessageDialog(null,"Area of circle is "+ CirlceArea);
JOptionPane.showMessageDialog(null,"Area of Square is "+ SquareArea);

}
}
• January 29th, 2011, 02:54 PM
newbie
Re: Shape Calculation Program
I haven't tested your code, but I noticed that you're attempting to return 2 values from your calculation() method, which means that only your area variable will be returned, and your CirlceArea variable will never be reached as 1 method can only return 1 value. Does this seem to be cause of your problem?

Off-topic: Please edit your code to include code tags (see my signature), and for good practice sakes, try and follow the convention by starting variable names with lower case letters.

-Never mind, doubt it's what's causing your problems, just noticed you aren't using the method for return purposes, so calculation() might as well not have a return type.