You are on page 1of 8

CO320 Assignment 3

Introduction
This assignment is presented in two stages. The first stage is worth 50% of the full
assessment mark and does not involve the use of inheritance. The second stage
makes up the rest of the marks and takes advantage of inheritance.

Date set: 7th December 2015


Deadline: 23:55 20th January 2016

Plagiarism and Duplication of Material

The work you submit must be your own. We will run checks on all submitted work in
an effort to identify possible plagiarism, and take disciplinary action against anyone
found to have committed plagiarism.

Some guidelines on avoiding plagiarism:

• One of the most common reasons for programming plagiarism is leaving work
until the last minute. Avoid this by making sure that you know what you have
to do (that is not necessarily the same as how to do it) as soon as an
assessment is set. Then decide what you will need to do in order to complete
the assignment. This will typically involve doing some background reading
and programming practice. If in doubt about what is required, ask a member
of the course team.

• Another common reason is working too closely with one or more other
students on the course. Do not program together with someone else, by which
I mean do not work together at a single PC, or side by side, typing in more or
less the same code. By all means discuss parts of an assignment, but do not
thereby end up submitting the same code.

• It is not acceptable to submit code that differs only in the comments and
variable names, for instance. It is very easy for us to detect when this has
been done and we will check for it.

• Never let someone else have a copy of your code, no matter how desperate
they are. Always advise someone in this position to seek help from their class
supervisor or lecturer. Otherwise they will never properly learn for themselves.

• It is not acceptable to post assignments on sites such as RentACoder and we


treat such actions as evidence of attempted plagiarism, regardless of whether
or not work is payed for.

Further advice on plagiarism and collaboration is also available.


You are reminded of the rules about plagiarism that can be found in the Stage I
Handbook. These rules apply to programming assignments. We reserve the right to
apply checks to programs submitted for assignment in order to guard against
plagiarism and to use programs submitted to test and refine our plagiarism detection
methods both during the course and in the future.

The Task
The task involves implementing a sort of simple interactive language that can
manipulate the shapes seen in the figures project from chapter 1. The assignment
files use a different version of that project – one that includes the use of inheritance
to avoid code duplication. Inheritance is covered in chapters 8 and 9 of the course
book.

You have been provided with a starting point for the assignment:

https://www.cs.kent.ac.uk/~djb/co320/LOCAL-ONLY/assign3.zip

You are provided with the source code of four shape classes – Circle, Person,
Square and Triangle – but the BlueJ editor for these classes is currently showing
the Documentation view of them rather than the Source Code view. The
Documentation view is simply the javadoc documentation for the classes that has
been generated from the comments and method headers that are in the source
code. The idea is that it should be easy to use these classes without needing to see
details of the source code. You can flip the views by using the drop-down menu in
the top-right corner of the editor window. In essence, these classes have exactly the
same methods as those in the figures project from chapter 1, but their
implementation differs slightly.

Your task is to complete the ShapeProgrammer class so that users can


interactively create and manipulate shapes to create pictures.

Requirements
The following sections give details of the tasks that are required. In completing
these, please bear in mind that all the code you write should be well laid out and the
fields, methods and constructors should be properly documented, using the style that
is used in this course. In particular, use of javadoc-style notation is a requirement
and adherance to these will be included in the marking scheme.

If in doubt about good style, a style guide is available: http://www.bluej.org/objects-


first/styleguide.html.

There should be no need to change the source code of any of the provided classes
with the exception of ShapeProgrammer. If you feel that you need to make changes
to other classes then ask me whether this is acceptable or appropriate before doing
so.
Background
The idea is that users should be able to interactively create, name and manipulate
shapes by typing commands into the terminal window. If you create a
ShapeProgrammer object and then call its program method, the following will
appear in the terminal window:

Welcome to the shape programmer.


Type commands like:
circle
to create a circle.
Type:
quit
to finish.
>

If you type circle at the prompt (>) in the window then a blue circle will appear on
a canvas. If you type quit then the program method will terminate. That is pretty-
much all of the functionality that is currently available in the ShapeProgrammer
class. Your task will be to add more.

How it currently works


A ShapeProgrammer object uses an InputReader object to prompt for and read
input from the user in the Terminal Window. Whatever the user types at the
command prompt is read by the InputReader and separated into individual words
– just as is done in the tech-support project in chapter 5. However, in this project the
words are not stored in a HashSet but returned from the InputReader’s
getInput method as an array of individual String objects. For instance, if the user
typed:

> circle color red

these would be returned to the program method of ShapeProgrammer as an array


containing three String objects: "circle", "color" and "red".

What to add
Your task is to enhance the ShapeProgrammer class so that users can create and
name shapes, and then manipulate those shapes via the names they have been
given. The manipulations will simply be calls to the standard methods of the shape
classes, such as makeVisible, moveLeft, etc. In Stage One of the assignment,
you work with just Circle objects and you can safely ignore the other shapes for the
time being. In Stage Two, it should be possible to create and manipulate the other
shapes in the same way as for circles.
The shape language – Stage One (50%)
The ‘shape language’ you will implement must implement the command set defined
below. Note that, in stage one, this language only applies to Circle objects. You
can ignore all other shape types. The commands of the language are defined as
follows:

• circle name

where name may be any string of one or more characters that does not
include a whitespace character (space, tab or newline). This means, create a
Circle object and associate it with the given name. For instance:

> circle ball


> circle very-large-ball

Storing an association between the name and the created object is very
important because the name will be used in later commands to refer to that
particular Circle object. Note that ‘name’ here does not mean a variable name.
Inside the program ‘name’ will be represented as a String, such as "ball" or
"very-large-ball" retrieved from the array of strings returned by the
InputReader. Hint: Use a HashMap to create the association between the
name and the created Circle object.

A newly created Circle object should be invisible to start with.

• name left

Call the moveLeft method on the Circle object that was previously created
with the given name. For instance:

> very-large-ball left

For this to work, there must have been a previous command to create a Circle
object with the associated name very-large-ball.

• name no-parameter-method

Call a particular method on the Circle object known as name. The string no-
parameter-method will be your chosen name for any of the following
methods of a Circle: moveLeft, moveDown, moveUp, moveRight,
makeVisible, makeInvisible. All of these methods do not take any
parameters.
Note that the string used to select a particular method does not have to
exactly match the method name. For instance, in the previous example we
used left as the word to call the moveLeft method. Here we are just
extending that idea to cover all of the methods that do not take parameters.

So you might type:

> ball visible

to call makeVisible on the Circle known as ball, for instance.


Note that methods taking parameters – such as moveHorizontal – are
excluded here.

• forget name

This command should remove the association between name and its
associated Circle object. In effect, the name should be removed from the
HashMap. For instance:

> forget sun

If no Circle object has been created with that name then an error message
should be printed to tell the user that this is the case. The Circle object that is
being forgotten should be made invisible so that it no longer appears on the
drawing. Any future commands referring to sun would now produce an error
message, unless a fresh shape is created and the name reused.

• name colour colour-string

Use the changeColor method of Circle to change the colour of the Circle
associated with name to be the given colour. The colour-string should be one
of: "black", "blue", "green", "magenta", "red", "white",
"yellow". If colour-string is not one of the recognised colour strings
then print an error message. NB: You may use either the US or UK spelling of
colour in this command.

An example script for stage one


Here is an example script that uses the language of stage one to create and
manipulate some shapes to create the picture on the right:
> circle sun
> sun colour yellow
> sun visible
> sun down
> sun down
> sun down
> circle moon
> moon visible
> moon black
Unrecognised operation:
black
> moon colour black
> moon up
> moon left
> quit

The shape language – Stage Two (40%)


Extend your language from stage one so that it can be applied to any of the
remaining shapes: Person, Square and Triangle. However, you should use just
one HashMap to store the associations between names and shapes. Don’t define a
separate map for each type of shape. A particular name is only allowed to refer to
one type of shape at a time.

You will obviously need to add an additional shape-creation command for each of
the additional three shapes:

• person name

Create a Person object and associate it with the given name.

• square name

Create a Square object and associate it with the given name.

• triangle name

Create a Triangle object and associate it with the given name.

In fact, this extension should not require very much work, because polymorphism
provides a simple solution.

• Add commands that will allow the following four methods to be called on a
shape: moveVertical, moveHorizontal, slowMoveVertical and
slowMoveHorizontal. Note that all of these methods require a single
integer parameter value, so the commands will be more complicated to
implement. For instance:

> sun vertical 50

to move a Circle named sun vertically by 50 pixels.

To make this easier, the InputReader class has the following methods:
isAnInt and convertToInt. Both take a String parameter. The first returns
true if the String passed to it contains numerical characters like an integer,
and false otherwise. The second coverts the numerical characters of its
String parameter to an integer value and returns that value. These methods
will make it very easy to check that the third word of a command is, indeed, a
number and then get the numerical value.

Challenge component (10%)


The final component should be considered challenging.

Although all the shape classes have a changeSize method, the parameters they
take are not consistent. Some take one parameter (Circle and Square) while the
others take two (Person and Triangle).

• Add a resize command that can be called on any shape, but have the
program check that the correct number of values have been passed for the
shape to which it is being applied. For instance, if sun is a Circle and robot
is a Person, the following uses of resize would be correct:

> resize sun 100


> resize robot 10 50

but the following would not:

> resize sun 100 200


> resize robot 10

Have the program print an error message if the correct number of values has
not be provided. It is not acceptable to always specify two values and only use
the first in the case of Circle and Square!

Hint: The instanceof operator might be useful here.

Getting help
If you need help with the assignment, there are various places you can go. You will
not lose marks if you ask for help.
• You can ask questions on the anonymous question-asking page.

• You can ask for assistance from your class supervisor.

• You can ask the module’s lecturer.

• You can turn up at the homework club:


http://www.cs.kent.ac.uk/teaching/student/support/homework/

Be very careful about asking for help from other students on the course, or students
who have taken the course before, other than as described above. If in doubt about
what is appropriate, check the plagiarism guidelines above and/or ask the module’s
lecturer.

Finally
Before you submit your assessment, thoroughly test the whole class to make sure
that nothing you added later has broken anything added earlier. If you are unable to
complete the class – even if you cannot get it to compile – still submit what you have
done because it is likely that you will get at least some credit for it.

David Barnes, 7th December 2015

You might also like