In: Computer Science
(JAVA) I was wondering if anyone can check if I am implementing this method correctly based on the instructions?
Here are the instructions:
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 don’t want part of the shape hanging off the edge of the screen.
Using the String’s 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 doesn’t, throw a newIllegalArgumentException.
If the string does contain “circle” or “square”, use x, y, size, and color as parameters to build the appropriate CircleShape or SquareShape. See the CS2GraphWindowLib APILinks to an external site. 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 method’s name. Return currentShape.
Here is my code for the method:
private Shape buildShape(String input) {
//randomly generate an int size ranging
from 100-200
randomGenerator = new
TestableRandom();
int low = 100;
int high = 200;
int size =
randomGenerator.nextInt(high-low) + low;
int xIndex=
randomGenerator.nextInt(window.getGraphPanelWidth());
int yIndex =
randomGenerator.nextInt(window.getGraphPanelHeight());
int xLocation = xIndex - size;
int yLocation = yIndex - size;
Shape currentShape;
if(input.contains("red")) {
currentShape.setForegroundColor(Color.RED);
}
else if(input.contains("blue")) {
currentShape.setForegroundColor(Color.BLUE);
}else{
try {
String color = input;
}
catch(IllegalArgumentException iae){
throw new IllegalArgumentException();
}
if(input.contains("square")) {
SquareShape(xLocation, yLocation, size, currentShape);
}
else
if(input.contains("circle")) {
CircleShape(xLocation, yLocation, size, currentShape);
} else {
try {
String shape = input;
}
catch(IllegalArgumentException iae){
throw new IllegalArgumentException();
}
}
}
return currentShape;
}
private Shape buildShape(String input) {
//randomly generate an int size ranging from 100-200
randomGenerator = new TestableRandom();
int low = 100;
int high = 200;
int size = randomGenerator.nextInt(high-low) + low;
int xIndex= randomGenerator.nextInt(window.getGraphPanelWidth());
int yIndex = randomGenerator.nextInt(window.getGraphPanelHeight());
int xLocation = xIndex - size;
int yLocation = yIndex - size;
Shape currentShape;
if(input.contains("red")) {
currentShape.setForegroundColor(Color.RED);
}
else if(input.contains("blue")) {
currentShape.setForegroundColor(Color.BLUE);
}
else if(input.contains("square")) {
SquareShape(xLocation, yLocation, size, currentShape);
}
else if(input.contains("circle")) {
CircleShape(xLocation, yLocation, size, currentShape);
}
else {
throw new IllegalArgumentException();
}
return currentShape;
}
As according to the question if input not have either color or shape we need to throw the exception so we can just do it once and as in question it is not mentioned to catch the exception so we don't need to catch it.
everything else is just amazing, nice code.
If the answer was helpful don't forget to upvote, It keeps me motivated :)
Thank you, Have a good day:)