Create a simple implementation of theLights Outgame. Create class LightsOutwith the following public methods:
- LightsOut(int m, int n) - create a game field with m rows and n columnsof patches,and randomly initialize it by calling reset(), see below
- void reset() - randomly initialize the game field
- boolean getPatch(int x, inty) - get the value of the patch in the x'throw and y'thcolumn; if either x or y is not a valid position, throw an exception
- void setPatch(int x, int y, boolean newValue) - setthe value of the patch in thex'throw andy'thcolumn to newValue; if either x or y is not a valid position, throw an exception
- void togglePatch(int x, int y) - toggle the state ofthe patch in thex'throw andy'thand its immediate 4 neighbors; if either x or y is not a valid position, throw an exception
- boolean isDark() - return true if all patches are dark (set to true) and false, otherwise
- booleanisWhite() - return true if all patches are white (set to false) and false, otherwise
Represent the field as a private 2D boolean array. Assume that the array is wrapped-around both horizontally and vertically (the right neighborof a patch in the m'thcolumn is a patch in the same row in the 1st column, etc).
Create the main class LightsOutTest. Ask the user to enter the size of the field (m and n) and create a LightsOutobject of the appropriatesize. Toggle random patches until the field becomes either completely dark or white, whatever comes first. Display the number of steps.
Problem 2
Create class Polygon that represents apolygonas an array of Point2D (see section 9.6.3). Implement the following public methods:
- Polygon(Point2D[] vertexes) - create a new Polygon with the vertexes as described in the parameter. The method must create an internal private attribute for the vertexes andcopythe points from the parameter to the new array (because the value of the parameter may change after the creation of the polygon and you do not want the shape of the polygon to change, too).
- double area() - calculate and return thearea of the polygon
- double perimeter() - calculate and returnthe perimeter of the polygon; do not forget that the closing edges of the polygon starts at the last vertex of the array and ends at the first vertex
- Polygon extents() - calculate the extent of the polygon (the smallest upright rectanglethat entirely encloses the polygon); the extent is also a Polygon, because every rectangle is a polygon.
- String toString() - return the content of the array of vertexes as a string (in any recognizable way); use Point2D.toString() to serialize individual vertexes.
The latterfourmethods will always return the same values because the shape of the polygon cannot be changed. Implement the methods in such a way that they calculate the respective values only once, at the first call, and then simply return the previously computedcachedresults.
Create the main class PolygontTest. Create several polygons (a triangle, a square, a general quadrilateral, a pentagon) and report their areas, perimeters, and extents.