java - When I create an object and pass arguments my class's constructors fail -
new programming, constructors seem straight forward concept. can't figure out why when create object , pass arguments in test class fail initialize polygon class's fields.
public class polygon{ private int numsides; private double sidelength, xcoord, ycoord, apothem, perimeter; 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; } public polygon(int numsides, double sidelength, double xcoord, double ycoord, double apothem, double perimeter){ this.numsides = numsides; this.sidelength = sidelength; this.xcoord = xcoord; this.ycoord = ycoord; this.apothem = apothem; this.perimeter = perimeter; } public static double getarea(double apothem, double perimeter) { double area = .5 * apothem * perimeter; return area; } public static string tostring(int numsides, double sidelength, double xcoord, double ycoord, double apothem){ string results = string.format("tostring() results: (numsides=%d, sidelength=%.1f%n, xcoord=%.1f%n, ycoord=%.1f%n, apothem=%.1f%n)", numsides, sidelength, xcoord, ycoord, apothem); return results; } }
and test class
public class testpolygon { public static void main(string[] args){ polygon testpoly = new polygon(4, 10.0, 0.0, 0.0, 5.0, 20.0); string results = testpoly.tostring(); system.out.println(results); double area = testpoly.getarea(); system.out.printf("getarea() results: %.1f%n", area); } // end main method }
you're calling polygon.tostring()
you've implemented polygon.tostring(int, double, double, double, double)
get rid of parameters in tostring()
(overriding object.tostring()
then) , work fine. also, can't static
Comments
Post a Comment