COMP122编程语言 辅导、 写作java程序语言

” COMP122编程语言 辅导、 写作java程序语言COMP122 Assessment 3 Due 2021-05-07 at 5pmExceptional PressBook-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(): StringPress-booksPerEdition: int-edition: MapString,Integer-shelf: MapString,QueueBook+Press(String p, int n)+print(String id, b): ListBook+GetCatalogue() ListString+request(String id, n) ListBookVendingMachine-supplier: Press-locationFactor: double-cassette: double+VendingMachine(Press p, double f)+getCassette(): double+insertCoint(double c): void+returnCoins(): double+buyBook(String id): BookDuring the pandemic you realised that lots of people are looking for books to read but are reluctantto throw their money at large online corporations while shops are closed. So you came up with theidea to re-print old classics whose copyright has expired and sell them o vending machines that youput up where once public libraries stood. In this assignment youll implement three aspects of thisendeavour: You will model books, the press, and vending machines.Part 1: BooksWell 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 attributecalled edition. Since we want readers to be able to see but not change the respective values of abook, all these fields must be Declared private and your class should have accessor-methods foreach of them. That is, your class should have public methods called getTitle, getAuthor, andgetContent that take no arguments and return a String, namely the unchanged value of theirrespective attribute. Similarly, there must be a method public int getEdition() that returns theedition of the book.Your class must feature a constructor method that accepts and stores initial values for these fourattributes 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.Lets assume the that the layout of our books is such that it fits 800 characters on every page. Adda (public) method called getPages that takes no arguments and returns the number of pages the1COMP122 Assessment 3 Due 2021-05-07 at 5pmbook has as an int. That is, it returns the integer corresponding to the number of characters in thebooks 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 returntitle, author, edition, as well as the number of pages in this order. Each should appear in one line andprefixed with the capitalised name of the attribute followed by a colon and one space. Each line shouldend in a newline character \n directly aer the last printable character. See below for an exampleoutput String.Title: Treasure IslandAuthor: Robert Louis StevensonEdition: 1Pages: 209Part 2: The PressWrite a class called Press that can be used to create books.The books well create are going to be re-prints of famous volumes whose copyright has expired. We canget them as (UTF8 encoded) text files from Project Gutenberg and a selection can also be downloadedfrom Canvas for testing. When we instantiate a Press object well be given a path to a directory thatcontains several such text files. Well assume for simplicity that each kind of book in print is identifiedby the name of the text file it is Read from, e.g. 120-0.txt. Such a bookID string should includethe suix .txt but no preceding directory name.As it is cumbersome and costly to reconfigure our press when switching to a new book, well printbooks in batches and store them until they are distributed to our vending machines. Our press shouldtherefore have several shelves, one for each kind of book. If someone wants to take out n copies ofa book, we will give them the first n from the shelf. If there are fewer than that many le we printnew ones (with a new edition number), add them to the end of the shelf and give out the requestednumber of books. Since storage space is expensive there is an fixed number of books each edition willhave. This number is stored in a private field of each Press object and fixed upon instantiation. Wecan assume that no user wants to take out more than that many books.Setting up the pressYour 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 stringsinto an integers. That is, there should be a single private attribute called edition of typeMapString, Integer.2COMP122 Assessment 3 Due 2021-05-07 at 5pm a shelf to store books. This should be implemented as a map from BookID strings into aqueue of Books. That is, there Should be a single private attribute called shelf that has typeMapString, QueueBook.You may add other private attributes to make things work, for example to store the content of eachtext file so that you do not need to read it again and again. We will not look at these attributes whentesting and grading your code.Your class should have a constructor method that takes 1. a (String) path to the directory containingbooks as text files, 2. the (int) number of books printed per edition. This should set up the datastructures 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 directorymybooks contains three files called 11-0.txt, 120-0.txt, and 12-0.txt. Then the newly instantiatedPress object should contain as shelf attribute, that maps 11-0.txt to a Queue containing three distinct first edition Bookswhose 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 alreadyprinted one edition of each book to fill the shelf).Printing booksIn order to instantiate a single book we need to read the respective text file and extract title, author andcontent. 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, languageand so on. Then there is a line that starts with *** START OF that ends the header and marks thebeginning of the books content. For example, here are the first 23 lines of 120-0.txt.1 The Project Gutenberg EBook of Treasure Island, by Robert Louis Stevenson23 This eBook is for the use of anyone anywhere in the United States and most4 other parts of the world at no cost and with almost no restrictions5 whatsoever. You may copy it, give it away or re-use it under the terms of6 the Project Gutenberg License included with this eBook or online at7 www.gutenberg.org. If you are not located in the United States, youll have8 to check the laws of the country where you are located before using this ebook.910 Title: Treasure Island1112 Author: Robert Louis Stevenson1314 Illustrator: Louis Rhead3COMP122 Assessment 3 Due 2021-05-07 at 5pm1516 Release Date: March 13, 1994 [EBook #120]17 [Most recently updated: October 28, 2020]1819 Language: English2021 Character set encoding: UTF-82223 *** START OF THIS PROJECT GUTENBERG EBOOK TREASURE ISLAND ***Everything from (and including) line 24 to the end of the file well 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 textfile and extracts title, author and content strings and then uses your four-argument constructor for theBook 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 toprint a whole batch of books at once.1 public ListBook print(String bookID, n)This method should instantiate n books from the text file whose name is given as the string parameterbookID and return a list that contains these n books. The edition of all new books should be the sameas the new value of edition attribute of our press (for the given bookID), which of course should beone 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 atext file does is not in the form described above, for example if it does not contain a line starting inTitle: then this method should throw an java.util.IllegalFormatException.Distributing BooksYour Press class needs to have a public method called getCatalogue() that takes no argumentsand returns a list Of books it can produce. Again, lets assume the book is identified by the name of thetext file it is read from, e.g. 120-0.txt. The getCatalogue method should return a list of Stringsand be declared exactly as below.1 public ListString getCatalogue()Your Press class needs to have a method called request to allow users to take out a number of booksat once. It should have a signature as follows.1 public ListBook request(String bookID, int amount)4COMP122 Assessment 3 Due 2021-05-07 at 5pmA user would give a bookID string that identifies the book and the number of copies to take. Theresulting list of books must naturally have the same length as requested and contain that many distinctcopies of the requested book.This method should return the books from the shelf or trigger a re-print if there are not enough copiesin store: If someone wants to take out n copies of a book, we will give them the first n from the shelf. Ifthere are fewer than that many le we print new ones (with a new edition number), add them to theend of the shelf and give out the requested number of books. We can assume that no user wants totake 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 booksWrite a class called VendingMachine that can sell books to customers. Each such vending machinewill be supplied by a press and oers all books that press can print. The price each book sells for willdepend on the size of the book but also on some location factor that allows us to sell books at higherprices in posher areas. For instance, a book with 500 pages sells for 500*0.01 = 5.0 GBP from avending 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 numberof 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 (adouble), and size (an int).The class should also have a private double attribute called cassette and a corresponding getterpublic double getCassette(). Moreover, there must be public methods as follows.1. a method called insertCoin1 public void insertCoin(double coin)that adds the value of is parameter to the cassette. This method should throw anIllegalArgumentException if the given coin is not of the right denomination. Acceptablevalues are 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, and 2.2. a method called returnCoins5COMP122 Assessment 3 Due 2021-05-07 at 5pm1 public double returnCoins()that empties the cassette (sets it to zero) and returns its original value.3. a method called buyBook1 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 mostthe value of cassette. The price of the book is the number of pages multiplied by the locationfactor of the vending machine.If the cassette contains enough money then the method removes the book from the shelf, reducescassette by the price of the book and returns the book. If the book sold like this was the last ofits kind (meaning aer the sale the machine has none le) then the vending machine contacts itssupplier press and requests more copies of this book. The number of copies it requests shouldbe determined by the value of the size attribute, so we request as many copies of this book aswe can fit.The buyBook method should also be able to throw (unchecked) exceptions for the following twoscenarios:1. If the parameter bookID is not a valid (there is no book by this id on oer) then this shouldthrow an IllegalArgumentException.2. If the price of the book exceeds the value of the cassette then the method should throw aCassetteException. This type of exception is of course special to our application andyou should define it yourself as a new type of RuntimeException.6COMP122 Assessment 3 Due 2021-05-07 at 5pmHints1. 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. Youcan 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 instantiateyour classes into objects. This means that if your constructor methods are missing or callingthem 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 MapString, Integer. Rememberthat Map is an interface that cannot be instantiated so youll want to instantiate a HashMapString,Integer here. Also recall that we use the wrapper class Integer and not simplyint because Javas collections cannot be build on primitive data types. Similarly, you will likelywant to use HashMapString, LinkedListBook for the shelf attribute (LinkedListis 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 filecontent into a String and using regular expressions just like in the labs.SubmissionSubmit you solution using submit50 just like the lab exercises.To check that your submission is in the correct format you can run check50 on the same slug.7COMP122 Assessment 3 Due 2021-05-07 at 5pmFine Print1. Submissions are subject to UoLs Code of Practice on Assessment and the usual late penaltiesapply. 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 willnot be considered. You can submit multiple times and only the latest version (and its submissiontime) 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 workto that of other students.3. Do not define classes in any Java packages! Most IDEs will suggest including a statement like1 package yourProjectName;at the beginning of each java file. However, in order to tests your submission we need to knowthe namespace where your classes are defined and the most fail-proof assumption is to use thedefault namespace, i.e., no package definition. If you dont know what this is about, dont worryand make sure your code does not contain any package declarations as above and compiles fineusing 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 sureyour code compiles directly using javac on the console.If you require an extension due to extenuating circumstances please get in touch with the CS studentoice (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.请加QQ:99515681 或邮箱:99515681@qq.com WX:codehelp

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导