COMP101 PRACTICAL EXERCISE 8 - CARD SHUFFLE(Week 11 starting
Monday 4 December 2006)
Hand-in date: one week after your scheduled tutorial session in week 11.
|
|
Aims and Objectives: To test student's ability and knowledge of the
usage and processing of arrays as discussed in the previous week's lectures.
1. REQUIREMENTS
A standard pack of cards can be represented as an array of 52 integers
with each number representing a standard card. Thus:
0 1 2 3 4 5 6 7 8 9 10 11 12 ..... 39 40 41 42 43 44 45 46 47 48 49 50 51
We can shuffle this pack of cards by randomly generating two indexes and
swapping those two cards. For example if we randomly generate the indexes 8
and 50 and swap these two "cards" we would get (assuming this is the first swap):
0 1 2 3 4 5 6 7 50 9 10 11 12 ..... 39 40 41 42 43 44 45 46 47 48 49 8 51
After a suitable number of swaps have been made
the pack can be said to be "shuffled".
Design and implement a Java programme which takes as input the number of
desired swaps, outputs an ordered Set of cards
which are then "shuffled" and output again. The number of swaps
must be at least 1 (otherwise we would not be doing any
"shuffling") and not more than 1000 (as any more than this would
be ridiculous).
Example output:
$ java CardShuffleApp
Input the desired number of swaps.
Integer between 1 and 1000 (inclusive)
0
ERROR: Input 0 out of range, try again!
10001
ERROR: Input 10001 out of range, try again!
300
Before shuffle:
2-Spd 3-Spd 4-Spd 5-Spd 6-Spd 7-Spd 8-Spd 9-Spd 10-Spd J-Spd Q-Spd K-Spd A-Spd
2-Hrt 3-Hrt 4-Hrt 5-Hrt 6-Hrt 7-Hrt 8-Hrt 9-Hrt 10-Hrt J-Hrt Q-Hrt K-Hrt A-Hrt
2-Dmd 3-Dmd 4-Dmd 5-Dmd 6-Dmd 7-Dmd 8-Dmd 9-Dmd 10-Dmd J-Dmd Q-Dmd K-Dmd A-Dmd
2-Clb 3-Clb 4-Clb 5-Clb 6-Clb 7-Clb 8-Clb 9-Clb 10-Clb J-Clb Q-Clb K-Clb A-Clb
After shuffle:
9-Clb 8-Dmd K-Dmd 3-Clb 6-Dmd 10-Spd 3-Spd A-Clb 3-Hrt 5-Spd 3-Dmd 10-Dmd 4-Dmd
A-Dmd 4-Clb J-Spd 7-Clb 6-Clb 9-Dmd 4-Spd 7-Hrt 9-Spd J-Clb K-Spd K-Clb Q-Hrt
5-Dmd 4-Hrt 9-Hrt 2-Spd 10-Hrt 8-Spd 2-Clb 7-Dmd A-Spd 7-Spd 5-Clb 2-Dmd 6-Spd
2-Hrt 10-Clb 5-Hrt A-Hrt J-Hrt 8-Hrt 8-Clb Q-Clb J-Dmd K-Hrt 6-Hrt Q-Dmd Q-Spd
Notes:
- Refer to earlier array examples (Metres to Yards,
Feet and Inches conversion,
Set I/O and
Set intersection) for guidance on how to process arrays.
- Remeber, to generate a random number between 0 and 51
inclusive
we can use the random method from the Math class and multiply by the
integer 52:
(int) (Math.random()*52);
- There are two obvious encodings:
- Order the array into suits and then values, i.e. integers 0 to 12 inclusive represent (say)
the spades, integers 13 to 25 (say) the hearts, and so on. In this case, using
integer division, the remainder after dividing by 13 (obtained using the Java
% operator) will give a value identifier, and the
resulting quotient a suit identifier.
- Order the array into values and then suits, i.e. integers 0 to 3
inclusive represent
(say) the deuces, integers 4 to 7 the value threes and so on. In this case we
can use the remainder after dividing (using integer division) by 4 to give a
value identifier, and the resulting quotient a suit identifier.
-
A switch statement is a useful mechanism whereby integers can be converted to suit identifiers or
"picture cards".
2. REPORT
Your solution should comprise the following:
- A report in the form of a single Microsoft Word file.
- The Java source files of your implementation.
The report should be of the (by now) standard format, comprising the
following sections:
- Requirements: (outline of the above)
- Analysis: Your analysis of the problem
comprising: (i) a Class Diagram
outlining the proposed class structure/hierarchy (at least one operational class
and an application class will be required), and (ii) a set of
summary tables describing the fields, constructors and methods for each class
you intend to create.
- Design: Detailed designs for the methods you intend to include,
described using Nassi-Shneiderman charts, and a high-level Activity Diagram
describing the "paths" through your code.
- Implementation: A computer print out of your implementation.
- Testing: A set of appropriate Black box and White box test cases
together with results and evidence of data validation.
All supporting documentation should be prepared as a single Microsoft
Word file.
3. SUBMISSION
Once completed you should "up-load" your Java source files
(extension .java) and your
word document to the CS department's electronic
"practical assignment submission" system.
4. MARK SCHEME
Marks will be awarded for:
- Analysis and design (30%)
- Implementation (30%)
- Testing (30%)
- Write up (10%)
With the total number of available marks distributed as indicated.
Remember:
- Guidance notes for the execution
of COMP101 practicals.
- General guidance
notes on COMP101 practicals and course work with respect to: the
presentation of work, the COMP101 marking scheme and University's late
submission policy.
- It is better to hand in an incomplete piece of work rather than nothing at
all as this will result in some marks being awarded, while the latter option is
guaranteed to result in a mark of 0!
- Although the exchange of ideas between students is
encouraged, student collaboration should not extend to the submitting of
identical, or near identical, pieces of work.
Created and maintained by
Frans Coenen.
Last updated 30 October 2006