Why won't my code compile?

I'm trying to figure out why this code isn't compiling correctly. When I try to compile, I get the error that it needs a main, when I add the main, it leads to a ton more errors that I'm honestly not sure how to fix. Can someone take a look at the code and help me out? If anyone could help it would be greatly appreciated.

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 = 10.0;
    this.xCoord = 0.0;
    this.yCoord = 0.0;
    this.apothem = 5.0;
    this.perimeter = 20.0;
}

/**
 * constructor that takes arguments
 *
 * @param _numSides :-number of sides
 * @param _sideLength :-the length of each side
 * @param _xCoord :-the x coordinate
 * @param _yCoord :-the Y coordinate
 * @param _apothem :-the apothem
 */
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;

}

/**
 *
 * @return area of the polygon[double]
 */
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;
}

//to string method
@Override
public String toString() {
    return "Polygon definitions[" + "number of sides=" + numSides + ", Each side length=" + sideLength + ", xCoord=" + xCoord + ", yCoord=" + yCoord + ", apothem=" + apothem + ']';
}

}

I apologize for not including the errors beforehand. When compiling via cmd I get the following error.

Error: Main method not found in class Polygon, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application


Create a separate class called Main in the same package. Add the main method there. then instantiate an object.

public class Main {

    public static void main(String[] args) {

        Polygon p = new Polygon();
        double apothem = p.getApothem();
        System.out.println(apothem);
    }
}

Your code is compiling just fine. What you're lacking is another class that uses the object class that you've just created.

So basically what you need to do is go to your editor (eclipse, notepad++, etc.), make another file and use something like this:

public class PolygonMain {

    public static void main(String[] args) {

       Polygon p = new Polygon();

       Here you can use your getters/setters toString etc. 
       The name of the object you've created in this example would be p, therefore
        p.toString() would return the toString method of your object class


    }

}
链接地址: http://www.djcxy.com/p/73648.html

上一篇: 为什么Java在使用“plus equals”运算符时执行从double到integer的隐式类型转换?

下一篇: 为什么我的代码不能编译?