CECS 277 – Lab 5 – Classes solution

$30.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Drawing Rectangles

Create a program that draws a rectangle on a grid that starts at (0,0) and then moves
around the screen based on inputs from the user.

Create a Rect class (in its own file: Rect.java) with the following properties:
Instance Variables Methods
1. x 1. Constructor – pass in w and h, sets width and height.
2. y 2. getX and getY – returns x and y value respectively.
3. width 3. getWidth and getHeight – returns the width and height.
4. height 4. translate – pass in dx and dy, then add to x and y.

In a separate file (Main.java), create a Main class and a main method. Then create a
20×20 2D array of characters (the grid), then prompt the user to enter the width and
height of the rectangle (1-5). Construct a Rect object using the width and height entered.

Create the following methods:
1. displayGrid – pass in the grid, then display the contents of the grid.
2. placeRect – pass in the grid and the rectangle. Go to the location in the grid at the
rectangle’s x and y location, then, using ‘*’s, fill in the grid using the rectangle’s
width and height to draw the rectangle on the grid.

3. resetGrid – pass in the 2D array, and fill the grid with ‘.’s.
4. menu – display the menu below, prompt the user for their input and return it.
Reset the grid, place the rectangle on it, display the grid, and then display the menu.
Allow the user to choose which direction to move the rectangle. Do not allow the user to
move any part of the rectangle outside of the bounds of the grid. If an edge of the
rectangle reaches a wall, then it should not be able to move any farther in that direction.
Repeat until the user decides to quit the program.

You can use the CheckInput class functions to make sure that all user inputs are valid and
within the bounds of the input.

Make sure you’re using the Rect class that you created for all rectangles constructed in
your program (not the Rectangle class in java.awt). Document all methods using
Javadocs style comments.

Example Output:
Enter rectangle width (1-5)? w
Invalid input.
Enter rectangle width (1-5)? 3
Enter rectangle height (1-5): 2
* * * . . . . . . . . . . . . . . . . .
* * * . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
©2021 Cleary
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Enter direction:
1. Up
2. Down
3. Left
4. Right
5. Quit
1
You cannot go that way.
2
. . . . . . . . . . . . . . . . . . . .
* * * . . . . . . . . . . . . . . . . .
* * * . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .