mutators


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: mutators

  1. #1
    Join Date
    Oct 2003
    Posts
    21

    Angry mutators

    i need help with some code modification
    This is a book class
    Code:
      // The fields.
        private String author;
        private String title;
        private int pages;
        private String refNumber;
        private int borrowed;
    constructors and some methods omitted

    basically the integer field borrowed is meant to keep a count of the number of times has been borrowed, what i need help with is adding a mutator borrow, and this should update the field by 1 each time it is called, include an accessor getBorrowed, that returns a value of this new field as its result.


    The main code is below it self, if anyone has time to look at it please help,


    and this runs in a normal jdk environment but we use an interface called blue j to view things(as a terminal window)
    Code:
    /**
      * @author (Aisha Aliyu)
      * @version (28/10/2003)
      */
    class Book
    {
        // The fields.
        private String author;
        private String title;
        private int pages;
        private String refNumber;
        private int borrowed;
    
        /**
         * Set the author and title fields when this object
         * is constructed.
         */
        public Book(String bookAuthor, String bookTitle, int bookPages, String ref, int borrow)
        {
            author = bookAuthor;
            title = bookTitle;
            pages = bookPages;
            refNumber = ref;
            borrowed = borrow;
    
        }
    
       // The number of pages. 
        public int getPages()
        {
        return pages;
        }
    
        // return the refrence number of the book
        public String getRefNumber()
        {
            return refNumber;
        }
    
      // Simulate the printing of the author's name.
       public void printAuthor()
        {
         System.out.println("*Book Author*= " + author); 
        }
    
      // Simulate the printing of the book's title.  
       public void printTitle()
       {
         System.out.println("*Book Title* = " + title);
       }
    
      // Simulate the printing of the book details.
       public void printDetails()
    {
           if(refNumber == "") {
        System.out.println("*Book Author*      = " + author);
        System.out.println("*Book Title*       = " + title);
        System.out.println("*Pages*            = " + pages);
        System.out.println("*Reference Number* = " + refNumber); 
    }
        else {
    
            System.out.println("*Reference Number* = zzz");
        }
    }
    
      // The refrence mutator.
        public void setRefNumber(String ref)
       {
       if(ref.length() < 3) {
                System.out.println("The reference number must be at least 3 characters");
           }
           else {
           refNumber = ref;
           }
    }
    
    // The Borrowed mutator.
    public int borrowed()
    {
    return borrowed;
    }
    }
    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    What problem are you having? How do you think it should be done?
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    1
    You aren't by any chance using the book Fundamentals of Java by Martin Osborne, are you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center