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
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...
Whenever I try to run this program a window appears with Class not Found in Main...
Whenever I try to run this program a window appears with Class not Found in Main project. Thanks in Advance. * * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assignment10; /** * * @author goodf */ public class Assignment10{ public class SimpleGeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;    /**...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT