Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a public class called Square that has 1 private data-field called side. Th

ID: 3695998 • Letter: W

Question

Write a public class called Square that has 1 private data-field called side. The type fo the data-field is double.

The class will only have one constructor that initializes a square object with a given legnth of one of its side. The class will have a getter and setter for its data-field.

The class will have two instance methods:

double getPerimeter(), where the perimeter is defined as 4*a where a is the length of 1 side.

double getArea(), where area is defined as a*a, where a is the length of 1 side.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Square {

   private double side;

   /**
   * @param side
   */
   public Square(double side) {
       this.side = side;
   }

   /**
   * @return the side
   */
   public double getSide() {
       return side;
   }

   /**
   * @param side
   * the side to set
   */
   public void setSide(double side) {
       this.side = side;
   }

   /**
   * where the perimeter is defined as 4*a where a is the length of 1 side.
   *
   * @return
   */
   public double getPerimeter() {

       return 4 * getSide();
   }

   /**
   * where area is defined as a*a, where a is the length of 1 side.
   *
   * @return
   */
   public double getArea() {
       return getSide() * getSide();
   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       try {

           Square square = new Square(3);
           System.out
                   .println("Area of square with side 3:" + square.getArea());
           System.out.println("Perimeter of square with side 3:"
                   + square.getPerimeter());
       } catch (Exception e) {
           // TODO: handle exception
       }
   }

}

OUTPUT:

Area of square with side 3:9.0
Perimeter of square with side 3:12.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote