A4-Rectangle Plotter
A4-Rectangle Plotter
Learning Outcomes
By the end of the assignment, you will be able to:
• Write a Java program from scratch using selection, loops and methods.
• Use top-down design to break down a large problem into much smaller solvable pieces.
• Install a 3rd party library into your IDE
Preamble
This program will accept commands from the user to draw filled-in rectangles within the positive
Cartesian quadrant. The program should accept the (x,y) coordinate pair for the top left corner of
the rectangle, along with the rectangle’s width and height. After accepting the input, it should
then draw the rectangle onto a 40 x 20 labelled grid. The user should be able to draw a
maximum of 5 rectangles on the same grid.
For example, the first rectangle has a top left corner of (15, 10) with a width of 4 and height of 7.
The second rectangle is at (30, 15) with a width of 2 and height of 3; the third is at (31, 13) with
a width of 5 and height of 2. The second and third rectangles overlap.
^
20 +
|
|
|
|
15 + **
| **
| ******
| *****
|
10 + ****
| ****
| ****
| ****
| ****
5 + ****
| ****
|
|
|
0 +====+====+====+====+====+====+====+====+>
0 5 10 15 20 25 30 35 40
Problem Statement
1. Design, by writing an algorithm, and test a solution to the above problem. You may discuss
and work together to develop an algorithm – I encourage it.
2. Now use Java to implement your algorithm. This is to be done individually.
1
LANGARA COLLEGE
COMPUTER SCIENCE 1150
Hints
• Top-down design is important in this assignment. Design each method carefully and what it
should do – remember functional cohesion, coupling and procedural abstraction.
• Use the Grid datatype that I’ve provided to you as a 3rd party library. You can think of it like
a virtual piece of graph paper that you will draw on. See the appendix for how to install the
jar file and the related documentation.
• Once you write something on the terminal screen you cannot “back up”; that is, once
anything is outputted to the screen you cannot go to the left or up, you can only go down or
to the right.
• You will need to validate all user input – that is, test all input to make sure they are
acceptable. For instance, each rectangle must fit on the grid. What does that imply about the
top left corner? The width and height?
• Provide error messages for erroneous inputs and then permit the user to try again.
• Use constants where appropriate (minimum and maximum X values, etc.) and base the rest of
your source code off these constants.
• Methods are useful friends; make them; use them. Methods can return at most one value to
you.
• Documentation (program header comments and method comments) is also necessary.
Remember to use Javadoc format for your comments.
• Arrays are not helpful for this assignment; please don’t use them. I don’t want to give
penalty marks.
What to Hand in
Submit a single zip file to Brightspace before the due date containing:
• Source code (*.java) of your final program after all the modifications have been
made. Be sure that the comments and printIdentification method contain your name,
student number, and course and section numbers. Remember to include instructions.
• Output listings (*.txt) from running your program showing that it works on multiple
test cases.
2
LANGARA COLLEGE
COMPUTER SCIENCE 1150
Mark Allocation
OUT OF CRITERIA
Style
/1 • Whitespace, brace placement, etc.
/1 • Good identifiers
/2 • Method / program Javadoc style documentation
Source Code
/1 • Use of constants
/3 • Input validation and error messages
/2 • Loops for each axis and its labels
/1 • Good use of method parameters
/5 • Nice cohesive methods
/1 • Permits drawing a maximum of 5 rectangles
Sample Output
/3 • Nicely labelled layout, multiple tests
Deductions
/-3 • Class scoped variables (keyboard is okay)
/-5 • Using arrays for the grid or the rectangle
/20 Total
3
LANGARA COLLEGE
COMPUTER SCIENCE 1150
4
LANGARA COLLEGE
COMPUTER SCIENCE 1150
3. Finally give the project a name. In this example, Rectangle is the project name. This
name also becomes a new folder located within the folder from the previous step.
4. This will create the following folder structure on your disk that you should maintain.
a. bin is where the compiled bytecode will be.
b. lib is where the extra libraries will be
installed.
c. src is where your source code is located.
5
LANGARA COLLEGE
COMPUTER SCIENCE 1150
i. If you do not see the Grid.jar file sitting inside the lib folder, then you
may need to go back to the previous step and try again.
ii. If that still fails, then open VSCode | Explorer and then down in the Java
Projects section you should have the picture below. If so, you can choose
the + beside the Reference Libraries and then connect it to the required jar
file.
7. Finally, type in the following program and run it. Notice the import statement at the
top. This line imports the 3rd party library into your source code, so it is now usable.
When this simple program works and you get the output as seen in the terminal window,
then you are ready to complete the rest of assignment.
6
LANGARA COLLEGE
COMPUTER SCIENCE 1150
8. IF you get an error message: Grid cannot be resolved or Grid cannot be resolved to a type
then you are not referencing the library properly yet. There are several possibilities:
a. The import statement is incorrect,
b. The jar file is in the wrong place,
c. The jar file is not being referenced properly,
Go back and retry.
Often you can make your life easier and put the jar file into the same place where your source
code is – less to type!
In the second command above, the part after the -cp flag shows that there are two paths that need
to be examined. The first is ./ which is the current folder, and then within the jar file itself. In
this case the colon (:) is just there to separate the two paths.
If All Else Fails
If all else fails, unzip the jar file and put the .class files into the same folder where your own
bytecode resides. (This is not necessarily the same place where your source code resides!) This
will work but is definitely fiddlier for you.