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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Shape Calculation Program

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    static int Radius;

    public static int calculation() {
    area = length *width;
    RectPerim = length + length +width +width;
    SquareArea= SquareSide * SquareSide;
    CirlceArea = Radius * Radius * 3.14;
    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");
    c= JOptionPane.showInputDialog( "Enter Radius");

    calculation();

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

    SquareArea = Integer.parseInt( c );
    Radius = Integer.parseInt( d );
    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. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Last edited by newbie; January 29th, 2011 at 03:59 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM
  2. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM
  3. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM
  4. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM
  5. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM