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...

1 answer below »

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.



Answered Same DayMar 04, 2021

Answer To: Create a simple implementation of theLights Outgame. Create class LightsOutwith the following public...

Mohd answered on Mar 06 2021
152 Votes
Completed Solution/Light Out Game/.classpath

    
    
    
Completed Solution/Light Out Game/.project

     Light Out Game
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.co
re.javanature
    
Completed Solution/Light Out Game/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Completed Solution/Light Out Game/bin/java.policy.applet
/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/
/* DO NOT EDIT */
grant {
permission java.security.AllPermission;
};
Completed Solution/Light Out Game/bin/LightsOut.class
public synchronized class LightsOut {
private int m;
private int n;
private javax.swing.JButton[][] arrayButton;
public void LightsOut(int, int);
public void reset();
public boolean getPatch(int, int);
public void setPatch(int, int, boolean);
public void togglePatch(int, int);
public javax.swing.JButton[][] getArrayButton();
public boolean isDark();
public boolean isWhite();
}
Completed Solution/Light Out Game/bin/LightsOutTest.class
public synchronized class LightsOutTest extends javax.swing.JFrame implements java.awt.event.ActionListener {
private javax.swing.JButton[][] arrayButton;
private javax.swing.JLabel countLabel;
private int count;
int size;
static LightsOut light;
public static void main(String[]);
private void inputUser();
public void LightsOutTest();
public void actionPerformed(java.awt.event.ActionEvent);
private void changingColor(javax.swing.JButton);
private void checkWin();
}
Completed Solution/Light Out Game/src/LightsOut.java
Completed Solution/Light Out...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here