How to Compute a Rectangle Perimeter in Java

The Java programming language contains the capacity to perform basic and advanced mathematical calculation. It even has built-in functions that can do certain special calculations, such as square roots. However, the programmer should know how to set up certain math problems before trying to implement them in code. The perimeter of a rectangle, for example, is easy to set up, and easy to implement in Java.

Things You'll Need

  • Java Development Kit (JDK)
  • Text Editor or Java Interactive Development Environment
Show More

Instructions

    • 1

      Create a rectangle class. This class will contain the necessary data for the rectangle, namely the size of its sides:

      public class Square{

      public float side1;
      public float side2;

      }

    • 2

      Create a method inside the class that calculates the perimeter of the rectangle:

      public int squarePerimeter(){
      }

    • 3

      Define the perimeter function so that it returns the perimeter. The perimeter of a square is the sum of all sides. In the case of a rectangle, where there are pairs of sides with the same length (side 1 and side 2) this means doubling each side measurement, and then adding the result:

      public int squarePerimeter(){
      return (side1 * 2) + (side2 * 2);
      }

Related Searches:

References

Comments

Related Ads

Featured