COMP122 java技术

COMP122 Assessment 1
Class Hierarchy; Polymorphism
Due 2019-03-08 at 5pm
COMP122 Assessment 1 Due 2019-03-08 at 5pm
Part I
The aim is to design, implement, and test a Java program to simulate a game between two players,
each rolling three dice for a number of rounds. In each round both players roll the dice. This gains
them points and the player with a higher score wins the round. Any outcome of the roll of three 6-sided
dice can be classified into exactly one of four cases, and will gain points as follows.
Case Description Scoring Function Example Points
all the same All three values are the same sumOfDice + 50 3, 3, 3 9 + 50 = 59
1, 1, 1 3 + 50 = 53
a Run sequence of values that are all dierent sumOfDice + 30 4, 5, 6 15 + 30 = 45
3, 1, 2 6 + 30 = 36
a Pair exactly two values are the same sumOfDice + 20 1, 1, 4 6 + 20 = 26
5, 1, 5 11 + 20 =31
all dierent All values are dierent and it isn’t a run sumOfDice 2, 3, 6 11
5, 1, 4 10
Part I of the assignment builds on the ThreeDice class which is given to you and must not be altered.
It implements a class that records the rolls of three dice and includes methods that can be used to
classify each of four possible outcomes above. It’s class diagram is below.
ThreeDice
die1: int
die2: int
die3: int
+getDie1(): int
+getDie2(): int
+getDie3(): int
+threeSame(): boolean
+runOfThree(): boolean
+pair(): boolean
+allDifferent(): boolean
+printResult(): void
The ThreeDice class constructor takes three integer parameters as input, each parameter representing
the number shown on one of the dice. Note that the way the class is written, the three numbers given
to the constructor have to be the integers 1-6, but can be integers.
As you can see, each of the methods threeSame(), runOfThree(), pair(), and allDifferent()
2
COMP122 Assessment 1 Due 2019-03-08 at 5pm
returns a boolean value (i.e. true or false) that represents whether or not the three dice values can
be classified into that category. Have a look at the code for these methods and convince yourself that
they will return the correct results. These methods are relying on the fact that the three dice rolls are
stored in (non-decreasing) order in the class variables.
The printResult() method is provided mainly for testing this class as it prints the values of the three
dice, and calls the methods to determine the type of outcome.
Requirements for Part I
a) Extend the class ThreeDice by designing a subclass that includes a method that returns the
points for a particular dice roll according to the points table above. Put this new class into a
separate class file called ThreeDiceScorer.java.
Note that you need to specify a constructor for your new subclass (in this case, it will likely just
call the constructor of the parent ThreeDice class), and this subclass needs at least one more
method to calculate the points score of the three dice.
b) Write a program called Game.java that will simulate the game described above. It should ask
the user for a (non-negative) number of rounds to play. Your application must store the dice rolls
for the pair of players as an array of objects. This application should output for each round:
the dice values, and number of points for each player for those dice values;
the winner of each round (the player with the highest points for that round, or no-one if
they are the same, as ties are possible).
At the end the following information should be calculated and displayed:
the total number of rounds won for each player;
the total points for each player;
the average points for each player (i.e. total points divided by the number of rounds played);
the player with the highest points total.
Rather than asking for user input for each dice roll, use randomly generated numbers between 1
and 6 (inclusive). You can use Math.random() or java.util.Random for this (recall Practical
2, Exercise 6).
Note: You may add other classes (in addition to ThreeDiceScorer.java) that are used by
your application. Your report (see submission instructions below) should contain descriptions
and class diagrams of all your classes.
An example run for Game.java is as follows.
3
COMP122 Assessment 1 Due 2019-03-08 at 5pm
1 Input the number of rounds to play (min 0): 5
2 Round 1 Player 1: 1 2 2 Points: 25 Player 2: 1 1 5 Points: 27
Round winner is player 2.
3 Round 2 Player 1: 2 3 3 Points: 28 Player 2: 1 3 3 Points: 27
Round winner is player 1.
4 Round 3 Player 1: 3 5 5 Points: 33 Player 2: 3 4 5 Points: 42
Round winner is player 2.
5 Round 4 Player 1: 1 3 6 Points: 10 Player 2: 1 4 5 Points: 10
Round is tied!
6 Round 5 Player 1: 1 1 3 Points: 25 Player 2: 1 5 6 Points: 12
Round winner is player 1.
7
8 Total wins:
9 Player 1: 2 Player 2: 2
10 Total points:
11 Player 1: 121 Player 2: 118
12 Average points per round:
13 Player 1: 24.2 Player 2: 23.6
14
15 Overall points winner is player 2.
c) Write a separate program, called Average.java, that will compute the average number of
points over all possible rolls of three six-sided (fair) dice. (How many such combinations of the
three dice are possible? See your COMP109 notes to remind yourself.) Average.java should (of
course) make use of your ThreeDiceScorer.java class that you designed above.
d) Suppose that instead of three fair six-sided dice, two of the dice are normal (fair) six-sided dice,
and that the third six-sided die has the numbers 2, 3, 4, 5, 6, 6 (where each of the possible six
sides of this die occurs with equal probability). What is the average number of points over all
possible rolls of these dice?
You only need to state the average here, and how it compares to the average you have obtained
in c).
4
COMP122 Assessment 1 Due 2019-03-08 at 5pm
Part II
This part of the assessment is about designing a hierarchy of Java classes. The idea here is that you are
writing a program to display information to a potential client for a company that supplies combined
broadband, TV, and mobile phone packages.
This company (currently) provides three dierent broadband, TV, and mobile phone packages which
are summarised below. Each account type has a basic flat cost (per month), and additional charges
that depend upon the number of minutes used on the phone and broadband usage charges.
Bronze Silver Gold
Package Costs (monthly) £35.00 £45.00 £60.00
Daytime phone costs (per minute) 12p 12p 0p
Evening/Weekend phone costs (per minute) 5p 0p 0p
Number of TV Channels 60 130 230
Broadband (included per month) 200GB 300GB 2000GB
Broadband (per GB above included amount) 20p 10p 10p
Additionally, the Silver and Gold accounts provide a free Spotify account, and Gold accounts include
music on demand.
For this part of the assessment you are going to use Java’s inheritance mechanism to implement a set
of classes to store information about the company’s packages, and display costs to a potential client.
Requirements for Part II
a) A user should be able to input the number of daytime phone minutes that she/he uses, the
number of evening/weekend minutes that she/he uses, and the amount of broadband usage,
expressed in Gb (megabytes), for a given month. These values should all be integer values, greater
than or equal to zero (i.e. negative values should be disallowed and the user asked to re-input
these values).
b) For each account (Bronze, Silver, and Gold), the account information should be printed, the total
cost of each type of call should be calculated and printed, the total cost for (extra) broadband
usage, and the total cost of that account (made up from the call costs, broadband costs, plus the
package costs) should be calculated and printed.
5
COMP122 Assessment 1 Due 2019-03-08 at 5pm
c) Additionally, the program should output which account has the total cost that is the cheapest. If
two (or more) accounts have the same cost, which would you recommend to the customer?
See an example of output below. Call your application program AccountUser.java.
d) Suppose that the company wants to introduce a ‘Platinum Account’, which has a monthly cost of
£75, increases the number of TV channels to 275, and has unlimited broadband (i.e. no additional
costs on a per GB basis). Otherwise, all other features about the Platimum Account are the same
as the Gold Account.
How could you implement this additional account, in terms of your class hierarchy and the way
you have implemented your application program?
This question requires no code to be written, just a short description in your report for the assessment:
Describe clearly what you would do to add this new account type. What would you do in
terms of Java programming, e.g. modifying/overriding methods, adding new constants, etc?
Example program output
1 Please enter the number of daytime minutes used per month: 100
2 Please enter the number of nighttime minutes used per month: 100
3 Please enter the number of Gigabytes used per month: 325
4 Account Summary for Bronze Account
5 Package Cost: 35.00
6 Cost of daytime calls: 0.12/min
7 Cost of evening and weekend calls: 0.05/min
8 Number of Channels: 60
9 Broadband Included: 200GB
10 Broadband Cost (above included limit): 0.2/GB
11 Total daytime calls cost: 12.00
12 Total evening calls cost: 5.00
13 Total (extra) broadband cost: 25.00
14 Total cost: 77.00
15
16 Account Summary for Silver Account
17 Package Cost: 45.00
18 Cost of daytime calls: 0.12/min
19 Cost of evening and weekend calls: 0.00/min
20 Number of Channels: 130
21 Broadband Included: 300GB
22 Broadband Cost (above included limit): 0.1/GB
23 Total daytime calls cost: 12.00
24 Total evening calls cost: 0.00
25 Total (extra) broadband cost: 2.50
6
COMP122 Assessment 1 Due 2019-03-08 at 5pm
26 Total cost: 59.50
27 Spotify Account provided
28
29 Account Summary for Gold Account
30 Package Cost: 60.00
31 Cost of daytime calls: 0.00/min
32 Cost of evening and weekend calls: 0.00/min
33 Number of Channels: 230
34 Broadband Included: 2000GB
35 Broadband Cost (above included limit): 0.1/GB
36 Total daytime calls cost: 0.00
37 Total evening calls cost: 0.00
38 Total (extra) broadband cost: 0.00
39 Total cost: 60.00
40 Spotify Account provided
41 Music on Demand provided
42
43 Silver Account is cheapest cost.
Hints/Suggestions
You will need (at least) four Java classes: BronzeAccount, SilverAccount, GoldAccount,
and an application class.
I suggest making an abstract class called (for example) StandardAccount that will be
the base class of a hierarchy of classes. The classes BronzeAccount, SilverAccount, and
GoldAccount can each extend this StandardAccount class.
Make the class attributes in StandardAccount protected (not private) because then subclasses
that extend StandardAccount can easily modify these class attributes as appropriate.
With careful thought and planning, most of the Java code for the program operations (such
as computing the costs of mobile phone charges, broadband charges, etc) can be placed in
the StandardAccount class. These methods can be overridden (if needed) in the classes that
extend it. So the code for the BronzeAccount class can be quite short (similarly for the other
classes that extend StandardAccount).
You could also include, for example, an abstract method in StandardAccount called
accountType() which returns a String for the type of account (e.g. ‘Bronze’). Since
that method is abstract, it must be overridden (implemented) in the classes that extend
StandardAccount.
7
COMP122 Assessment 1 Due 2019-03-08 at 5pm
Try to put as much data and related calculations into a StandardAccount class, that each other
class will extend, in order to avoid code duplication.
I also suggest it can be useful to create a Java interface that will contain various constants
that describe the charges per minute, (extra) broadband charges per GB, number of channels
included in the package, etc. Each class can implement this interface and then have access to
the constants which are all in one place.
Submission Instructions
Deadline: Friday, 8 March 2019, 5:00pm (Friday of Week 6)
Submission server: https://sam.csc.liv.ac.uk/COM...
Your submission should be a single compressed .zip file called comp122_assessment_1_SID.zip
where SID should be replaced by your student ID. This file should include:

  1. Your implementation
    These must include the .java source files, not .class files!
    For Part I you should include the (unchanged!) file ThreeDice.java so that your code
    compiles directly.
    Put all files for Part I into a subdirectory called part_1, and everything for Part II in a
    directory part_2 accordingly.
  2. A report. This must be a .pdf file and should contain sections as follows.
    Requirements: A Summary of the above requirements statement in your own words.
    Analysis and Design: A short (one paragraph) description of your analysis of the problem including
    a Class Diagram outlining the class structure for your proposed solution, and pseudocode for
    methods. Again, do this for each part of the assessment.
    Testing: A set of proposed test cases presented in tabular format including expected output, and
    evidence of the results of testing (the simplest way of doing this is to cut and paste the result of
    running your test cases into your report).
    8
    COMP122 Assessment 1 Due 2019-03-08 at 5pm
    Marking Scheme
    Each part counts 50% towards the final mark of this coursework. Marks will be awarded for:
    Analysis and Design 10% (This includes things like UML diagrams for your classes/application,
    justification for how/why you structure your classes, appropriate pseudocode for methods, etc.)
    Implementation 25% (This includes correctness of your programs, appropriate level of comments,
    correct indentation so your code is readable, having useful identifiers, etc.)
    Testing 10% (Have you done suitable testing in terms of checking dierent paths through your
    code, checking what you do with ‘unexpected’ inputs, a suicient number of tests, etc.
    Extra questions 5% such as in Part I c) or d) or Part II d).
    If your Java source files do not successfully compile and run on departmental computer systems you
    will suer a penalty of 5 marks. This applies to both parts of the assessment separately.
    If you use any form of compression other than zip or your report is not a PDF format, you risk your
    files not being read by the assessors, resulting in a mark of 0! If your files can be read, you will still lose
  3. marks from your final grade.
    Your submission is an individual piece of work. No collaboration with other students is allowed!
    Expect that plagiarism detection soware will be run on your submission to compare your work to
    that of other students. Late submissions and plagiarism/collusion are subject to the University Code of
    Practice on Assessment 1

你可能感兴趣的