0

I'm trying to do my first methods. I'm having trouble getting the perimeter to display the output as a String. I'm wondering why this is happening. I very well could have other problems inside of my code but the perimeter not outputting is what is holding me back right now.

Following is my code.

public class Polygon {

    public Polygon() {
        int numSides = 4;
        double SideLength = 5.0;
        double xCoord = 0.0;
        double yCoord = 0.0;
        double apothem = 5.0;
        double perimeter = 20.0;
    }

    private int numSides = 2;
    private double SideLength = 2;
    private double xCoord;
    private double yCoord;
    private double apothem;
    private double perimeter;
    private double area;

    public Polygon(int numsides, double sideLength, double xcoord, double ycoord, double Apothem, double Perimeter) {
        SideLength = sideLength;
        numSides = numsides;
        xCoord = xcoord;
        yCoord = ycoord;
        apothem = Apothem;
        perimeter = Perimeter;
    }

    public int getnumsides() {
        return numSides;
    }

    public double getSideLength() {
        return SideLength;
    }

    public double getxcoord() {
        return xCoord;
    }

    public double getycoord() {
        return yCoord;
    }

    public double getApothem() {
        return apothem;
    }

    public double getPerimeter() {
        return numSides * SideLength;
    }

    public void setsideLength(double ssideLength){
        SideLength = ssideLength;
    }

    public void setnumsides(int snumsides){
        numSides = snumsides;
    }

    public void setxcoord(double sxcoord){
        xCoord = sxcoord;
    }

    public void setycoord(int sycoord){
        yCoord = sycoord;
    }

    public void setApothem(int sApothem){
        apothem = sApothem;
    }

    public void setPerimeter(int sPerimeter){
        perimeter = sPerimeter;
    }

    public String toString() {
        String str = numSides + " is the number of sides the polygon has and " + SideLength + " is how long the sides are. "+ xCoord + " is how long the x coordinate is and " + yCoord + " is how long the y coordinate is. " + apothem + " is the apothem of the polygon and " + perimeter + " is the perimeter of the polygon."; 
        return str; 
    }

    public void getArea() {
        area = .5 * apothem * perimeter;

    }
}
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
Domonos
  • 11
  • 1

2 Answers2

0

You are again defining same field variables in Polygon() constructor which is not required because you have already defined them as private class members. This is the reason that some values are setting as default while printing toString() method.

Your Polygon() constructor should be look like this:

    public Polygon() {
        numSides = 4;
        SideLength = 5.0;
        xCoord = 0.0;
        yCoord = 0.0;
        apothem = 5.0;
        perimeter = 20.0;
    }
Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
0

What do you mean by "perimeter not outputting"?

May be this is what you want to achieve?

public static void main(String[] args){
        Polygon p = new Polygon();
        double perimeter = p.getPerimeter();
        System.out.println("Perimeter is " + perimeter);
    }
coderWorld
  • 602
  • 1
  • 8
  • 28