The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 364 Weeks Ago, 2 Days Ago |
| Questions Answered: | 1850 |
| Tutorials Posted: | 1850 |
Graduate in Biology and Nutrition, MBA Finance
Florida State University
Aug-2000 - Jul-2007
Ass. Relationship Manager
Penn-Florida
Mar-2009 - Feb-2016
This is my second program in the java realm of programming. I was able to write up the "polygon" file and it is able to compile. "TestPolygon" I found on this site and tried to modify it to be able to run. Something is not executing properly. The "TestPolygon" file should be separate from the "Polygon" file? Or should one be copied into another for it to run properly? Please advise and touch on the "TestPolygon" program. Thank you!
/*
*
*
*
*/
Â
public class Polygon {
Â
  private int numSides;    //number of sides
  private double sideLength; //length of each side
  private double xCoord;   //x-coordinate of th center of the polygon
  private double yCoord;   //the y-coordinate
  private double apothem;   //defines the apothem
  private double perimeter;
Â
  // no argument constructor
  Â
  public Polygon() {
    this.numSides = 4;
    this.sideLength = 2.0;
    this.xCoord = 0.0;
    this.yCoord = 0.0;
    this.apothem = 4.0;
    this.perimeter = 16.0;
  }
Â
    public Polygon(int _numSides, double _sideLength, double _xCoord, double _yCoord, double _apothem) {
Â
    this.numSides = _numSides;
    this.sideLength = _sideLength;
    this.xCoord = _xCoord;
    this.yCoord = _yCoord;
    this.apothem = _apothem;
Â
  }
Â
  // area is doubled
Â
    public double getArea() {
    perimeter = numSides * sideLength;
    double area = (0.5) * apothem * perimeter;
    return area;
Â
  }
Â
  //getter & setters
Â
  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(double perimeter) {
    this.perimeter = perimeter;
  }
Â
  public String toString() {
    return "Polygon definitions[" + "number of sides=" + numSides + ", Each side length=" + sideLength + ", xCoord=" + xCoord + ", yCoord=" + yCoord + ", apothem=" + apothem + ']';
  }
Â
}
Â
/*
 *Â
 *
 *
 */
Â
public class TestPolygon {
Â
  public static void main(String[] args) {
Â
    //create Polygon class object with no parameters
Â
    Polygon poly1 = new Polygon();
Â
    //call all methods
Â
    System.out.println(poly1.toString());
    System.out.println("\tNumber of sides: " + poly1.getNumSides() + "\n"
        + "\tPolygon side length: " + poly1.getSideLength() + "\n"
        + "\tThe X coordinate: " + poly1.getxCoord() + "\n"
        + "\tThe Y coordinate: " + poly1.getxCoord() + "\n"
        + "\tPolygon's apothem: " + poly1.getyCoord() + "\n"
        + "\tThe polygon perimeter: " + poly1.getPerimeter() + "\n"
        + "\tThe polygon area: " + poly1.getArea());
Â
Â
    //create 5 Polygon class object with parameters
Â
    Polygon poly[] = new Polygon[5];
Â
    //initialize the objects with values
Â
//[Sides,sideLength,xCoord,yCoord,apothem]
Â
Â
    poly[0] = new Polygon(3, 2.0, 1.0, 1.0, 1.0);    Â
poly[1] = new Polygon(8, 7.1, 1.0, 1.0, 5.0);
    poly[2] = new Polygon(6, 4.1, 0.0, 0.0, 2.0);
    poly[3] = new Polygon(7, 4.0, 2.0, 2.0, 4.0);
poly[4] = new Polygon(8, 3.0, 1.0, 5.0, 6.0);
Â
    //display the values
Â
    for (int i = 0; i < poly.length; i++) {
      System.out.println("----------------------------------------");
      System.out.println(poly[i].toString());
      System.out.println("\tNumber of sides: " + poly[i].getNumSides());
      System.out.println("\tPolygon side length: " + poly[i].getSideLength());
      System.out.println("\tThe X coordinate: " + poly[i].getxCoord());
      System.out.println("\tThe Y coordinate: " + poly[i].getyCoord());
      System.out.println("\tPolygon's apothem: " + poly[i].getApothem());
      System.out.println("\tThe polygon perimeter: " + poly[i].getPerimeter());
      System.out.println("\tThe polygon area: " + poly[i].getArea());
    }
  }
}
Â
Â
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- -----------Tha-----------nk -----------you----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------.Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m Â----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I