You are on page 1of 6

Computer Engineering Department

Lab Assignment 06
2020 – 2021 Fall, CMPE 211 Fundamentals of Programming II
Drawing Tool - Part One

In this assignment, you will write a Java program to develop a simple drawing tool. The outline of the class will
be given to you (Lab6Driver.java) and you need to complete the missing parts in this file.

Missing parts are explained with TODO comments.

The Lab6Driver will read user commands and create some shapes according to these commands. The
program will work on a two dimensional virtual coordinate system. X and Y coordinates will define a grid
where the starting point is (0,0) as below:

You will use the Point class (from the java.awt package) to represent a location in this coordinate system.
Point class has integer x and integer y instance data. This means that any Point object is defined as:

Pointp = new Point(x, y);

1
Computer Engineering Department

In addition to the driver class, you need to write one abstract class called Shape, 3 subclasses that extends
Shape class, and 1 interface called Movable. The class diagram of the program is as follows:

Shape class:

As the diagram shows, Shape class will have a leftTop variable to keep the left top point information for a
shape. leftTop point coordinates will be defined by the user command.

isCircular variable is to identify circular objects. This data can be false by default, and can be set to true for
circle objects.

Shape class will have a points ArrayList as well. This list will keep the ordered list of vertices (starts from
leftTop) for a shape. For each shape, calculatePoints() method will define the points and add them to the list.

For each shape, calculateArea() and calculatePerimeter() methods will calculate the area and perimeter.

2
Computer Engineering Department

Calculating the points:

The following figure


shows an example for
the leftTop point of a
rectangle.

The other points can be calculated as follows:

The order will be: leftTop, rightTop, rightBottom and leftBottom.

Square class can be implemented in a similar way.

3
Computer Engineering Department

For circle class, there will be only two points, leftTop and rightBottom as shown below:

User Commands:

There will be a shapes list in the main method:


ArrayList<Shape>shapes = new ArrayList<Shape>();
Your program will get input from the user to perform the following operations on this list.

1) Create a Rectangle object and add it to the shapes list.


addR <x_coordinate_leftTopPoint><y_coordinate_leftTopPoint><height><width>
For example: addR 100 100 50 200
This command will add a rectangle with height=50 and width=200. The leftTop point of the rectangle is
(100,100).
After addition, you will print the information of the object on the terminal.

2)Create a Square object and add it to the shapes list.


addS <x_coordinate_leftTopPoint><y_coordinate_leftTopPoint><edge_length>
For example: addS 200 250 30
This command will add a square with edge=30. The leftTop point of the square is (200,250).
After addition, you will print the object.

3) Create a Circle object and add it to the shapes list.


addC <x_coordinate_leftTopPoint><y_coordinate_leftTopPoint><radius>
For example: addC 150 280 50
This command will add a circle with radius=50. The leftTop point for this circle is (150,280).
After addition, you will print the object.

4
Computer Engineering Department

4) Move an object in the shapes list to a new location.


move <shape_index><x_coordinate_newLeftTopPoint><y_coordinate_newLeftTopPoint>
For example: move 3 30 200
This command will move the third shape in the list to a new location, i.e. the third shape’s
leftTop point will be updated as (30,200) and the other points will be re-calculated.
After updating, you will print the object.
(Note: shape_index starts from 1.)

5) The program will stop when the user enters “exit” .


The detailed description of the print commands will be provided during the last session of the lab.

5
Computer Engineering Department

Sample Execution:

Enter the command: addR 100 100 50 200


Rectangle[h=50,w=200] Points[(100,100)
(300,100)(300,150)(100,150)] Area=10000,0
Perimeter=500,0
Enter the command: addS 200 250 30
Square[e=30] Points[(200,250)(230,250)
(230,280)(200,280)] Area=900,0
Perimeter=120,0
Enter the command: addC 150 280 50
Circle[r=50] Points[(150,280)
(250,380)] Area=7854,0
Perimeter=314,2
Enter the command: move 3 30 200
Circle[r=50] Points[(30,200)
(130,300)] Area=7854,0
Perimeter=314,2
Enter the command: exit

NOTE: In the template Driver class, there are two lines with comment tag GRAPHICS. If you successfully
implement the program, you can uncomment those lines to see a graphical representation of the shapes.
More information will be given by the instructors during the lab. You need to submit your program on Moodle
without Graphics part. For example, for the sample execution above, we get the following drawings:

After the add commands After the move command

You might also like