Question: In WhackAShape, we implement the methods bottom up. If method A depends on methods B and C, then methods B and C will be implemented
In WhackAShape, we implement the methods bottom up. If method A depends on methods B and C, then methods B and C will be implemented before method A. Remember to import cs2.* to access the CS2GraphWindowLib API classes.
buildShape(String input)
In the buildShape() method, you will parse the provided input to determine whether to create a CircleShape or SquareShape. Before you create the shape, you need to determine it's size, location and color.
Use TestableRandom to randomly generate an int size ranging from 100-200.
Randomly generate an x and y index for its location. X will range over the window's width, and y over the window's height. Use window's getGraphPanelWidth() andgetGraphPanelHeight() methods. Be sure to subtract the size you generated, since the object will be drawn from its upper left hand corner. We dont want part of the shape hanging off the edge of the screen.
Using the Strings contains() method and if statements, determine if it says red or blue. If neither of these colors are specified, throw a new IllegalArgumentException. This tells whoever gave the string to this method that their input was wrong. Allow the @throws tag to be generated.
Also check to see if the string contains circle or square. If it doesnt, throw a newIllegalArgumentException.
If the string does contain circle or square, use x, y, size, and color (same as in Project1, you will need to: import java.awt.Color;to access Color class.) as parameters to build the appropriate CircleShape or SquareShape. See the CS2GraphWindowLib API to look at their methods and constructors. Name your shape currentShape.
Tie currentShape to the clickedShape() method by calling its onClick method with the parameters this and clickedShape. Be sure to specify the methods name. Return currentShape.
clickedShape(Shape shape)
If the user has clicked the displayed shape, this should call the clickedShape() method. Here, we know the user has selected the Shape given as the Shape parameter. We now need to remove this Shape from the window, by calling the window's removeShape(shape)method, and from the bag, by calling the bag's remove(shape) method.
Once the clicked shape is removed, the next Shape needs to be displayed. Pick a new one from the bag using bags pick() method, and name it nextShape. Check to see ifnextShape is null.
If it is, it means that the bag is empty, and the game is over. Display a new TextShapesaying You Win! in the center of the screen. To get the x and y location of the center of the screen, use getGraphPanelWidth() and getGraphPanelHeight() on your Window.
Otherwise, add nextShape to the window using the windows addShape(nextShape)method.
clickedQuit(Button button)
Call System.exit() with a zero for its parameter. This means there was no errors.
getWindow() && getBag()
Implement these yourself.
WhackAShape()
In the default WhackAShape constructor, you will initialize your bag with SimpleArrayBagor SimpleLinkedBag, your choice. (Feel free to try both! It wont affect your WhackAShapecode.) Initialize your window, and add a Quit Button to your Windows East side.
Now you will fill up your bag with some Shape of red circle, blue circle, red square, and blue square. Save these strings in a String array. Generate a number from 6 to 14 (inclusive) which will determine your bag's size. Save that size in a variable. Randomly select one of the strings of the above array, call buildShape() on that string and add that Shape to your bag. Keep adding until your bag is of the generated size.
Add the first Shape to the Window by calling pick().
WhackAShape(String[] inputs)
Initialize your bag and window as well as add a a Quit Button as you did in the default constructor.
For each string in your input parameter, use the buildShape() method to convert them into Shapes. Surround this call with a try catch statement. If an exception is caught, youve tried to build a shape from a string which was not one of the four choices. Here it is possible that the string was wrong, since youre using a variable which you did not define.
Inside the catch clause, print out your exceptions error message by calling its printStackTrace() method. This is typically autogenerated.
Afterwards, add the shape returned from the buildShape method into your bag.
Once weve finished adding every shape from the input, add the first Shape to the window by calling pick().
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
