简体   繁体   中英

(Beginner Java issues) Why isn't this code working?

(full code below) I have just handed in a lab for class where we had to make a class that describes a certain book. I could not figure out how to do two things. 1. If someone inputs a value less than zero for the 'pages' or 'suggestedRetailPrice' , the value must be set to zero. In this code the value is set 0 even if the value is positive. In the:


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

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


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

Then the value for whatever you input for 'pages' will be 1. But shouldn't it be 1 ONLY if the value you input is a negative number? I don't get what I'm doing wrong.

The second thing I could not figure out is at the bottom of the code, we had to display all info. My teacher wanted us to display whether or not the book was paperback as 'yes' or 'no' instead of 'true' or 'false'. How do I do this? I tried putting an if/else statement in like this: System.out.println("Paperback : " + if (paperback = true) {Yes} if (paperback = false) {no}; )

Didn't work, can't figure it out. See whole 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);
    }
}

You're checking the pages value before setting it with 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 your pages value:

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

For your second question, create a String, say called isPaperback, set it to "yes" String to your output String if 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 testing the value of paperback.

ie,

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

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

if(numberOfPages <= 0) pages = 0

Also, Bonus Points you can make this all neat and pretty if you can figure out how to use the ternary operator, but I'll leave that one up to you.

As for the print statement take the "if" outside of the print so it would be.

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

Also look this is another great spot for the ternary operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM