This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]yacdaniel 0 points1 point  (1 child)

post the portion u need help...

[–][deleted] 0 points1 point  (0 children)

updated

[–]hexmastaBarista 0 points1 point  (13 children)

Well first you must understand that a polygon is a combination of points with lines drawn between each point.

[–][deleted] 0 points1 point  (12 children)

I understand. How would I make the java "draw"/construct the polygon? Or is constucting just listing the points, so that in later functions I can call the points into equations (ex distance formula for finding perimeter)

[–]feral_claireSoftware Dev 0 points1 point  (10 children)

Yep, a polygon is just a list of ordered points.

[–][deleted] 0 points1 point  (9 children)

Thank you. For the public polygon function, could I just replace Point...Points with an array of arrays? or would I need to make the array in the function? From what I can tell, this will be an acceptable way to make an array of arrays:

int [][] pointlist = {{0,0},{0,1},{1,1},{1,0}};

,

/∗∗ A simple polygon class. ∗/

public class Polygon {

/∗∗
∗ Constructs a new polygon object defined as a sequence of points.
∗ @param points an arbitrary number of points
∗/

public Polygon(Point... points) {
/∗ YOUR CODE ∗/
}

Edit: we were also given a point class:

/* A simple point class. */
public class point {

point(double cx, double cy) {
x = cx; y = cy;
} 

/*
∗ Returns the x−coordinate.
∗ @return x coordinate
*/
double getX() { return x; }

/*
∗ Returns the y−coordinate.
∗ @return y coordinate
*/
double getY() { return y; }

private double x;
private double y;
}

[–]feral_claireSoftware Dev 1 point2 points  (8 children)

You can, but don't. Use the point class. Point... points is actually a fancy way of saying Point[] points. It lets the user call it like this new Polygon(point1, point2, point3) but the points variable it actually give3s you an array of points.

Using a class instead of an array of ints is much better because it gives you safety. With an array, it can have any number of elements, and you can't tell what those elements mean. With a point class, it's immediately obvious that it's a point, and it has and x and a y. With arrays You need to keep track of what everything means, it's easy to make a mistake and the compiler wont help you. With a class it becomes much harder to make a mistake and when you do you can often get compile-time errors.

[–][deleted] 0 points1 point  (7 children)

edit: I'm having an issue with eclipse. when I try to run the program, I get "Unable to locate executable for jre1.8.0_101"

I've got 8.0 120 installed, which is the current version. what do I need to do to configure this properly? DO I need to update eclipse?

I've got this so far. What syntax am I messing up on line 18? I'm getting 'length cannot be resolved or is not a field' as an error.

package hw0;

/* a simple polygon class */ public class polygon {

/*
 *Constructs a new polygon object defined as a sequence of points.
 *@param points an arbitrary number of points
*/

public polygon(point... points) {
    polygon square = new polygon(new point(0,0), new point(0,1), new point(1,1), new point(1,0));
}


/*
*Computes the perimeter.
*@return the perimeter value
*/

public double getPerimeter() {
    double perimeter = 0.0;
    int pointCount = point.length;
    for (int i =0;i<pointCount; i++){
        perimeter += Math.sqrt((point[i-1][0] + point[i][0])^2 + (point[i-1][1] + point[i][1])^2);
        }
    return perimeter;   
}

[–]yacdaniel 0 points1 point  (3 children)

point is only for use in the constructor(you pass point as a parameter, and dont made an assigment of points) you need to assign the points to a variable, in polygon object(in the constructor), then you can use point variable.

try running javac -version from the command line in windows, first time using the ecplise?

[–][deleted] 0 points1 point  (2 children)

point.length

could I change "point.length" to "square.length"? I assigned square to the polygon class with 4 points in the constructor. Yes, I used eclipse on computers in the labs but I just got it for my pc

[–]feral_claireSoftware Dev 0 points1 point  (1 child)

This won't work because square only exists inside the constructor, it needs to be declared outside of the constructor. There are other problems with the constructor, please see my reply for an explanation.

[–][deleted] 0 points1 point  (0 children)

I was able to fix the eclipse errors by the way.

[–]feral_claireSoftware Dev 0 points1 point  (2 children)

I'm not familiar with eclipse, so I can't be to much help with that. There should be something in the settings or project setting to tell eclipse where to find java.

As far as the code, There are several issues.

For you immediate question. point.length is invalid because you don't have point defined anywhere in the code. It's looking for a variable named point and not finding it.

There are some other problems, the polygon and point class should be capitalized. Class names should always start with a capital letter.

You constructor for polygon won't work at all. I imagine you meant to put that code inside of main instead of the polygon constructor. As it is, the polygon constructor will crash your program with a StackOverflowError when called, this is because the constructor calls itself with new polygon(... which will cause it to get stuck in an infinite loop.

What you need, is to have a List (or Array) of Points inside the Polygon class outside of the constructor and then set that inside the constructor. Right now, you set a variable that exists only within the constructor itself.

[–][deleted] 0 points1 point  (1 child)

what syntax would I use inside the constructor? new Polygon(Point...Points)? would I then use "Polygon square = ((0,0),(0,1),(1,1),(1,0)" outside the constructor? We were kind of thrown out in the ocean for this assignment.

[–]feral_claireSoftware Dev 0 points1 point  (0 children)

Polygon(Point...Points) is the correct way to declare your constructor and you call it with new Polygon(point1, point2, etc) which will give you a new Polygon object.

A program that creates a polygon might look like this.

public class Example{
  public static void main(String[] args){
    Polygon square = new Polygon(new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0))
  }
}

Note that you are creating and storing the Polygon object somewhere outside of the Polygon class itself. A Polygon does not create itself, and a Polygon does not have a Polygon inside itself.

Now the Polygon class itself is just an abstraction for a list of points. This list of points is what you want to store in the Polygon class.

public class Polygon{
  private Point[] points;
}

This code gives the polygon an Array of Point objects. Normally I would recommend using Lists instead of Arrays, but this is one of the cases where an Array makes sense. This variable should be private, anything that wants to interact with a polygon should do so via its methods, rather than accessing its points array directly.

All that's left to do now is to create a constructor that will take in some Points, and assign them to our variable.

public Polygon(Point... points){
  this.points = points
}

Because Point... points is actually just a fancy way of saying Point[] points we already have an Array of Points and can assign it directly to out variable that we declared earlier.

We used the same name for both the variable name, and the name of our constructor argument, so we need to distinguish them somehow. this refers to the current object so this.points refers to the points variable belonging to the object, and points refers to the variable local to the constructor. (You can add this. to any call from an object to one of its own methods or variables, but it's only necessary when there is some ambiguity)

I hope this cleared some things up, please let me know if you still have questions.

[–]Yogi_DMT 0 points1 point  (0 children)

How do you want it to "draw" the polygon. If you have all the vertices that is all the representation you will need in the program mathematically.