Java Certification, Programming, JavaBean and Object Oriented Reference Books
Program to calculate the perimeter of a square, rectangle and triangle

programtocalculatetheperimeterofasquarerectangleandtriangle.java

// Program to calculate the perimeter of a square, rectangle and triangle

public class Shape
{
 private String shape;
 private int perimeter;
 public String m;

 // constructors

 public Shape()
 {
  setPerimeter();
  setShape("point");
 
 }

 public Shape(int x)
 {
  setPerimeter(x);
  setShape("Square");
 }

 public Shape(int x, int y)
 {
  setPerimeter(x,y);
  setShape("Rectangle");
 }

 public Shape(int x, int y, int z)
 {
  setPerimeter(x,y,z);
  setShape("Triangle");
 }

 // method to set the name of the shape
 
 public void setShape(String m)
 {
  shape = m;
 }
 
 // Methods to calculate the perimetre of the shapes

 public void setPerimeter()
 {
  perimeter=0;
 }

 public void setPerimeter(int x)
 {
  perimeter=x*4;
 }

 public void setPerimeter(int x, int y)
 {
  perimeter=(x*2)+(y*2);
 }

 public void setPerimeter(int x , int y, int z)
 {
  perimeter = x+y+z;
 }

 // methods to return the values of the perimeters

 public int getPerimeter()
 {
  return perimeter;
 }

 public String getShape()
 {
  return shape;
 }
}// ends the class

Return to : Java Programming Hints and Tips