Question
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have
SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
- Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
- Unless otherwise noted in the question, assume that parameters in method calls are notnulland that methods are called only when their preconditions are satisfied.
- In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.
This question involves theDrawclass, which is used to draw line segments and squares on a10
-by-10
xy
-coordinate grid.
public class Draw
{
/**Precondition:All parameters are between0and10,inclusive.
*Draws a line segment in a10
-by-10
xy
-coordinate grid.
*The line segment is drawn from(x1, y1)to(x2, y2).
*/
public void drawLine(int x1, int y1, int x2, int y2)
{ /*implementation not shown*/ }
/**Precondition:0 ? x 0.
*Draws a square on a10
-by-10
xy
-coordinate grid
*and prints the square's side length and area.
*The upper left corner of the square will be located
*at the coordinate(x, y)and the side length of the
*square will belen(or as large as will fit in the grid).
*/
public void drawSquare(int x, int y, int len)
{ /*to be implemented*/ }
}
ThedrawLinemethod, whose implementation is not shown, is used to draw a line segment from the coordinate(x1, y1)to the coordinate(x2, y2). For example, the calldrawLine(2, 5, 6, 4)will produce the figure below.
You will write methoddrawSquare, which draws a square on a10
-by-10
grid and prints the square's side length and area in the format shown in the examples. The upper left corner of the square is located at the coordinate(x, y). The sides of the square will be of lengthlen, or as large as will fit on the grid.
The following examples show the intended behavior ofdrawSquare.
Example 1
The calldrawSquare(1, 4, 3)will produce the figure below. The upper left corner of the square is at the coordinate(1, 4)and the square has sides of length3.
The call will print"side length = 3, area = 9".
Example 2
The calldrawSquare(6, 8, 5)will produce the figure below. A square whose upper left corner is at the coordinate(6, 8)cannot have sides of length5and still fit on the grid. The largest possible square (with sides of length4) is drawn.
The call will print"side length = 4, area = 16".
Example 3
The calldrawSquare(7, 2, 4)will produce the figure below. A square whose upper left corner is at the coordinate(7, 2)cannot have sides of length4and still fit on the grid. The largest possible square (with sides of length2) is drawn.
The call will print"side length = 2, area = 4".
Write the methoddrawSquarebelow.
/**Precondition:0 ? x 0.
*Draws a square on a10
-by-10
xy
-coordinate grid
*and prints the square's side length and area.
*The upper left corner of the square will be located
*at the coordinate(x, y)and the side length of the
*square will belen(or as large as will fit in the grid).
*/
public void drawSquare(int x, int y, int len)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started