You are on page 1of 7

CO320 Assignment 1 (Version: 2016.10.

10)
Copyright 2016 David J. Barnes
Posting outside kent.ac.uk is not permitted
Introduction
This assignment is designed to allow you to practice 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. Most of the assignment has been broken
down into detailed stages as a guide to help you to complete it, but you don’t have to
implement it in the stages suggested if you don’t wish to. You will hand the
assignment in on Moodle and details of that process will be made available shortly.

Date set: 10th October 2016


Deadline: 23:55 24th October 2016
Weighting: 15% of the module mark.

Please make an effort to 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.

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 payed 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.

The Task
Imagine that you are part of a team working on a software system for an online
auction site, a little like eBay. You have been asked to write a class whose instances
can store details of an item for sale. The details to be stored for each item are very
simple. The class will need a field to store a text description of the item, and another
to store the value of the current highest bid in pence. The Java types you need for
this are String and int. A challenge part of the assignment adds a further field.

When writing software it is essential to ensure that your code meets the
requirements of it. This assurance can only be gained through testing the code
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 with a wide range of parameter values. Although
this might seem tedious at times, you will often reveal errors that you had not
imagined existed.

Creating a new class in BlueJ


Start a new project in BlueJ using the Project/New project option. Choose a name
and location for the project.

Select the New Class button to start creating a new class. Give your class the name
Item. 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 Item here.


*
* @author (your name)
* @version (a version number or a date)
*/
public class Item
{
// instance variables - replace the example below with your own
private int x;

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

/**
* An example of a method - replace this comment with your own
*
* @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;
}
}

BlueJ has given you a sample field, constructor and method, but these are mostly
not relevant to this assignment and it is probably easiest to delete everything inside
the class body and leave yourself with just the following:

/**
* Write a description of class Item here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Item
{

Even though this contains no fields, constructors or methods, it is still a valid class
definition, so press the Compile button to make sure that you haven’t deleted too
much or too little from the code. If you get any compiler errors then read the error
message and see if you can work out what is wrong in the code. Your version should
look exactly like the code above.

Add a Field
Objects of the Item class will need an integer field that can store the amount of the
highest bid as an integer number of pence. Add a field with an appropriate name of
your choosing (but avoid meaningless, single-letter names like c, 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 all Java keywords (like class,
int, boolean, public, private, etc.) consist entirely of lower-case letters.

Even though the class has no methods, 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 zero. At this point, you won’t be able to change that
(default) value because you have no methods to operate on (mutate) it. Those will
come later. Anyway, a starting value of zero is exactly what we want for this field.

A second field
In a similar way, add a second field to store the text description of an item. Repeat
the process above to make sure your code compiles. When you inspect a newly-
created Item object you should find that the value of this second field is null. A role
of the constructor, which we cover next, will be to ensure that this field is initialised
with the item’s description.

Constructor
Add a constructor to the class. The constructor must take a single parameter that
supplies the description of an item. This constructor must initialise the description
field using this parameter. Compile your class, fix any errors, then create an object
and pass a description to the constructor. Use the inspector to check that the
description field has been set from the parameter.

The 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.
Accessor Method
Add an accessor method for each field to your class that will return, in one case, the
value of the current highest bid and, in the other case, the item’s description. You will
find it easiest to add these one at a time. Get the first one compiled and working
properly before adding the second.

Accessor methods often 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,
getAmount for a field called amount, getDate, etc. Give your methods public
visibility and a return type that is the same as the type of the field.

The methods must take no parameters. In each method body there must be a return
statement to return the associated field’s value. Once again, take a look at the
accessor methods in the TicketMachine class if you need some code to refer to.
Compile and test your code.

Mutator Method
Write a mutator method that takes a single parameter that is used to set the amount
of the current bid. Choose a meaningful name. Give your method public visibility
and a void return type. The body of the method should simply assign the value of
the parameter to the field variable. At this stage, don’t worry about checking whether
the parameter value is sensible; just make the assignment to the field.

A mutator method is not needed for the description field, as the item’s description will
not be changed once it is set.

Review
At this point, you should have a class that defines two fields that store a text
description and an amount of pence. The class has one constructor that sets the
description, an accessor for each field, and a method that can alter the value of the
bid amount.

Using if Statements
It is now time to add some checking functionality so that the bid amount is protected
from being modified in an inappropriate way. 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:

// 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 a test to the method that changes the current bid amount to ensure that the
value of a new bid is strictly greater than the value currently stored, because bids
must always increase. A bid of the same amount is not allowed. If a parameter value
is used that is not big enough then print an appropriate error message and do not
change the field. The error message must identify the method that was called and
the incorrect parameter value that was used. For instance:

bidFor was called with a value that is too small: 50

When you are testing, be sure to test all methods both with parameter values that
are correct and incorrect, and make sure that the field is not changed if a bad
parameter value is used.

Challenge task
This part of the assignment is worth 20% of the marks for this assignment. It is
described in less detail than the earlier parts. It has been divided into two stages.
Completing the first stage correctly is worth 10% of the assignment marks.
Completing the second stage correctly is worth the full 20%.

1. Add an extra field to the class called increment. The value of this specifies
how much bigger a new bid must be than the current one to be accepted. In
other words, it is the minimum bid increment. The error message when a bid
is rejected must include details of what the minimum bid must be to be
acceptable.

By default, the increment’s value is 1 but it may be set to any non-zero,


positive integer value via a mutator.

Completing this part will necessarily require a change to the way that the
earlier bid-for method works. A changed version is acceptable for the non-
challenge part and a separate version of the bid-for method is not required.
Note that the increment value is stored as part of the object and not passed
as a parameter to the bid-for method.
2. Rather than being a fixed amount, the size of the minimum increment must
increase as the highest bid amount increases. The increment must always be
1 plus approximately 10% of the current highest bid, rounding down when
necessary. For instance:

If the current highest bid is 5 pence then the minimum increment is 1 + 0.


If the current highest bid is 10 pence then the minimum increment is 1 + 1.
If the current highest bid is 27 pence then the minimum increment is 1 + 2.

Completing this part will necessarily remove the need for a mutator for the
increment field, but that is acceptable as a solution to the first challenge part.

Note that in Java, integer division naturally rounds down.

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, 10th October 2016

You might also like