# Java Polygons NEED HELP

*label*Computer Science

*timer*Asked: Jul 13th, 2015

**Question description**

n elementary mathematics of 2-dimensions, each point on the plane (or paper or screen, if you like) can be represented by a pair of numbers, **(x, y)**, where **x** tells you the horizontal location of the point and **y** tells you the vertical location. The values are relative to some * "origin" (0,0)*. So

**(3.1, 0)**is the point

**3.1**units

*to the right*of the origin, and

**(-3.6, 5)**is the point

**3.6**units

*to the left*of the origin and

**5**units

*above it*. This allows us to label every point in the plane with a unique

**(x,y)**coordinate pair. The two numbers,

**x**and

**y**, are called

**of the point.**

*the coordinates* Create a class called **Point** that has the following **private** members:

**float x**- theof the point.**x-coordinate****float y**- theof the point.**y-coordinate**

**Point** should also contain

**private static float MIN_VAL**and**MAX_VAL**- which are initialized to**-10.0**and**+10.0**, respectively. These will represent the legal range of both the**x**and**y**coordinates for all**Points**in the class. It is important that these be**static**values since all objects will share the same range limits.

If you wish, you can let **x** and **y** (and the two statics) be** doubles**. If you do, adjust the data types of corresponding members below to be compatible with your choice.

Supply the following * instance members*:

,**Two Constructors**- one that takes
*no arguments*which constructs a default point of**(A, A),**where**A = (MIN_VAL + MAX_VAL) / 2**(which would be**(0,0)**if**MIN_VAL**= -**MAX_VAL**), - and another that takes
*two arguments*,**x**and**y**which constructs a point (**x, y**).

- one that takes
**boolean set(float x, float y)**- a*mutator*which sets the coordinates of the point to**x**and**y**.**float getX()**and**float getY()**,*accessors*for the**x**and**y**coordinates of the**Point**.**displayPoint()**- displays the**Point**in the form "**(0.46, -0.102)**"

Also create a public * static method*:

**boolean setRange(float newMinVal, float newMaxVal)**- athat changes the**static method****floats****MIN_VAL**and**MAX_VAL**to the values passed (if successful). This method must check that**newMinVal**<**newMaxVal**and return**true**or**false**, accordingly. Note that any changes made to**MIN_VAL**and**MAX_VAL**through this method will not enforce the range of any previously constructed**Points**, but will only affect future constructor and mutator calls.

Create another class called **Polygon** that has the following members:

**numPoints**- a private*instance member,*an**int**, that describes the number of**Points**in the current**Polygon.****points[]**- an array of**Points**representing the list of points that define this**Polygon**. This is a private instance member.**MAX_POINTS**- a*static final*that gives the maximum number of**Points**allowed in the array (can be used to declare the array if you wish). Note that**numPoints**could be much smaller than**MAX_POINTS**. For example, you might choose**MAX_POINTS**to be 100, but three different objects might have**numPoints**set to 3, 13 and 7.

You should supply all of the following (at a minimum):

that takes no parameters and sets the number of points to 0.*A Constructor*that takes three parameters.**A Constructor**- The first parameter is an
**int**:**numPoints**, which indicates how many points the newly constructed**Polygon**will contain. - The second is an array of
**floats**:**xArray[]**, which contains all theof the**x-coordinates****Points**in the newly constructed**Polygon**. - The third is an array of
**floats**:**yArray[]**, which contains all theof the**y-coordinates****Points**in the newly constructed**Polygon**.

For example, the

of the first (really 0th)**x-y coordinates****Point**in the**Polygon**would come from**xArray[0]**and**yArray[0]**, theof the second (really 1st)**x-y coordinates****Point**in the**Polygon**would come from**xArray[1]**and**yArray[1],**theof the third (really 2nd)**x-y coordinates****Point**in the**Polygon**would come from**xArray[2]**and**yArray[2]**, and so on. (The two arrays are calledbecause they work together "in parallel" to specify the**parallel arrays****Points**: if you want to get the coordinates of the**k**th**Point**in the**Polygon**, you would use the**xArray[k - 1]**for theand**x-coordinate****yArray[k - 1]**for the.)**y-coordinate**The actual arrays passed might be larger than the

**int numPoints**passed, and that's okay. We only use the first**numPoints**elements of the two arrays. For example, the arrays might each be 100 elements, but if**numPoints**is**3**, we are only using the first**three**elements of each array.*Only supply these two constructors and no others.*- The first parameter is an
**boolean setPoints(int numPoints, float xValues[], float yValues[])**- a*mutator*that takes an**int**,**numPoints**, and two**float***arrays*. The two**float***arrays*provide theof the**x-y coordinates****Points**in the requested**Polygon**in the same manner described in detail for the constructor, above.*This method overwrites any values in the existing object, and creates a fresh Polygon from scratch - it does not append points to the existing Polygon.*Return**false**(error) if the internal**points[]**array is smaller than**numPoints**or if**numPoints**is < 0. There is another possible reason for failure (**false**return) that you must consider - this is part of your assignment. Do not ask what it is, but figure it out based on the information above.**void showPolygon()**- displays the points in the**Polygon**as a text string such as "(3.5, 1.2), (-9.1, 5.5), (0.120, -0.30)", ...."**boolean addPoint( float x, float y)**- adds (appends) a new**Point**to the list, but only if there is room!**boolean addPoint (Point p)**- adds (appends) a new**Point**to the list, but only if there is room!

As always, make sure that *mutators*, *constructors* and any methods that affect private data filter out bad parameters from the client.

This example gives lots of opportunity to use/invoke some methods to make other methods' lives easier. Make sure you are not duplicating code, but exploiting this opportunity.

We don't care if a **Polygon's** points cause it to be drawn with sides crossing (if we were to draw it, which most of you will not). Any list of **(x,y)** pairs will be considered a valid **Polygon**, assuming that they are valid **Points**.

**Polygon** and **Point **should be classes *distinct* from (and not contained within) your main class (which we call **Foothill**). However, you can and should defined them as a non-public classes so it can reside in the same file, **Foothill.java**.

Generate a sample **main()** that tests your classes.

- Establish one set of min/max values for the statics in the
**Point**class using the static*mutator*. - set and display a few
**Point**objects manually. Check that your range limitation works by attempting to set values of one**Point**outside the range and proving that your**Point**rejects this attempt. - Next, instantiate three
**Polygons**manually from literal values in your program, i.e., do not involve the user with run-time input. - Two
**Polygons**should bewith**constructed****float**arrays arguments for the**x**and**y**values, instantly populating the**Polygon**. The third should be instantiated using the default constructor, then filled with points using the*mutator*methods. Use different coordinate values for the**Points**for all three**Polygons**(i.e., don't create two identical Polygons). - Display the
**Polygons**. - Make changes to the
**Polygons**using the*mutators*of that class and display them again. - In at least one Polygon
*mutator*call, attempt to set values illegally (in any sense of the word based on the above criteria) and prove that your class protects against this attempt.