Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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);

}
}

2. ## 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.