2016-09-18 5 views
-2

私はこのコードが正しくコンパイルされない理由を理解しようとしています。私がコンパイルしようとすると、メインを追加するときにエラーが発生します。メインを追加すると、修正する方法が正直わからないほど多くのエラーが発生します。誰かがコードを見て助けてくれますか?もし誰かが大いに感謝するのを助けることができれば。私のコードはなぜコンパイルされませんか?

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は、予めエラーを含まないために謝罪します。 cmdを使用してコンパイルすると、次のエラーが発生します。

エラー:クラスポリゴンでは見られない主な方法は、のようにmainメソッドを定義してくださいます。public static無効メイン(文字列[] args)を または

+0

を、それがにつながりますあなたは、あなたが見ているコードとエラーを共有することから始めることができます。 – smarx

+0

基本的なSOの推奨事項:エラーメッセージを要約して質問に貼り付けないでください。コンパイル時にメインがなく、メインを持たないことがコンパイル時エラーではないというエラーが発生するとします。どんな種類のエラーが発生しているかわからないと思って、質問に必要な情報*を入れてください。 – arcy

+0

あなたの基本的な理解のために@RichardこのコードがPolygon.javaという名前のファイルにある場合、コードはコンパイルされます。私はこれをして、それはエラーなしで私のためにコンパイルされています。このコードを実行しようとすると、メインメソッドがないためエラーが発生する可能性があります。 メインメソッドの追加方法がわからない場合や、エラーが表示された場合は、表示されているすべてのエラーを投稿してください。それが難しい場合は、スクリーンショットを投稿してください。 – Pat

答えて

0

作成JavaFXアプリケーションクラスがjavafx.application.Applicationを拡張する必要があります同じパッケージ内のMainという別のクラス。そこにmainメソッドを追加してください。オブジェクトをインスタンス化します。

public class Main { 

    public static void main(String[] args) { 

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

あなたのコードはうまくコンパイルされています。あなたが欠けているものは、今作成したオブジェクトクラスを使用する別のクラスです。だから、基本的にはあなたが何をする必要があるか

は、あなたのエディタ(日食、メモ帳++など)に移動され、別のファイルを作成し、このようなものを使用します。私はメインを追加するときに「

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 


    } 

} 
関連する問題