到目前为止,我已经完成了此代码,但我正在尝试学习如何完成此方法,以便它将编译使用的总边数并在最后一个打印行中打印该值。问题方法是getTotalSides,我目前将它设置为返回0,但我希望它返回总边数,意思是:mp1.getSides() + mp2.getSides() + mp3.getSides() + mp4.getSides( )。
public class TestParts {
public static void main(String[] args) {
MyPolygon mp1 = new MyPolygon();
MyPolygon mp2 = new MyPolygon(4);
MyPolygon mp3 = new MyPolygon(5);
MyPolygon mp4 = new MyPolygon(6);
System.out.println("Polygon 1 has " + mp1.getSides()
+ " sides, angles total " + mp1.getSumOfAngles());
System.out.println("Polygon 2 has " + mp2.getSides()
+ " sides, angles total " + mp2.getSumOfAngles());
System.out.println("Polygon 3 has " + mp3.getSides()
+ " sides, angles total " + mp3.getSumOfAngles());
System.out.println("Polygon 4 has " + mp4.getSides()
+ " sides, angles total " + mp4.getSumOfAngles());
System.out.println("There are " + MyPolygon.getTotalSides()
+ " total sides");
}
}
class MyPolygon {
double getSides;
MyPolygon() {
getSides = 3;
}
static double getTotalSides() {
return 0;
}
double getSides() {
// TODO Auto-generated method stub
return getSides;
}
MyPolygon(double newGetSides) {
getSides = newGetSides;
}
double getSumOfAngles() {
return ((getSides - 2) * 180);
}
void setGetSides(double newGetSides) {
getSides = newGetSides;
}