Question

In: Computer Science

(JAVA) I was wondering if anyone can check if I am implementing this method correctly based...

(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;
     
   }

Solutions

Expert Solution

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:)


Related Solutions

Wondering if I am doing these calculations correctly I have a lab report due today. Questions...
Wondering if I am doing these calculations correctly I have a lab report due today. Questions Load P1 = 0 lb P2 = 350 lb P3 = 700 lb Gauge # Strain Stress Strain Stress Strain Stress 1 1102 724 383 2 1876 1546 1324 3 2177 2179 2181 4 876 1204 1558 5 662 989 1296 Sample Calculation: = s / e (10,600,000 psi)*1102 = 1.168 x 10^10 lb E = 10600 ksi = 10,600,000 psi
Hello, I am wondering how I can determine the direction in which a wave is travelling...
Hello, I am wondering how I can determine the direction in which a wave is travelling given a function I must verify if it first is a solution to the wave equation by finding the partial derivatives then I do not know how to extrapolate the direction and speed of a wave. Thanks
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
I was wondering if I was doing a conversion right. I am supposed to take aqueous...
I was wondering if I was doing a conversion right. I am supposed to take aqueous HNO3 with a density of 1.42 g/mL and a mass percent of solution of 70% and convert that into molarity. Would the correct conversion be to take 70 g of HNO3 over 100 g of H2O multiplied by 1 mol HNO3 over 63.01 g HNO3 (the molar mass) and then multiply that by 1 g H2O over 0.001 L of H2O to get the...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
Can anyone explain to me why I am getting an access violation when I try to...
Can anyone explain to me why I am getting an access violation when I try to add notes? It occurs when I try to set the "pLast" pointer to the previous node (for a doubly linked list). The code worked as singly linked list. When I added a "pLast" pointer and the needed code to make it a doubly linked list everything broke. #include <iostream> #include <stdlib.h> using namespace std; //build class that has a private function to inc count....
Can anyone give a detailed explanation with some examples on these questions? I am going to...
Can anyone give a detailed explanation with some examples on these questions? I am going to have an exam soon. 1. How should we account for the borrowing costs that we incurred this year on constructing the Torry warehouse facility? 2. Why would a machine, that we have started leasing on a 10 year agreement, have to be shown on our statement of financial position when we don’t own it? 3. The Board of ABC Corp., a manufacturer of electrical...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold...
Can someone check to see if I answered them correctly? The answers are highlighted in Bold font. I also need help with Question 6 1) What criteria must sales transactions meet in order for the seller to recognize revenues before collecting cash? a. The revenues must be earned (the firm must have achieved substantial performance). b. The amount to be received must qualify as an asset (there must be a future economic benefit and the amount must be measured with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT