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

Chapter 9 Exercise 9, Introduction to Java Programming, Tenth Edition Y. Daniel

ID: 3841237 • Letter: C

Question

Chapter 9 Exercise 9, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. Please include some comments.

*9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides
have the same length and all angles have the same degree (i.e., the polygon is
both equilateral and equiangular). Design a class named RegularPolygon that
contains:
A private int data field named n that defines the number of sides in the poly-
gon with default value 3 .
A private double data field named side that stores the length of the side with
default value 1 .
A private double data field named x that defines the x-coordinate of the poly-
gon’s center with default value 0 .
A private double data field named y that defines the y-coordinate of the poly-
gon’s center with default value 0 .
A no-arg constructor that creates a regular polygon with default values.
A constructor that creates a regular polygon with the specified number of sides
and length of side, centered at ( 0 , 0 ).
A constructor that creates a regular polygon with the specified number of sides,
length of side, and x- and y-coordinates.
The accessor and mutator methods for all data fields.
The method getPerimeter() that returns the perimeter of the polygon.
The method getArea() that returns the area of the polygon. The formula for


Draw the UML diagram for the class and then implement the class. Write a test
program that creates three RegularPolygon objects, created using the no-arg
constructor, using RegularPolygon(6, 4) , and using RegularPolygon(10,
4, 5.6, 7.8) . For each object, display its perimeter and area.

Area : nxS Area = II 4 × tan n 2

Explanation / Answer

Answer ->

package my_chegg_package;
import java.lang.*;
public class RegularPolygonClass {
   private int numberOfSides;
   private double side;
   private double x;
   private double y;
   public RegularPolygonClass()
   {
       numberOfSides = 3;
       side = 1;
       x = 0;
       y = 0;
   }
   public RegularPolygonClass(int numberOfSides,double side)
   {
       this.numberOfSides = numberOfSides;
       this.side = side;
       x = 0;
       y = 0;
   }
   public RegularPolygonClass(int numberOfSides,double side,double x,double y)
   {
       this.numberOfSides = numberOfSides;
       this.side = side;
       this.x=x;
       this.y=y;
   }
   public int getNumberOfSides() {
       return numberOfSides;
   }
   public void setNumberOfSides(int numberOfSides) {
       this.numberOfSides = numberOfSides;
   }
   public double getSide() {
       return side;
   }
   public void setSide(double side) {
       this.side = side;
   }
   public double getX() {
       return x;
   }
   public void setX(double x) {
       this.x = x;
   }
   public double getY() {
       return y;
   }
   public void setY(double y) {
       this.y = y;
   }
   double getParameter()
   {
       double parameter=( numberOfSides * side);
       return parameter;
   }
   double getArea()
   {
      
        double tempVar = Math.PI/numberOfSides;
        double tanValue = Math.tan(tempVar);
        double area = (numberOfSides*side*side)/(4*tanValue);
        return area;
   }
   public static void main (String[] args){
       RegularPolygonClass obj1=new RegularPolygonClass();
       RegularPolygonClass obj2=new RegularPolygonClass(6,4);
       RegularPolygonClass obj3=new RegularPolygonClass(10,4,5.6,7.8);
       System.out.println("Parameter: "+obj1.getParameter()+" "+"Area: "+ obj1.getArea());
       System.out.println("Parameter: "+obj2.getParameter()+" "+"Area: "+ obj2.getArea());
       System.out.println("Parameter: "+obj3.getParameter()+" "+"Area: "+ obj3.getArea());
      
   }
  
}

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