You are on page 1of 10

COMP3200 Assignment 1 (Version: 2022.10.

10)
Copyright 2022 David J. Barnes
Posting outside kent.ac.uk is not permitted
Introduction
This assignment is designed to allow you to practise writing basic class definitions,
consisting of fields, constructor and methods. In some of the methods you will need
to use if-statements to make decisions.

Core background material can be found in Chapter 2 of the course text book,
Objects First with Java.

You will hand the assignment in on Moodle in the Assignment 1 Upload Area, within
the Assignments section.

Date set: 10th October 2022


Deadline: 23:55 24th October 2022.
Weighting: 15% of the module’s coursework mark.

Please read the assignment description in full to be sure that the work you submit
conforms to the full specification.

Even though there are two weeks available to complete the assignment, please read
it through quite soon so that you know what you need to understand to complete it,
and can think about how best to organise your time. Research has found that, “When
students received A- and B-level scores, they started earlier and finished earlier than
on assignments where they received lower scores [but] they did not appear to spend
any more time on their work.” (Stephen H. Edwards, et al., Comparing Effective and
Ineffective Behaviors of Student Programmers.)

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 Freelancer and we


treat such actions as evidence of attempted plagiarism, regardless of whether
or not work is paid for. In addition, posting anywhere other than kent.ac.uk
would be an infringement of the author’s copyright.

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.

Task Overview
The scenario is that you are part of a team working on a software system for a
company that manages events, for which rooms must be booked. Each room as a
finite capacity and must be large enough for the number of people who wish to
attend an event. You have been asked to write a class whose instances can store
basic details about an event. The company manages multiple events simultaneously
and each object of this class with hold details of just one event.

The data stored by an object is: the event’s name, the name of the room in which the
event will take place, the capacity of the room (the maximum number of people who
may attend an event in that room) and the current number of people registered to
attend it. The capacity and number registered are whole numbers (integers) and the
names of the event and the room are text. The main operations on instances cause
the registration level to be updated as people register or deregister. There are also
operations to allow the room to be changed for one of a different capacity, and to
print the current event details. More specific details of the class are given below
under Detailed Requirements.
You will find that the TicketMachine class discussed in chapter 2 of the course
textbook has a lot of functionality that implements similar ideas, so you can use that
for ideas to help you write your own class. Aim to study and practise using Chapter 2
as part of your preparation for undertaking this assignment.

Detailed Requirements
Class name
• Write a class which must be called Event.

Fields
• The class must have exactly four fields: two integer fields (for capacity and the
number of registrations) and two String fields (for the event name and room
name).
Important: There must be no additional fields defined in the class, even if you
think more are required. Consider using local variables if you think you need
extra fields.
• An important constraint on the number of registrations is that it must always be
less than or equal to the capacity of the room. This is a requirement because
of fire regulations.

Constructor
• The initial values of the event name, room name and capacity must be
received as parameters to the class constructor, while the number of
registrations must always be set to zero when an Event object is created.

Methods
• The class must define getter methods for all four fields.
• The class must define a method called register that takes no parameters. If
the current number of registrations is less than the room capacity then the
number of registrations must be increased by 1. Otherwise, the following error
message must be printed and the number of registrations must not be
changed:

The room is full.

Use System.out.println to print the error message.


• The class must define a method called deregister that takes no
parameters. If the current number of registrations is greater than zero then the
number of registrations is decreased by 1. Otherwise, the following error
message must be printed and the number of registrations must not be
changed.

There are no registrations.


Use System.out.println to print the error message.
• The class must define a method called changeRoom. It takes two parameters
in the following order: a String for a room name and an integer for the capacity
of that room. The idea is that the current room allocated to an event is to be
changed to either a larger or a smaller room. So, the object’s room name will
be set from the String parameter and its capacity from the integer parameter.

However, the following condition must be considered before automatically


changing the current values of the room and capacity fields: the capacity of the
new room must be greater than or equal to the current number of registrations.
If it is not then the following error message must be printed and neither the
room name nor the capacity must be changed:

Unable to change the room.

• The class must define a method called isSpace that allows the event
managers to determine whether there is enough capacity for a group to attend.
This method must take a single integer parameter representing the size of the
group, and return a boolean result. The method must work as follows:

o If the value of the parameter is less-than or equal-to 0 then the method


must return false. This case has priority over those following.
o If the value of the parameter is less-than or equal-to the space left in
the event (use the capacity and number of registrations in the Event
object to work this out) then the method must return true.
o Otherwise (i.e., if there is not space in the event for the whole group)
then the method must return false.

Note that the method must only return true if there is space for the whole
group to attend.

Note that this method must not change the state of the Event object. In other
words, both the number of registrations and the capacity of the object must be
exactly the same after it is called as it was before. Be careful to check that this
is the case.

• The class must define a method called printEventDetails that prints the
details (using System.out.println) of the event in the following format:

Computing BBQ in Darwin CR. 50 registered. Capacity 150.

Where “Computing BBQ” is the event name; “Darwin CR” is the room name;
50 people have registered and the room’s capacity is 150.

Of course, different Event objects will have different event names, room
names, number of people registered and different room capacities.

Important: Pay particular attention to the exact format of the details to be


printed. Take note of the spacing between the different parts, the punctuation,
and the order of the things to be printed. You will lose marks if your format
does not match.

• The class, the constructor and each method must be preceded by an


informative javadoc-style comment. Look at how this has been done in the
example code provided with the course projects, such as lab-classes and
naive-ticket-machine, and in the text book and follow that pattern.

• Your code must be neatly formatted and you must follow standard Java
guidelines for the methods of classes, variables and methods. The following
link has style guidelines to help:

https://www.bluej.org/objects-first/styleguide.html

You will also find that the Edit/Auto-layout option in the BlueJ editor will help
you to format your code neatly.

This video will help you to review good style:


https://www.youtube.com/watch?v=ekZaGT9sLh4

Tips for getting started


This description of the requirements might seem overwhelming at first, so below
there is some advice to help you build your final version in small steps. For instance,
it will be a good idea to develop the code one field at a time and, initially, it will be
easier to ignore the ‘greater than or equal to’ constraints and not worry about
checking the values of the parameters. Those details can be added later once the
basic fields, constructor and methods are in place.

Remember to try to follow the fundamental principles we have used when coding in
lectures: document as you go; write small amounts of code; use the compiler to find
and fix errors quickly. Don’t write a lot of code before trying to find the errors, and
don’t write more code if you haven’t fixed the errors in the code you have written so
far.

However, you will only be able to follow those principles if you leave yourself
plenty of time to complete the assignment rather than leaving it to the last
minute!
Start a new project in BlueJ using the Project/New project option. Choose an
appropriate name for the project, such as assignment1.

Select the New Class button to start creating a new class. Give your class the name
Event. BlueJ will create an outline class definition for you in the main area of the
project. You will probably find that the source code in the class looks something like
the following:

/**
* Write a description of class Event here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Event
{
// instance variables - ...
private int x;

/**
* Constructor for objects of class Event
*/
public Event()
{
// initialise instance variables
x = 0;
}

/**
* An example of a method - ...
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}

Add a Field
Instances of the Event class will need a String field that can store the name of the
event. Add a field with an appropriate name of your choosing (but avoid
meaningless, single-letter names like c, e, x, y, etc.). Don’t forget to include an
appropriate visibility modifier for the field. If you are not sure what that is, look at the
fields in some of the example classes you have already seen and model your field on
them.
Compile the code and fix any errors you see, such as missing semicolons or mis-
spelled type names. A good rule to remember is that Java keywords (like class, int,
boolean, public, private, etc.) consist entirely of lower-case letters. However,
commonly-used types like String and System are classes and have an initial upper-
case letter.

Even though you haven’t added any new methods yet, you can still create objects
from it. Do this and use the inspector to show the state of an object. You should see
the field you have defined with a value of null. At this point, you won’t be able to
get or change that (default) value because you have no methods to operate on it.
Those will come later.

Constructor
Add an assignment statement to the constructor to initialise the event name field to
have a value of “Computing BBQ”. Compile your class, fix any errors, then create an
object and check that the field has a value of “Computing BBQ” as soon as it is
created.

In order to set the event name in different instances to different values, add a String
parameter to the constructor. Use the value of the parameter to initialise the event
name field. Use the TicketMachine class to help you with this if you need to look up
how to do this – see the way that different ticket prices can be set. Even though price
is an integer field and the name is a String, the principles of assignment from
parameters to fields are the same.

Once again, compile the class, fix any errors, create some objects using the new
constructor and different parameter values to check that things are working as they
must. The thing to look out for is that the name field’s value you see when you
inspect an object matches the value that you pass in as a parameter to the
constructor. If it is still null then you need to fix that in the constructor.

The sort of cycle we have been following up to this point – add a little code; compile;
fix errors; compile; create objects and test – is a fundamental practice that you
should follow whenever you are writing software, no matter whether you are a novice
or an expert. One of the advantages of doing things this way is that you catch errors
very quickly, and will know roughly where to look for the problem because it will likely
have something to do with the code you most recently added or changed. We will
take it for granted that you will follow this cycle and won’t keep repeating that you
should do so.
Getter Method
Add a getter method to your class that will return the name of the event. Getter
methods usually have names beginning with ‘get’ and followed by the name of the
field they are associated with; for instance, getTotal for a field called total,
getAddress for a field called address, getDate, etc. Give your method public
visibility and a return type that is the same as the type of the field. The method must
take no parameters. In the method body, there must be a return statement to return
the field’s value. Once again, take a look at the accessor methods in the
TicketMachine class if you need some code to refer to and make the necessary
adjustments for this different context. Compile and test your code.

Review
At this point, you should have a class that defines a String field to store the event
name. It has a constructor to initialise the name and a getter for it.

At this stage, you could remove some of the original template code associated with
the unwanted variable x and the sampleMethod. However, you might also like to
rename the variable x so that it can be used as the variable that stores the number
of registrations.

Further fields and accessors


Continue by adding a field for the number of registrations. Initialise it to zero in the
constructor and add a getter. Compile, fix, compile and test. Then add fields for the
number of registrations and follow the same process. Always use the Inspector for
an object to make sure it has been initialised correctly.

The remaining features


One by one, add further fields and methods to the class, always remembering to do
things in small increments and to compile and test after each change. In your first
versions of the various methods – register, deregister, changeroom, … –
don’t worry about implementing the rules to stop the number of registrations going
out of bounds. Just concentrate on the basic arithmetic operations. Once you have
those correct you can add the necessary checks – see below.

Using if statements to implement checks


It is now time to add some checking functionality so that the number of registrations
and capacity fields are protected from being set or modified in inappropriate ways.
For this you will need to use if-statements. The basic syntax of an if-statement is:

if(test-some-condition) {
do this part if the condition was true
}
else {
do this part if the condition was false
}

For instance, in a sendText method from a mobile phone class it might be


necessary to check whether the phone has sufficient credit, as follows:

// Make sure there is enough credit for a text.


if(credit < textCost) {
System.out.println("You do not have enough credit.");
}
else {
// Reduce the credit by the cost of a text message.
credit = credit – textCost;
}

Making the Class Robust


Add tests to the methods that change the fields to make sure that the detailed
requirements are fulfilled.

When you add new code, be sure to test all methods at least twice so that an error
message is output on one test and not output on another, and make sure that fields
are not changed inappropriately in the error cases.

Finally
When writing software, it is essential to check that your code meets the requirements
if you want to obtain good marks. Unfortunately, this is something that is almost
always forgotten and marks are lost unnecessarily. Confidence in the correctness of
your code can only be gained through testing it thoroughly. You should, therefore,
extensively check your code as you are developing it, as well as when you have
finished it. Just because your code compiles does not mean that it operates
correctly. Test your code, therefore, by creating objects and calling their methods to
cover all of the different cases described above. Although this might seem tedious at
times, you will often reveal errors that you had not imagined existed.

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.

Assessment submission
Submit your assessment through the COMP3200 Moodle page in the Assignment 1
Upload Area within the Assignments section. You need to create a zip or jar file of
the folder containing all the project files and upload that.
David Barnes, 19th October 2022

You might also like