weixin_43509224 2018-10-24 12:55 采纳率: 0%
浏览 687

本人计算机编程初学者,在国外学习,给的作业不知如何入门求大神解读

以下是题目联系 求大神指导一二

此问题如何进行编写

            The TextBook class.

Fields (Instance Variables):
The class should have three private fields: one of type String to represent the title of the text book, one of type int to represent the length of the book in chapters, and one of type int to represent the number of the last chapter to be read (i.e. the number of chapters read so far).
Constructor Method:
The class should provide a constructor method with two parameters, one of type String and the other of type int. The first parameter represents the title of the book and the second its length in chapters. The constructor should therefore initialise a TextBook object by setting the title of the book to the specified String, and the length of the book to the specified int. In addition, the number of the last chapter read should initially be set to zero (as nothing has been read at this point)
Other Methods:
A TextBook object should also have the following public methods associated with it:
• a method getTitle() that returns the String that represents the title of the book.
• a void method readNextChapter() that 'reads' the next chapter of the book (if this is possible): i.e. it should cause the 'last chapter' field to be increased by one, but only if there are chapters left to be read! If there are no more chapters left to read (i.e. the last chapter read was the final chapter of the book) then it should simply print out a suitable warning message.
• a boolean method isFinished() that returns true if there are no chapters of the book left to read and false otherwise.
• a void method closeBook() that resets the last chapter read field to zero (i.e. it is used to return the book to its initial state, ready for the next person to begin reading it).
• a void method describe() that prints out a simple description of the text book, with the book title and number of chapters left to read, e.g.: "Programming with Java with 4 chapters left to read"
The LibraryCard Class
Fields (Instance Variables):
Your implementation of the LibraryCard class should provide the following private fields:

• an int field that holds the limit on the number of books that the card can be used to borrow.
• an int field that holds the number of books that have been borrowed on the card so far.
• a String field that holds a card reference, for e.g. "cardID 3", "cardID 27" or "cardID 94".
Constructor:
The constructor method should take two int parameters:
the first is used to set the limit on the number of books that can be borrowed with the card.
• the second is used to set the card reference. Note: the card reference should be a String of the form "cardID NUM", where NUM is the actual value of the second formal parameter of the constructor.
The constructor should also initialise the 'number of books borrowed' field as 0 when the card is constructed.
Methods:
There are four methods in addition to the constructor:
a method with signature public void swipe() which when invoked, increases the number of books borrowed on the card by one.
• a method with signature public boolean expired() which returns true if no more books can be borrowed on the card, and false otherwise.
• a method with signature public String getCardRef() that returns the value of the card reference field.
• a method with signature public void describe() that prints out a description of the card, for e.g. "Library card cardID 27 with 3 books left".
The Library Class
Fields (Instance Variables):
Your implementation of the Library class should declare the following private fields:
A field TextBook[] bookShelf which holds the library's collection of TextBook objects.
• You will also find it useful to have an int field (called nextBook or something similar) which indicates the position on the bookShelf where the next TextBook object can be found (the next one available for borrowing when someone presents a valid library card).
• an int field representing the number of borrowers who have been issued with library cards.
Constructor:
The class should have a constructor with a single integer parameter which determines the maximum number of TextBook objects that may be held on the library's bookShelf.
Note: A problem here is to set up the Library object up so that it contains TextBook objects in an array. There are several steps in Java to setting up an array of objects:
1. Declare a variable of the appropriate type (in this case it is just the field bookShelf).
2. Create an array and store it in the variable (at this point it is an array of nulls).
3. Fill the array up with suitable objects (for which you will need to use a suitable loop statement)
A further issue is how to construct the TextBook objects that are put into the bookShelf array. One simple possibility is to give all the books the same title and number of chapters. A better possibility would be to give the books different titles, for example "text book 1", "text book 2","text book 3" and so one (the number of chapters can still be the same for each book). This is fairly simple to do as you can keep track of the number of each book as you go through the loop statement.
Methods:
The class has the following methods in addition to the constructor:
a method with signature public LibraryCard issueCard() that when invoked issues (i.e. returns) a new LibraryCard object. The method should increment the count of the number of library borrowers by 1. Note: to issue a new LibraryCard it is necessary to provide two int parameter values (see description of LibraryCard constructor, above). The limit on borrowing can be fixed with a small int value, such as 5. However, the card identity number should be unique to each card, so you need to find a way of setting the actual parameter value to a different int value each time a new card is constructed. (Hint: think about numbering the cards sequentially, according to the number of borrowers. So, the first card issued might be given the reference "cardID 0", the next card "cardID 1", and so on.)
• A method with signature public TextBook borrowBook(LibraryCard card), which allows a book to be borrowed from the library if a valid (i.e. non-expired) LibraryCard is presented. If there are still books left to be borrowed in the library and the card object has not expired, then the method should return the next TextBook object from the bookShelf array (and otherwise, it should return the null object). The method should also 'remove' the TextBook object from the bookShelf (by setting the array location to null) and increment the field that indicates the location of the next book that may be borrowed. Finally, the method should ensure that the library card is 'swiped' to record that a book has been borrowed. (Note: you may also wish to ensure that your method behaves sensibly if passed a null LibraryCard object.)
• a method with signature public void returnBook(TextBook book) that allows a book to be handed back to the library. The method will need to put the book back in the bookShelf array. Note: it is important that it should do this in such a way that there are no 'gaps' on the shelf, and you may find that the pointer to the next book to be borrowed is again useful here. Once the TextBook has been put back in the array, the method will also need to change the 'next book' pointer appropriately.
• a method with signature public void describe() that, as usual, is responsible for printing out a description of the object. In this case, an example of the sort of message that should be printed out is: "The library has 15 books left on the shelf and has issued 7 library cards".
Testing
Having written the classes make sure that they work properly. Get one or more instances of the classes onto the BlueJ workbench and check that they behave the way that they should. For example, what happens if you try to borrow a book with an invalid card, or when there are no books left on the book shelf? Make sure that you can only borrow as many books from the library as were put on the book shelf in the first place, and that library card expires once it has been swiped up to its limit. Use BlueJ's inspect facility to check that text books borrowed from the library really are removed from the bookShelf array, and so on.
Layout and Code Style
It is important to make sure that you lay out your code properly. In particular make sure that everything is indented properly, and that opening and closing braces are lined up. Also make sure you use meaningful variable names and follow the Java naming conventions (i.e. follow capitalisation conventions). Note that no comments are required at this stage (we will be looking at commenting later).

Marking Scheme
50 marks awarded in all.
class TextBook: (12 marks overall)
Fields: 2 marks for correct work.
Constructor: 2 marks for correct work.
Methods: 8 marks in all for correct work. Methods getTitle() and closeBook() are worth 1 mark each. Method isFinished(), describe() and readChapter() carry 2 marks each. All marks lost for a method if it is completely incorrect, incoherent or missing. Lose 1 mark is isFinished() does the right thing but redundantly. Similarly, lose 1 mark for describe() if it uses more than a single print statement. For reachChapter(), the conditional expression should not allow a chapter to be read if the book is finished.
class LibraryCard: (worth 15 marks overall)
Fields: 2 marks for correct work.
Constructor: 3 marks for correct work.
Methods: 10 marks for work as per the spec. Methods describe() and getCardRef() are worth 2 marks each; methods swipe() and expired() are worth 3 marks each; Marks lost for use of incorrect return types, or over-use of conditional statements in method expired() etc.
class Library: (worth 23 marks overall)
Fields: 2 marks for correct work. Marks lost for missing out a field or failing to make it private or of the correct type.
Constructor: 4 marks for correct work. Marks lost for failing to initialise fields appropriately. Note that a loop is needed to initialise the bookShelf[] array. Marls lost for getting the bounds on the loop wrong or failing to build a new TextBook object to assign to each location. One mark lost for failing to give each book a unique title (same number of chapters is fine).
Methods: 17 marks for correct work. Methods returnBook() and describe() earn 3 marks each; method issueCard() is worth 4 marks, and method borrowBook(LibraryCard card) is worth 7. Marks lost for a method if it is completely incorrect or missing. Marks also lost for failing to implement a method as described in the brief (e.g check that borrowers field is incremented when a card is issued, and that the LibraryCard gets a unique card reference number. Marks lost if the the returnBook(TextBook book) method doesn't put books back in the way specified in the brief.
Layout & Commenting:
Code should be laid out clearly and systematically. Indentation should be used, and a consistent and sensible bracketing convention adopted throughout. Marks are lost for weird, unsystematic, inconsistent or obscure layout. Comments are not expected.

  • 写回答

2条回答 默认 最新

  • Italink 2018-10-24 13:23
    关注

    TextBook class

     class TextBook {
        String title;
        int length;
        int last;
    
        public TextBook(String str,int len) {
            title=str;
            length=len;
            last=0;
        }
    
        public String getTitle(){
            return title;
        }
    
        public void readNextChapter(){
            if(last>length){
                System.out.println("ERROR : There is no next chapter");
                return;
            }
            last++;
        }
    
        public boolean isFinished(){
            if(last==length)
                return true;
            return false;
        }
    
        public void closeBook(){
            last=0;
        }
    
        public void describe(){
            System.out.println(title+" with "+(length-last)+"chapters"+" left to read ");
        }
    
    }
    
    class LibraryCard{
        int limit;
        int number;
        String id;
    
        public LibraryCard(int l,int num){
            limit=l;
            id="cardID "+String.valueOf(num);
            number = 0;
        }
    
        public void swipe(){
            number++;
        }
    
        public boolean expired(){
            if(number>limit)
                return false;
            return true;
        }
    
        public String getCardRed(){
            return id;
        }
    
        public void descripiton(){
            System.out.println("Library card "+getCardRed()+" with "+number+" books left ");
        }
    
    
    }
    
    class Library{
        TextBook[] bookShelf;
        int nextBook;
        int borrowers;
    
        public Library(int max){
            bookShelf= new TextBook[max];
        }
    
        public LibraryCard issueCard(){ 
            return new LibraryCard(5,borrowers++);
        }
    
        public TextBook borrowBook(LibraryCard card){
            if(card.expired()&&(bookShelf.length>=nextBook&&card!=null)){
                bookShelf[nextBook]=null;
                card.swipe();
                return bookShelf[nextBook++];
            }
            else
                return null;
        }
    
        public void returnBook(TextBook book){
            bookShelf[--nextBook]=book;
        }
    
        public void describe(){
            System.out.println("The library has "+(bookShelf.length-nextBook)+" books left on the shelf and has issued "+borrowers+" library cards ");
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?