(Java beginner problems) Why doesn't this code work?

(full code below) I just passed a lab for a class where we needed to create a class that describes a specific book. I couldn't figure out how to do two things. 1. If someone enters a value less than zero for "pages" or "suggested by RetailPrice", the value must be set to zero. In this code, the value is 0 even if the value is positive. IN:


if ( pages <= 0 )
    {
        pages = 0;
    }

      


if I set the second "0" to a different number, say:


if ( pages <= 0 )
    {
        pages = 1;
    }

      


Then the value for what you enter for "pages" will be 1. But shouldn't it be 1 ONLY if the value you entered is negative? I don't understand what I am doing wrong.

The second thing I couldn't figure out is at the bottom of the code, we needed to display all the information. My teacher wanted us to show if the paperback was β€œyes” or β€œno” instead of β€œtrue” or β€œfalse”. How should I do it? I tried to put the if / else statement like this: System.out.println ("Paperback:" + if (paperback = true) {Yes} if (paperback = false) {no};)

Doesn't work, I can't understand. See all the code below.


public class Book {
    // Instance variables
    private String title;
    private String author;
    private int isbn;
    private int pages;
    private boolean paperback;
    private int suggestedRetailPrice;

    /**
     * Default contructor
     */
    public Book() {
        title = "";
        author = "";
        isbn = 0;
        pages = 0;
        paperback = false;
        suggestedRetailPrice = 0;
    }

    /**
     * book information
     */
    public Book(String whatIsTitle, String whoIsAuthor, int isbnCode,
            int numberOfPages, boolean isItPaperback,
            int theSuggestedRetailPrice) {
        title = whatIsTitle;
        author = whoIsAuthor;
        isbn = isbnCode;
        if (pages <= 0) {
            pages = 0;
        } else {
            pages = numberOfPages;
        }
        paperback = isItPaperback;
        if (suggestedRetailPrice <= 0) {
            suggestedRetailPrice = 0;
        } else {
            suggestedRetailPrice = theSuggestedRetailPrice;
        }
    }

    /**
     * @return title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * @return isbn
     */
    public int getIsbn() {
        return isbn;
    }

    /**
     * @return pages
     */
    public int getPages() {
        return pages;
    }

    /**
     * @return paperback
     */
    public boolean getPaperback() {
        return paperback;
    }

    /**
     * @return suggestedRetailPrice
     */
    public int getSuggestedRetailPrice() {
        return suggestedRetailPrice;
    }

    /**
     * title
     */
    public void setTitle(String whatIsTitle) {
        title = whatIsTitle;
    }

    /**
     * author
     */
    public void setAuthor(String whoIsAuthor) {
        author = whoIsAuthor;
    }

    /**
     * isbn code
     */
    public void setIsbn(int isbnCode) {
        isbn = isbnCode;
    }

    /**
     * number of pages
     */
    public void setPages(int numberOfPages) {
        if (pages <= 0) {
            pages = 0;
        } else {
            pages = numberOfPages;
        }
    }

    /**
     * is it paperback
     */
    public void setPaperback(boolean isItPaperback) {
        paperback = isItPaperback;
    }

    /**
     * suggested retail price
     */
    public void setSuggestedRetailPrice(int theSuggestedRetailPrice) {
        if (suggestedRetailPrice <= 0) {
            suggestedRetailPrice = 0;
        } else {
            suggestedRetailPrice = theSuggestedRetailPrice;
        }
    }

    /**
     * displays information
     */
    public void displayBook() {
        System.out.println("Title : " + title);
        System.out.println("Author : " + author);
        System.out.println("ISBN : " + isbn);
        System.out.println("Pages : " + pages);
        System.out.println("Paperback : " + paperback);
        System.out.println("Suggested price : " + suggestedRetailPrice);
    }
}

      

+3


source to share


2 answers


You check the page value before setting it using the parameter, numberOfPages:

 title = whatIsTitle;
 author = whoIsAuthor;
 isbn = isbnCode;

 // pages is still at its initialzed value of 0 here.
 if ( pages <= 0 )
 {
     pages = 0;
 }
 else
 {
     pages = numberOfPages; // this will *never* be called
 } 

      

Reverse this order. Or better, check the parameter value and use it to set the value of your pages:

if (numberOfPages < 0) {
   pages = 0;
} else {
   pages = numberOfPages
}

      



For your second question, create a String called isPaperback, set it to "yes" String if the paperback is true and "no" if not, and then display that string when you need it. Either that, or put your System.out.println ("yes") in an if block that checks the paperback value.

i.e.,

if (paperback) {
   System.out.println(...);
} else {
   System.out.pringln(...);
}

      

+4


source


I'm a little unsure what you are trying to do, but I notice that in the Book class you are using "pages" when the parameter is "numberOfPages". Try to do

if(numberOfPages <= 0) pages = 0

      

Also, Bonus Points you can do it all neatly and nicely if you can figure out how to use the ternary operator, but I'll leave that up to you.



As for the print statement, take the "if" outside of print to have it.

if(paperback) System.out.println("Paperback: Yes")
else System.out.println("Paperback: no)

      

Also see that this is another great place for the ternary operator.

+1


source







All Articles