0% found this document useful (0 votes)
13 views7 pages

A4-Rectangle Plotter

abc 11

Uploaded by

Shivansh Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

A4-Rectangle Plotter

abc 11

Uploaded by

Shivansh Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LANGARA COLLEGE

COMPUTER SCIENCE 1150

ASSIGNMENT #4 – Rectangle Plotter


March 5, 2024 Bryan Green
Due by midnight on: March 18, 2024

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

Appendix – How to Use 3rd Party Libraries


There will be times when you need to use 3rd party libraries to accomplish some tasks or provide
some features that are not a standard part of the Java programming language. Generally, the
process involves creating a project, putting the library in the right place on your disk and finally
informing your development framework of where the library is. Each IDE has its own set of steps
for how you do this.
The supplied zip file contains two things– a Grid.jar file, which contains the bytecode for the
required Grid class, and an entire folder containing the complete documentation for this class.
The documentation you can view with your favourite browser. The jar file, on the other hand,
cannot be “viewed”, but instead needs to be integrated into your Java development environment.
Using VSCode
Since you are working with two files now (one is yours and one is mine!), you should create a
Java project to hold the entire thing together.
1. Create a Java project for this lab with no build tools.

2. Select a location for the project.

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. App.java is a terrible name for your program.


Rename it to something more appropriate.
a. When you change the name on the file, what
else needs to change?
b. You can also delete the “throws Exception” from the main method.
6. Now that you have a project you need to get the supplied zip file extract it and put the
items where they belong.
a. Use file explorer and put the Grid.jar file into the lib folder. When you come
back to your IDE it should automatically be found and usable. Don’t bother
trying to open the jar file; it is compiled byte code. No, I won’t give you the
source code either, so don’t bother asking.
b. Put the entire docs folder into the project folder.
i. In here is the HTML documentation for the entire library that I’ve
provided. Use your favourite browser to view the index.html file. It

5
LANGARA COLLEGE
COMPUTER SCIENCE 1150

contains everything you need to know about the functionality of the


library. Read it.
c. From within VSCode | Explorer, verify that the docs folder is inside the
Rectangle folder and the Grid.jar file is sitting in the lib folder.

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.

Using Command Line


You’ll need to specify the classpath so the compiler and JVM know where to go looking for the
library.

javac -cp path/to/Grid.jar yourSourceFile.java


java -cp ./:path/to/Grid.jar yourByteCode

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.

You might also like