-3

I am having issues with getting the TestPolygon.java to run correctly. THe "//Constructor that creates four polygons with any value that are not equal to the defualt value or to each other Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)}; " line of my code keeps coming back with an error for every new statement saying that "no suitable constructor found for Polygon(int,double,double,double). Picture is attached.

/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/

import java.lang.Math;

public class Polygon {
 private int numSides;
 private double sideLength;
 private double xCoord;
 private double yCoord;
 private double apothem;
 private double perimeter;

 //Constructor that creates a Polygon using the defaults
 public Polygon(){
     numSides = 4;
     sideLength = 10.0;
     xCoord = 0.0;
     yCoord = 0.0;
     apothem = 5.0;
}
 //Constructor that uses the specified numbers
 public Polygon(int s,double sL,double x,double y,double a, double p){
     setNumSides(s);
     setSideLength(sL);
     setXCoord(x);
     setYCoord(y);
     setApothem(a);
}

 // A getArea() method that returns a double value for the area of the polygon
 public double getArea() {
 return .5*apothem*perimeter;
}

 // Getter and setter for all data fields
 public int setNumSides(int s){
      numSides = s;
      return numSides;
}
 public double setSideLength(double sL){
      sideLength = sL;
      return sideLength;
}
 public double setXCoord(double x){
      xCoord = x;
      return xCoord;
}
 public double setYCoord(double y){
      yCoord = y;
      return yCoord;
}
 public double setApothem(double a){
      apothem = a;
      return apothem;
}
 public double setPerimeter(double p){
       perimeter = p;
       return 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 perimeter;
 }

 //A toString() that displays the values in string format
 public String toString(){
      return "("+numSides+", "+String.format("%.1f",sideLength)+", "+String.format("%.1f",xCoord)+", "+String.format("%.1f",yCoord)+")";
 }
}

/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/

//Import necessary packages
import java.util.Scanner;
import java.text.DecimalFormat;
import java.lang.Math;

class TestPolygon{
    public static void main(String[] args){

    //Constructor that creates one polygon with no argument
    Polygon P = new Polygon();

    //Constructor that creates four polygons with any value that are not equal to the defualt value or to each other
    Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)};

    //Constructor that calls all methods and displays results
    System.out.println("toString() results: "+P);
    System.out.println("getNumSides() results: "+ P.getNumSides());
    System.out.println("getSideLength() results: "+ P.getSideLength());
    System.out.println("getXCoord() results: "+ P.getXCoord());
    System.out.println("getYCoord() results: "+ P.getYCoord());
    System.out.println("getPerimeter() results: "+ P.getPerimeter());
    System.out.println("setNumSides(4) results: "+ P.setNumSides(4));
    System.out.println("setSideLength(3.0) results: "+ P.setSideLength(3.0));
    System.out.println("setXCoord(2.0) results: "+ P.setXCoord(2));
    System.out.println("setYCoord(2.0) results: "+ P.setYCoord(2));
    for(int i=0; i<P_array.length; i++)
    {
    System.out.println("toString() results: "+P_array[i]);
    System.out.println("getNumSides() results: "+ P_array[i].getNumSides());
    System.out.println("getSideLength() results: "+ P_array[i].getSideLength());
    System.out.println("getXCoord() results: "+ P_array[i].getXCoord());
    System.out.println("getYCoord() results: "+ P_array[i].getYCoord());
    System.out.println("getPerimeter() results: "+ P_array[i].getPerimeter());
    System.out.println("setNumSides(4) results: "+ P_array[i].setNumSides(4));
    System.out.println("setSideLength(3.0) results: "+ P_array[i].setSideLength(3.0));
    System.out.println("setXCoord(2.0) results: "+ P_array[i].setXCoord(2));
    System.out.println("setYCoord(2.0) results: "+ P_array[i].setYCoord(2));
    }
    }
}

enter image description here

SLePort
  • 15,211
  • 3
  • 34
  • 44
DarrenPJr
  • 11
  • 2
  • 5
  • 1
    The error message seems pretty obvious to me. Your Polygon() constructor accepts an int and 5 doubles. You are attempting to use a constructor that takes an int and _4_ doubles. A tiny bit of research probably would have gone a long way here. – rmlan Nov 17 '16 at 18:10
  • Which constructor exactly are you trying to invoke with `new Polygon(10, 5.0, 1.0, 3.0)`? Compare its expected amount of arguments with amount of arguments you are actually providing. Do they match? – Pshemo Nov 17 '16 at 18:12
  • I did do research. The reason I posted on here is because I needed help because I didnt find anything. Thanks for the rude comment and help though! – DarrenPJr Nov 17 '16 at 18:13
  • It's actually an int and _3_ other values, for a total of 4. You'll also want to fix your setters to be of type `void` and not return anything. Plus, *Java* and *JavaScript* are separate, unrelated languages. –  Nov 17 '16 at 18:14
  • Research includes googling the error message. –  Nov 17 '16 at 18:21
  • Thank all of you again. Sorry if this is a bad question but this is my first java programming class so I am still learning. – DarrenPJr Nov 17 '16 at 18:26

3 Answers3

0

The issue is that you are calling Polygon() with 4 arguments, but you do not have a 4 argument constructor. You have a no arg

 public Polygon(){
     numSides = 4;
     sideLength = 10.0;
     xCoord = 0.0;
     yCoord = 0.0;
     apothem = 5.0;
}

and 6 arg

public Polygon(int s,double sL,double x,double y,double a, double p){
 setNumSides(s);
 setSideLength(sL);
 setXCoord(x);
 setYCoord(y);
 setApothem(a);
}

but you are calling

new Polygon(10,5.0,1.0,3.0)

You can either add 2 arguments to your new Polygon calls OR remove 2 arguments from the 6 arg constructor OR create a constructor that takes 4 arguments.

Mike
  • 143
  • 2
  • 11
0

Your parameterized constructor takes 6 arguments ,i.e,

  • int s
  • double sL
  • double x
  • double y
  • double a
  • double p

But, when you are constructing the polygon, you are passing in only four values, i.e new Polygon(10,5.0,1.0,3.0)

Since you are not using the last argument in your constructor, i.e "double p", your constructor should be : public Polygon(int s,double sL,double x,double y,double a)

vinay hegde
  • 99
  • 10
0

The error message says it all:

no suitable constructor found for Polygon(int,double,double,double).

It simply means that Java is unable to find a constructor for class Polygon with argument (int, double, double, double) (which is what you have used when creating a Polygon object)

If you look at your constructors for class Polygon:

You created 2 constructors:

  • No arguments constructor

  • Constructor with 6 arguments (int, double, double, double, double, double)


Hence, when creating a Polygon object, ensure you are doing:

new Polygon(); 
or
new Polygon(int, double, double, double, double, double);

Example:

Polygon p1 = new Polygon();
Polygon p2 = new Polygon(10, 2.5, 3.5, 4.5, 5.5, 6.5);
Polygon p3 = new Polygon(10, 20, 30, 40, 50, 60);    //this is ok too

Right now, you are providing only 4 arguments:

new Polygon(10, 5.0, 1.0, 3.0), 
             ^   ^    ^    ^
             |   |    |    |
            int  d    d    d  (d for double)
user3437460
  • 17,253
  • 15
  • 58
  • 106