Question

Design a Java class named Polygon that contains:  A private int data field named numSides...

Design a Java class named Polygon that contains:

 A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.

 A private double data field named sideLength that defines the length of each side. The default value should be 5.0.

 A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.

 A private double data field named yCoord that defines the y-coordinate of the center of the polygon. The default value should be 0.0.

 A private double data field named apothem that defines the apothem of the polygon. The default value should 5.0.

 A private double data filed named perimeter that defines the perimeter of the polygon. The default value should 20.0.

 A no argument constructor that creates a Polygon using the default number of sides, default side length, default x- and y-coordinates, and the default apothem.

 A constructor that creates a Polygon using a specified number of sides, side length, x- and y-coordinates and the apothem

 Getter and setter methods for all data fields

 A getArea() method that returns a double value representing the area of the polygon.

(Note the area of a regular Polgon can be calculated from 1⁄2 * Apothem*Perimeter)

 A toString() method that displays the number of sides, side length, x-coordinate, y-

coordinate and apothem values in String format

Be sure your code compiles. You should use the command prompt and not a GUI for data entry and display.

Write a Java test program, named TestPolygon, to create 5 different polygons representing the 5 test cases you just created. When creating the five polygons, create one using the no argument constructor. For the remaining four, feel free to use any number of sides, side length and x-, and y-coordinates and apothem that are not equal to the default values and not equal to each other. For each of the five polygons, call all of the methods and display the results. For example for a Polygon with 3 sides, side length of 2.0 and x-coordinate and y-coordinates of 1.0, and apothem of 1.0 the following test data may result:

***Output***

toString(): (numsides=3, sideLength=2.0, xcoord=1.0,ycoord=1.0, apothem=1.0) getNumSides(): 3
getSideLength(): 2.0
getXCoord(): 1.0

getYCoord(): 1.0
getApothem():1.0
getPerimeter(): 6.0
getArea(): 3.0

Homework Answers

Answer #1

So, the complete solution for the same is as follows:

Source-Code;

Polygon class

class Polygon
{
private int numSides;
  
private double sideLength;
  
private double xCoord;
  
private double yCoord;
  
private double apothem;
  
private double perimeter;
  
public Polygon()
{
numSides = 4;
  
sideLength = 5.0;
  
xCoord = 0.0;
  
yCoord = 0.0;
  
apothem = 5.0;
  
perimeter = 20.0;
}
  
public Polygon(int no, double len, double x, double y, double a)
{
numSides = no;
  
sideLength = len;
  
xCoord = x;
  
yCoord = y;
  
apothem = a;
  
setPerimeter();
}

public int getNumSides() {
return numSides;
}

public void setNumSides(int numSides) {
this.numSides = numSides;
}

public double getSideLength() {
return sideLength;
}

public void setSideLength(double sideLength) {
this.sideLength = sideLength;
}

public double getXCoord() {
return xCoord;
}

public void setXCoord(double xCoord) {
this.xCoord = xCoord;
}

public double getYCoord() {
return yCoord;
}

public void setYCoord(double yCoord) {
this.yCoord = yCoord;
}

public double getApothem() {
return apothem;
}

public void setApothem(double apothem) {
this.apothem = apothem;
}

public double getPerimeter() {
return perimeter;
}

public void setPerimeter() {
perimeter = numSides * sideLength;
}
  
public double getArea()
{
double area;
  
area = apothem * perimeter / 2;
  
return area;
}
  
@Override
public String toString()
{
String str;
  
str = "(numsides=" + numSides +", sideLength=" + sideLength +", xcoord="+xCoord + ", ycoord="+yCoord+", apothem="+apothem+")";
  
return str;
}
}

TestPolygon class

public class TestPolygon {
public static void main(String[] args) {
  
Polygon p1 = new Polygon();
  
System.out.println("Polygon 1:");
System.out.println("toString(): "+ p1.toString() );
System.out.println("getNumSides(): "+p1.getNumSides());
System.out.println("getSideLength(): "+p1.getSideLength());
System.out.println("getXCoord(): "+p1.getXCoord());
System.out.println("getYCoord(): "+p1.getYCoord());
System.out.println("getApothem(): "+p1.getApothem());
System.out.println("getPerimeter(): "+p1.getPerimeter());
System.out.println("getArea(): "+p1.getArea());
System.out.println("");
  
Polygon p2 = new Polygon(3, 2.0, 1.0, 1.0, 1.0);
System.out.println("Polygon 2:");
System.out.println("toString(): "+ p2.toString() );
System.out.println("getNumSides(): "+p2.getNumSides());
System.out.println("getSideLength(): "+p2.getSideLength());
System.out.println("getXCoord(): "+p2.getXCoord());
System.out.println("getYCoord(): "+p2.getYCoord());
System.out.println("getApothem(): "+p2.getApothem());
System.out.println("getPerimeter(): "+p2.getPerimeter());
System.out.println("getArea(): "+p2.getArea());
System.out.println("");
  
Polygon p3 = new Polygon(5, 3.0, 1.5, 1.5, 1.0);
System.out.println("Polygon 3:");
System.out.println("toString(): "+ p3.toString() );
System.out.println("getNumSides(): "+p3.getNumSides());
System.out.println("getSideLength(): "+p3.getSideLength());
System.out.println("getXCoord(): "+p3.getXCoord());
System.out.println("getYCoord(): "+p3.getYCoord());
System.out.println("getApothem(): "+p3.getApothem());
System.out.println("getPerimeter(): "+p3.getPerimeter());
System.out.println("getArea(): "+p3.getArea());
System.out.println("");
  
Polygon p4 = new Polygon(6, 6.0, 0.0, 1.0, 2.0);
System.out.println("Polygon 4:");
System.out.println("toString(): "+ p4.toString() );
System.out.println("getNumSides(): "+p4.getNumSides());
System.out.println("getSideLength(): "+p4.getSideLength());
System.out.println("getXCoord(): "+p4.getXCoord());
System.out.println("getYCoord(): "+p4.getYCoord());
System.out.println("getApothem(): "+p4.getApothem());
System.out.println("getPerimeter(): "+p4.getPerimeter());
System.out.println("getArea(): "+p4.getArea());
System.out.println("");
  
Polygon p5 = new Polygon(7, 2.0, 2.0, 2.0, 3.0);
System.out.println("Polygon 5:");
System.out.println("toString(): "+ p5.toString() );
System.out.println("getNumSides(): "+p5.getNumSides());
System.out.println("getSideLength(): "+p5.getSideLength());
System.out.println("getXCoord(): "+p5.getXCoord());
System.out.println("getYCoord(): "+p5.getYCoord());
System.out.println("getApothem(): "+p5.getApothem());
System.out.println("getPerimeter(): "+p5.getPerimeter());
System.out.println("getArea(): "+p5.getArea());
System.out.println("");
}
}

Output:

Description:

  • ​At first, the class Polygon is created with given member variables.
  • All these variables' default values are set in the default constructor.
  • When user creates a polygon with specific arguments, it is set using parameterized constructor.
  • Here, user will not pass perimeter. So, it is set in the parameterized constructor which means number_of_sides*side_length.
  • All the getter and setter methods are written after that.
  • Area is calculated as per the given formula for a polygon.
  • At last, toString() method is of Object class, which is the base class in Java. So, it is overridden in this class, so annotation @Override is mentioned there.
  • Now comes the TestPolygon class in which p1 is created via the default constructor. Other polygon members are given the values and they are created as per those values.
  • For each and every polygon, I have called all the methods as per the question and the output is also displayed here.

Do comment if there is any query. Thank you. :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ Design a class named Account that contains (keep the data fields private): a) An int...
c++ Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate. f) A function named getMonthlyInterestRate() that...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private...
Problem Description: Problem Description: (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
java Consider the following class definition: public class Circle { private double radius; public Circle (double...
java Consider the following class definition: public class Circle { private double radius; public Circle (double r) { radius = r ; } public double getArea(){ return Math.PI * radius * radius; } public double getRadius(){ return radius; } } a) Write a toString method for this class. The method should return a string containing the radius and area of the circle; For example “The area of a circle with radius 2.0 is 12.1.” b) Writeaequalsmethodforthisclass.ThemethodshouldacceptaCircleobjectasan argument. It should return...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Define the EvenNumber class for representing an even number. The class contains: A data field value...
Define the EvenNumber class for representing an even number. The class contains: A data field value of the int type that represents the integer value stored in the object. A no-arg constructor that creates an EvenNumber object for the value 0. A constructor that constructs an EvenNumber object with the specified value. A function named getValue() to return an int value for this object. A function named getNext() to return an EvenNumber object that represents the next even number after...
Must be C++ programming    The MyPoint class was created to model a point in a...
Must be C++ programming    The MyPoint class was created to model a point in a two-dimensional space. The MyPoint class has the properties x and y that represent x- and y-coordinates, two get functions for x and y, and the function for returning the distance between two points. Create a class named ThreeDPoint to model a point in a three-dimensional space. Let ThreeDPoint be derived from MyPoint with the following additional features: A data field named z that represents...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT