Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with boolean logic please!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with boolean logic please!

    This is a take-home lab we have to do. I have all but this last part working. The lab states
    "Write these methods:

    public void displayBook() displays the book information on the screen like this:
    Title : Ghost Stories of British Columbia
    Author : Jo-Anne Christensen
    ISBB : 0-88882-191-3
    Pages : 192
    Paperback : yes
    Suggested price : $17.99
    Note that the display method checks the boolean field and displays “yes” if true, “no” if
    false.

    public boolean isHeavyBook() returns true if the book is hard-cover (not
    paperback) and has at least 500 pages. Use a symbolic constant for the number of
    pages." So I think I have the displayBook method right but I am unclear as to how to write the isHeavyBook method which is supposed to update the Paperback field in the displayBook method.

    Here is my code so far:

    /**
     * This class shows some of the relevant attributes of a Book
     * 
     * @author Randy Watson 
     * @version October 5 2013
     */
    public class Book
    {
       public static final int PAGES_AMOUNT = 500;
     
       public String title;
       public String author;
       public String bookCode;
       public int numPages;
       public boolean pBack;
       public double retailCost;
     
       /**
        * This is the default constructor
        */
     
       public Book()
       {
           title = "";
           author = "";
           bookCode = "";
           numPages = 0;
           pBack = false;
           retailCost = 0.0;
       }
     
       /**
        * Non-default constructor
        */
     
       public Book(String nameOf, String writer, String iSBN, int pageCount, boolean paperBack, double cost)
       {
           title = nameOf;
           author = writer;
           bookCode = iSBN;
           if(pageCount <= 0)
           System.out.println("Page amount must be positive");
           else
           pageCount = PAGES_AMOUNT;
           pBack = paperBack;
           retailCost = cost;
       }
     
       /**
        * @return The title of the book
        */
     
       public String getTitle(String bookTitle)
       {
           return title;
        }
     
        /**
         * @return The books author
         */
     
        public String getAuthor(String bookAuthor)
        {
            return author;
        }
     
        /**
         * @return Book ISBN number
         */
     
        public String getISBN(String bookNumber)
        {
            return bookCode;
        }
     
        /**
         * @return The number of pages in the book
         */
     
        public int getNumberOfPages(int pageAmount)
        {
            return numPages;
        }
     
        /**
         * @return if the book is paperback or hardcover
         */
     
        public boolean getPaperBack(boolean heavyCover)
        {
            return pBack;
        }
     
        /**
         * @return the suggested retail price of the book
         */
     
        public double getRetailCost(double bookCost)
        {
            return retailCost;
        }
     
        /**
         * Mutator Method to change the book title
         */
     
        public void setBookTitle(String title1)
        {
            title = title1;
        }
     
        /**
         * Mutator Method to change the book's author
         */
     
        public void setAuthor(String author1)
        {
            author = author1;
        }
     
        /**
         * Mutator Method to change the book's ISBN number
         */
     
        public void setISBN(String newISBN)
        {
            bookCode = newISBN;
        }
     
        /**
         * Mutator Method to change the amount of pages in the book
         */
     
        public void setPages(int newPageAmount)
        {
            if (numPages <= 0)
            numPages = newPageAmount;
        }
     
        /**
         * Mutator method for checking if the book is hardcover or paperback
         */
     
        public void setHardCover(boolean heavyBook)
        {
            pBack = heavyBook;
        }
     
        /**
         * Mutator method for changing the cost of the book
         */
     
        public void setBookCost(double bookCost)
        {
     
            if(retailCost <= 0)
            System.out.println("Cost must be more than zero");
            else
            retailCost = bookCost;
        }
     
        /**
         * Method to display the fields of the book class and to check if the book us hardcover or not
         */
     
        public void displayBook()
        {
            System.out.println("Title : " + title);
            System.out.println("Author : " + author);
            System.out.println("ISBN : " + bookCode);
            System.out.println("Pages : " + numPages);
            System.out.println("Paperback : " + pBack);
            System.out.println("Suggested price : " + retailCost);
        }
     
        /**
         * @return Method for determining if book is hardcover or not
         */
     
        public boolean isHeavyBook()
        {
            boolean heavy;
            if(pBack == true && PAGES_AMOUNT) {
                return true;
            }
            return false;
     
        }
    }
    I think I also have something wrong with displayBook info method because if I am checking it it is hardcover in another method how do I get that method to use the isHeavyBook method for checking? I hope whoever reads this can understand what I am trying to say and can help me. Thank you in advance.


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with boolean logic please!

    Is the book heavy when the PAGES_AMOUNT is 500 or greater because it seems you made it seem like a Boolean maybe that's the problem. Hope this helps!

    --- Update ---

    I meant that you made it seem like a boolean in your isHeavyBook() method when you said && PAGES_AMOUNT sorry @_@

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with boolean logic please!

    Its supposed to check if the book is hardcover or not and if it has at least 500 pages. I know it has something to do with a local variable as well as using the symbolic constant along with the Boolean logic but I cant figure it out.

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Help with boolean logic please!

    A few questions for you:
    1. Why do you pass a getter an argument? public double getRetailCost(double bookCost)
    To invoke it the programmer would have to write out Book.getRetailCost(3.99); or any other double which makes no sense since you are trying to get the value stored in that object.

    2. Your variable pBack refers to what exactly when its true? (true = paperBack or true = hardBack?) It cannot be true for both paper back and hard back since this is what is going to define whether it is one or the other. having it named as pBack makes it seem like it is for paper back when it is set to true but you create a method called setHardCover(boolean heavyBook) which sets pBack = heavyBook and makes it confusing.

    3. In your isHeavyBook() Method you create a boolean heavy which is not needed nor used. In your if statement you check if pBack (Variable causing confusion mentioned in 2nd question. You also check if PAGES_AMOUNT but pages amount what? You need to provide it a condition to check for in your case if the pages are higher than a predetermined amount.

    I am willing to help you out. It is small issues you just need to be a little clearer on things.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with boolean logic please!

    Hopefully the answers to your questions:

    1) I am passing it that argument because for lack of a better reason that is what I have to done with the other get methods. Unless this is a different type of get method.
    2) pBack is supposed to refer to when the book is set to hardcover or paperback. I thought I was referring it to true = hardcover but I get your point that using that name for that specific variable is confusing so I have changed it.
    3) In the layout that we have it says that that we have to write in the isHeavyBook method to check if the book is hardcover and if it is over 500 pages and if both conditions are true then it needs to display true or false in the displayInfo method for the paperback System.out.println. The errors are cleared but when I run the constructor if I set the hCover value to false and the page amount to 500 it is NOT supposed to allow a change in the field. Here is what we have been given for information so maybe this will clear some things up

    COMP 1409 Lab 3-b ( 4 points )
    Take-home lab
    Write a Book class. Here are the relevant attributes:
     title
     author
     ISBN (code on back cover)
     number of pages
     is it a paperback?
     suggested retail price
    Be sure to include Javadoc comments for constructors and methods. Provide a default
    constructor and a non-default constructor. The default constructor sets the strings to
    empty strings (“”) and the other fields to the default for that data type. Pass all the
    necessary information to the non-default constructor. This constructor must check the
    number of pages and the price parameters to ensure they are not negative. If one of
    them is negative, set the field to zero.
    Write accessor and mutator methods for all instance variables. The mutator methods for
    number of pages and price must ensure that those fields do not become negative. If the
    parameter for one of the methods is negative, do not change the field.
    Write these methods:
    public void displayBook() displays the book information on the screen like this:
    Title : Ghost Stories of British Columbia
    Author : Jo-Anne Christensen
    ISBB : 0-88882-191-3
    Pages : 192
    Paperback : yes
    Suggested price : $17.99
    Note that the display method checks the boolean field and displays “yes” if true, “no” if
    false.
    public boolean isHeavyBook() returns true if the book is hard-cover (not
    paperback) and has at least 500 pages. Use a symbolic constant for the number of
    pages.
    The take-home lab is due before midnight the night before the next class. Upload it to
    the appropriate D2L dropbox. A suggested solution will be discussed in class and labs
    not already in the dropbox will not receive any points.

    Here as well is my updated code:

    public class Book
    {
    public static final int PAGES_AMOUNT = 500;

    public String title;
    public String author;
    public String bookCode;
    public int numPages;
    public boolean hCover;
    public double retailCost;

    /**
    * This is the default constructor
    */

    public Book()
    {
    title = "";
    author = "";
    bookCode = "";
    numPages = 0;
    hCover = false;
    retailCost = 0.0;
    }

    /**
    * Non-default constructor
    */

    public Book(String nameOf, String writer, String iSBN, int pageCount, boolean hardCover, double cost)
    {
    title = nameOf;
    author = writer;
    bookCode = iSBN;
    if(pageCount <= 0)
    System.out.println("Page amount must be positive");
    else
    pageCount = PAGES_AMOUNT;
    hCover = hardCover;
    retailCost = cost;
    }

    /**
    * @return The title of the book
    */

    public String getTitle(String bookTitle)
    {
    return title;
    }

    /**
    * @return The books author
    */

    public String getAuthor(String bookAuthor)
    {
    return author;
    }

    /**
    * @return Book ISBN number
    */

    public String getISBN(String bookNumber)
    {
    return bookCode;
    }

    /**
    * @return The number of pages in the book
    */

    public int getNumberOfPages(int pageAmount)
    {
    return numPages;
    }

    /**
    * @return if the book is paperback or hardcover
    */

    public boolean getPaperBack(boolean heavyCover)
    {
    return hCover;
    }

    /**
    * @return the suggested retail price of the book
    */

    public double getRetailCost(double bookCost)
    {
    return retailCost;
    }

    /**
    * Mutator Method to change the book title
    */

    public void setBookTitle(String title1)
    {
    title = title1;
    }

    /**
    * Mutator Method to change the book's author
    */

    public void setAuthor(String author1)
    {
    author = author1;
    }

    /**
    * Mutator Method to change the book's ISBN number
    */

    public void setISBN(String newISBN)
    {
    bookCode = newISBN;
    }

    /**
    * Mutator Method to change the amount of pages in the book
    */

    public void setPages(int newPageAmount)
    {
    if (numPages <= 0)
    numPages = newPageAmount;
    }

    /**
    * Mutator method for checking if the book is hardcover or paperback
    */

    public void setHardCover(boolean heavyBook)
    {
    hCover = heavyBook;
    }

    /**
    * Mutator method for changing the cost of the book
    */

    public void setBookCost(double bookCost)
    {

    if(retailCost >= 0)
    System.out.println("Cost must be more than zero");
    else
    retailCost = bookCost;
    }

    /**
    * Method to display the fields of the book class and to check if the book us hardcover or not
    */

    public void displayBook()
    {
    System.out.println("Title : " + title);
    System.out.println("Author : " + author);
    System.out.println("ISBN : " + bookCode);
    System.out.println("Pages : " + numPages);
    System.out.println("Paperback : " + hCover);
    System.out.println("Suggested price : " + retailCost);
    }

    /**
    * @return Method for determining if book is hardcover or not
    */

    public boolean isHeavyBook()
    {
    boolean heavy;
    if(hCover == true && PAGES_AMOUNT >= 500) {
    return true;
    }
    return false;

    }

    }

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Help with boolean logic please!

    Well the getters you have don't really prove the need for an argument to be passed down to them.
    For example
    Book b = new Book();
    b.setTitle("Some book");
    System.out.println("Title: " + b.getTitle("A string is required here it does not matter what you write as long as it is a string for this getter"));
    //Prints out:Title: Some book
    As you can see it makes no sense to pass it an argument as it does nothing with it. You would want to simply write b.getTitle(); instead of passing it an argument. Now if it was an array of books stored in the object and you needed to get book 3 then that would be a reasonable place to pass an argument to the getter/accessor.

    For the variable rename there will still be an issue with it. For example in your constructor you ask for hardCover (true/false). Let's say for the sake of the example we set it to true. Now in your displayBook() take a look at the line that says Paperback: " + hCover Well hCover was set to true earlier so how could it be both paper back and hard cover? It made more sense keeping it as pBack and making sure to keep track of false referring to it being a hard cover. The issue was you were setting both of them as true in the method of
    public void setHardCover(boolean heavyBook){ pBack = heavyBook; }

    May I inquire about your main method where you are initializing the book object to test out your results? I am curious as to how you are accessing the isHeavyBook() method in your Book class

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with boolean logic please!

    I am using BlueJ and we don't/have not gotten to a main method per se but it is supposed to check if the book is hardcover AND it is 500 pages or more then change the field in the displayBook method. If one of those checks are not true then it is not supposed to change the field in the displayBook method but it doesn't do that. If I change the hCover to false and pages to 500 or more when I initialize the constructor it doesn't change the field but when I inspect and run the set methods for changing the hCover and pageAmount variables to different options it still changes the field. I understand what you are showing me in your first example but we aren't learning how to write methods that way. The instructor showed us this way and that is the way I am doing it so your first example makes sense but I cant put it in to how we are being showed how to write methods.

  8. #8
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Help with boolean logic please!

    I was asking about the main Method because I am don't see isHeavyBook() invoked anywhere in you Book Class I might just be missing it though. I am unsure how it is suppose to be implemented without the risk of giving you the answer. Keep in mind you can call methods within methods. Think of checking the information when you display the information and based on the result display a certain output. So if conditions are met Paperback: No other wise Yes it is a paper back.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with boolean logic please!

    Thank you Ubiquitous. Although I still am unclear of the logic your help has helped me understand more, but not all, of what I needed to get it to work properly.

Similar Threads

  1. Logic
    By KAJLogic in forum Java Theory & Questions
    Replies: 4
    Last Post: September 3rd, 2013, 04:32 PM
  2. Need logic ....!!!
    By Afzal in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 30th, 2013, 12:12 PM
  3. need logic
    By arunjib in forum Java Theory & Questions
    Replies: 3
    Last Post: November 27th, 2011, 05:39 PM
  4. whats logic behind this?
    By X0X0 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 26th, 2011, 02:48 PM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM