177k views
1 vote
I need help, i am coding this question i checked all the internet and i cant find method to move rectangle. Please help you dont have to code for me just explain to me. Thanks.

Its java language i want know how to move the box1 rectangle, the question is complete.Now use the Rectangle class to complete the following tasks: Create another object of the Rectangle class named box2 with a width of 100 and height of 50. Display the properties of box2. Call the proper method to move box1 to a new location with x of 20, and y of 20. Call the proper method to change box2's dimension to have a width of 50 and a height of 30. Display the properties of box1 and box2. Call the proper method to find the smallest intersection of box1 and box2 and store it in reference variable box3. Calculate and display the area of box3. Hint: call proper methods to get the values of width and height of box3 before calculating the area. Display the properties of box3.

1 Answer

3 votes

Answer:

To move box1, you can use the setLocation() method of the Rectangle class. The syntax for this method is setLocation(int x, int y), where x and y represent the new x and y coordinates of the rectangle respectively. For example, to move box1 to (20, 20):

box1.setLocation(20, 20);

To change the dimensions of box2, you can use the setSize() method. The syntax for this method is setSize(int width, int height), where width and height are the new width and height of the rectangle respectively. For example, to change box2's dimensions to have a width of 50 and a height of 30:

box2.setSize(50, 30);

To display the properties of box1 and box2, you can use the toString() method of the Rectangle class. This will print out the x and y coordinates, width, and height of each rectangle. For example:

System.out.println("box1: " + box1.toString()); System.out.println("box2: " + box2.toString());

To find the smallest intersection of box1 and box2, you can use the intersection() method. This method takes two Rectangle objects as arguments and returns a new Rectangle object that represents the intersection of the two rectangles. For example:

Rectangle box3 = box1.intersection(box2);

To calculate the area of box3, you can use the getWidth() and getHeight() methods to get the width and height of box3, and then multiply them to get the area. For example:

double area = box3.getWidth() * box3.getHeight();

Finally, to display the properties of box3, you can use the toString() method again:

System.out.println("box3: " + box3.toString());

User RxRead
by
8.4k points