首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
COMP122编程语言辅导、讲解java程序语言、java程序调试 解析Haskell程序|讲解R语言编程
项目预算:
开发周期:
发布时间:
要求地区:
COMP122 Assessment 3 Due 2021-05-07 at 5pm
Exceptional Press
Book
-title: String
-author: String
-content: String
-edition: int
+getTitle(): String
+getAuthor(): String
+String getContent()
+getEdition(): int
+Book(String t, String a, String c, int e)
+getPages(): int
+toString(): String
Press
-booksPerEdition: int
-edition: Map
-shelf: Map
>
+Press(String p, int n)
+print(String id, b): List
+getCatalogue() List
+request(String id, n) List
VendingMachine
-supplier: Press
-locationFactor: double
-cassette: double
+VendingMachine(Press p, double f)
+getCassette(): double
+insertCoint(double c): void
+returnCoins(): double
+buyBook(String id): Book
During the pandemic you realised that lots of people are looking for books to read but are reluctant
to throw their money at large online corporations while shops are closed. So you came up with the
idea to re-print old classics whose copyright has expired and sell them o vending machines that you
put up where once public libraries stood. In this assignment you’ll implement three aspects of this
endeavour: You will model books, the press, and vending machines.
Part 1: Books
We’ll need a data structure to represent an individual Book. Write a class called Book as follows.
There should be string attributes called title, author, and content as well as an integer attribute
called edition. Since we want readers to be able to see but not change the respective values of a
book, all these fields must be declared private and your class should have accessor-methods for
each of them. That is, your class should have public methods called getTitle, getAuthor, and
getContent that take no arguments and return a String, namely the unchanged value of their
respective attribute. Similarly, there must be a method public int getEdition() that returns the
edition of the book.
Your class must feature a constructor method that accepts and stores initial values for these four
attributes in the order they are mentioned above.
1 public Book(String t, String a, String c, int e)
In order to implement our innovative pricing model each book should be able to report on its content.
Let’s assume the that the layout of our books is such that it fits 800 characters on every page. Add
a (public) method called getPages that takes no arguments and returns the number of pages the
1
COMP122 Assessment 3 Due 2021-05-07 at 5pm
book has as an int. That is, it returns the integer corresponding to the number of characters in the
books content string divided by 800 and rounded up if necessary.
Finally, in order to nicely present the book on a shelf, overload the toString() method to return
title, author, edition, as well as the number of pages in this order. Each should appear in one line and
prefixed with the capitalised name of the attribute followed by a colon and one space. Each line should
end in a newline character '\n' directly aer the last printable character. See below for an example
output String.
Title: Treasure Island
Author: Robert Louis Stevenson
Edition: 1
Pages: 209
Part 2: The Press
Write a class called Press that can be used to create books.
The books we’ll create are going to be re-prints of famous volumes whose copyright has expired. We can
get them as (UTF8 encoded) text files from Project Gutenberg and a selection can also be downloaded
from Canvas for testing. When we instantiate a Press object we’ll be given a path to a directory that
contains several such text files. We’ll assume for simplicity that each kind of book in print is identified
by the name of the text file it is read from, e.g. "120-0.txt". Such a “bookID” string should include
the suix “.txt” but no preceding directory name.
As it is cumbersome and costly to reconfigure our press when switching to a new book, we’ll print
books in batches and store them until they are distributed to our vending machines. Our press should
therefore have several shelves, one for each kind of book. If someone wants to take out n copies of
a book, we will give them the first n from the shelf. If there are fewer than that many le we print
new ones (with a new “edition” number), add them to the end of the shelf and give out the requested
number of books. Since storage space is expensive there is an fixed number of books each edition will
have. This number is stored in a private field of each Press object and fixed upon instantiation. We
can assume that no user wants to take out more than that many books.
Setting up the press
Your Print class needs to have private attributes as follows.
• an integer called booksPerEdition. Each new edition will have that many books.
• the most recent edition of each book. This should be stored as a map from “BookID” strings
into an integers. That is, there should be a single private attribute called “edition” of type
Map
.
2
COMP122 Assessment 3 Due 2021-05-07 at 5pm
• a shelf to store books. This should be implemented as a map from “BookID” strings into a
queue of Books. That is, there should be a single private attribute called “shelf” that has type
Map
>.
You may add other private attributes to make things work, for example to store the content of each
text file so that you do not need to read it again and again. We will not look at these attributes when
testing and grading your code.
Your class should have a constructor method that takes 1. a (String) path to the directory containing
books as text files, 2. the (int) number of books printed per edition. This should set up the data
structures mentioned above and trigger a first printing run of all books.
1 public Press(String pathToBooKDir, booksPerEdition)
For example, suppose that we instantiate a press as new Press("mybooks", 3) and the directory
“mybooks” contains three files called “11-0.txt”, “120-0.txt”, and “12-0.txt”. Then the newly instantiated
Press object should contain
• as shelf attribute, that maps “11-0.txt” to a Queue containing three distinct first edition Books
whose content (and title, author) is read from that file, and similarly for the other two file names.
• as edition attribute, that maps from all three strings to the integer 1 (because we already
printed one edition of each book to fill the shelf).
Printing books
In order to instantiate a single book we need to read the respective text file and extract title, author and
content. Luckily, all these text files have a similar header in the first few lines that contains that info.
They all start in "The Project Gutenberg ", list a few properties such as title, author, language
and so on. Then there is a line that starts with "*** START OF" that ends the header and marks the
beginning of the book’s content. For example, here are the first 23 lines of “120-0.txt”.
1 The Project Gutenberg EBook of Treasure Island, by Robert Louis Stevenson
2
3 This eBook is for the use of anyone anywhere in the United States and most
4 other parts of the world at no cost and with almost no restrictions
5 whatsoever. You may copy it, give it away or re-use it under the terms of
6 the Project Gutenberg License included with this eBook or online at
7 www.gutenberg.org. If you are not located in the United States, you'll have
8 to check the laws of the country where you are located before using this ebook.
9
10 Title: Treasure Island
11
12 Author: Robert Louis Stevenson
13
14 Illustrator: Louis Rhead
3
COMP122 Assessment 3 Due 2021-05-07 at 5pm
15
16 Release Date: March 13, 1994 [EBook #120]
17 [Most recently updated: October 28, 2020]
18
19 Language: English
20
21 Character set encoding: UTF-8
22
23 *** START OF THIS PROJECT GUTENBERG EBOOK TREASURE ISLAND ***
Everything from (and including) line 24 to the end of the file we’ll consider the content of the book.
This is likely going to start with a few blank lines. You will need to write some code that opens a text
file and extracts title, author and content strings and then uses your four-argument constructor for the
Book class to instantiate one or more books of this kind.
For this purpose, your Press class should have a public method called print that can be used to
print a whole batch of books at once.
1 public List
print(String bookID, n)
This method should instantiate n books from the text file whose name is given as the string parameter
bookID and return a list that contains these n books. The edition of all new books should be the same
as the new value of edition attribute of our press (for the given bookID), which of course should be
one higher than it was before this printing process.
If the requested book is not in print, i.e. the string given as bookID is not the name of one of our files,
then this method should throw an java.lang.IllegalArgumentException. If the content of a
text file does is not in the form described above, for example if it does not contain a line starting in
"Title: " then this method should throw an java.util.IllegalFormatException.
Distributing Books
Your Press class needs to have a public method called getCatalogue() that takes no arguments
and returns a list of books it can produce. Again, let’s assume the book is identified by the name of the
text file it is read from, e.g. "120-0.txt". The getCatalogue method should return a list of Strings
and be declared exactly as below.
1 public List
getCatalogue()
Your Press class needs to have a method called request to allow users to take out a number of books
at once. It should have a signature as follows.
1 public List
request(String bookID, int amount)
4
COMP122 Assessment 3 Due 2021-05-07 at 5pm
A user would give a bookID string that identifies the book and the number of copies to take. The
resulting list of books must naturally have the same length as requested and contain that many distinct
copies of the requested book.
This method should return the books from the shelf or trigger a re-print if there are not enough copies
in store: If someone wants to take out n copies of a book, we will give them the first n from the shelf. If
there are fewer than that many le we print new ones (with a new “edition” number), add them to the
end of the shelf and give out the requested number of books. We can assume that no user wants to
take out more books than would fit on the shelf.
If the requested book is not in print, i.e. the string given as bookID is not the name of one of our files,
then this method should throw an IllegalArgumentException.
Part 3. A vending machine for books
Write a class called VendingMachine that can sell books to customers. Each such vending machine
will be supplied by a press and oers all books that press can print. The price each book sells for will
depend on the size of the book but also on some “location factor” that allows us to sell books at higher
prices in posher areas. For instance, a book with 500 pages sells for 500*0.01 = 5.0 GBP from a
vending machine with location factor 0.01.
Your class must have a constructor method that takes exactly three parameters, namely a Press object,
the location factor (a double), and an int that determines the size of its shelves, meaning the number
of books of each kind to store.
1 public VendingMachine(Press supplier, double locationFactor, int size)
These items should be stored in the vending machine as private attributes (of the same name and type)
to be accessed later. So there should be private attributes supplier (a Press), locationFactor (a
double), and size (an int).
The class should also have a private double attribute called “cassette” and a corresponding getter
public double getCassette(). Moreover, there must be public methods as follows.
1. a method called “insertCoin”
1 public void insertCoin(double coin)
that adds the value of is parameter to the cassette. This method should throw an
IllegalArgumentException if the given coin is not of the right denomination. Acceptable
values are 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, and 2.
2. a method called returnCoins
5
COMP122 Assessment 3 Due 2021-05-07 at 5pm
1 public double returnCoins()
that empties the cassette (sets it to zero) and returns its original value.
3. a method called buyBook
1 public Book buyBook(String bookID)
that sells a book with the given bookID string. It should check if the price of the book is at most
the value of cassette. The price of the book is the number of pages multiplied by the location
factor of the vending machine.
If the cassette contains enough money then the method removes the book from the shelf, reduces
cassette by the price of the book and returns the book. If the book sold like this was the last of
its kind (meaning aer the sale the machine has none le) then the vending machine contacts its
supplier press and requests more copies of this book. The number of copies it requests should
be determined by the value of the size attribute, so we request as many copies of this book as
we can fit.
The buyBook method should also be able to throw (unchecked) exceptions for the following two
scenarios:
1. If the parameter bookID is not a valid (there is no book by this id on oer) then this should
throw an IllegalArgumentException.
2. If the price of the book exceeds the value of the cassette then the method should throw a
CassetteException. This type of exception is of course special to our application and
you should define it yourself as a new type of RuntimeException.
6
COMP122 Assessment 3 Due 2021-05-07 at 5pm
Hints
1. You can use anything you want from the Java standard library.
2. Do not only focus on the diicult bits! You can gain points for having the prescribed methods.
3. Use the automarker and take its feedback into account before you submit your final version. You
can do that by running check50 .
4. Notice that nowhere in this assignment do we ask you to write a main method. To test your code,
you can write some (unassessed) main method somewhere that instantiates a Book/Press
/VendingMachine, calls its methods and prints the results. Our checks will try to instantiate
your classes into objects. This means that if your constructor methods are missing or calling
them results in errors, you will not get points for functionality.
5. To list files in a given directory you can use java.io.File.listFiles() for example like this.
1 File directoryPath = new File(booksDirAsString);
2 File filesList[] = directoryPath.listFiles();
3 for(File file : filesList) {
4 System.out.println("File name: "+file.getName());
5 }
6. For the Press, the “edition” attribute should be of type Map
. Remember
that Map is an interface that cannot be instantiated so you’ll want to instantiate a HashMap<
String,Integer> here. Also recall that we use the wrapper class Integer and not simply
int because Java’s collections cannot be build on primitive data types. Similarly, you will likely
want to use HashMap
> for the “shelf” attribute (LinkedList
is a class that implements the Queue interface‘).
7. In order to extract title, author and content info from a text file, consider reading the whole file
content into a String and using regular expressions just like in the labs.
Submission
Submit you solution using submit50 just like the lab exercises.
submit50
To check that your submission is in the correct format you can run check50 on the same slug.
7
COMP122 Assessment 3 Due 2021-05-07 at 5pm
Fine Print
1. Submissions are subject to UoL’s Code of Practice on Assessment and the usual late penalties
apply. This means you can still submit until 120 hours past the deadline, and will incur a -5%
penalty for each 24 hour period immediately following the deadline. Submissions aer that will
not be considered. You can submit multiple times and only the latest version (and it’s submission
time) will be used for grading.
2. 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.
3. Do not define classes in any Java packages! Most IDEs will suggest including a statement like
1 package yourProjectName;
at the beginning of each java file. However, in order to tests your submission we need to know
the namespace where your classes are defined and the most fail-proof assumption is to use the
default namespace, i.e., no package definition. If you don’t know what this is about, don’t worry
and make sure your code does not contain any package declarations as above and compiles fine
using javac *java in the directory that contains your files.
4. Code that does not compile will get zero points and will not be further considered. Make sure
your code compiles directly using javac on the console.
If you require an extension due to extenuating circumstances please get in touch with the CS student
oice (csstudy@liv.ac.uk) before the submission deadline. We cannot grant any extensions aerwards.
If you are granted an extension you cannot in addition submit late aer your personal deadline.
The results will be made available on Canvas and we aim for a turn-around time of three weeks.
8
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做 program、代写 c++设计程...
2024-12-23
comp2012j 代写、代做 java 设...
2024-12-23
代做 data 编程、代写 python/...
2024-12-23
代做en.553.413-613 applied s...
2024-12-23
代做steady-state analvsis代做...
2024-12-23
代写photo essay of a deciduo...
2024-12-23
代写gpa analyzer调试c/c++语言
2024-12-23
代做comp 330 (fall 2024): as...
2024-12-23
代写pstat 160a fall 2024 - a...
2024-12-23
代做pstat 160a: stochastic p...
2024-12-23
代做7ssgn110 environmental d...
2024-12-23
代做compsci 4039 programming...
2024-12-23
代做lab exercise 8: dictiona...
2024-12-23
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!