Question

cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

cs55(java) please.

Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes, isFull( returns Boolean), isEmpty(returns Boolean) and toString method that returns all information about the Books in the Bookself. Create a driver class called TestBookshelf, whose main method instantiates five Book objects, instantiates one Bookshelf, updates the Books, and add or remove the Books to the Bookshelf, isDuplicate(Book b). Make sure to print information of the Bookshelf every time you update the Books or the Bookshelf. Add as many API that you think is good to have for the Bookshelf class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.
Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.
Thank You !!
========================================================================================

import java.text.SimpleDateFormat;
import java.util.Date;

public class Book {

    private String title;
    private String author;
    private String publisher;
    private double price;
    private Date copyrightDate;

    public Book() {
        title = "";
        author = "";
        publisher = "";
        price = 0.0;
        copyrightDate = null;
    }

    public Book(String title, String author, String publisher, double price, Date copyrightDate) {
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.price = price;
        this.copyrightDate = copyrightDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Date getCopyrightDate() {
        return copyrightDate;
    }

    public void setCopyrightDate(Date copyrightDate) {
        this.copyrightDate = copyrightDate;
    }

    @Override
    public String toString() {

        String pattern = "MM-dd-yyyy";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String date = simpleDateFormat.format(copyrightDate);


        return
                "Title: " + title +
                        "\nAuthor: " + author +
                        "\nPublisher: " + publisher +
                        "\nPrice: $" + price +
                        "\nCopyright Date: " + date;

    }

    public boolean isDuplicate(Book b) {
        return getTitle().equals(b.getTitle()) &&
                getPublisher().equals(b.getPublisher());
    }
}

========================================================================================

public class Bookshelf {

    private final static int MAX_SIZE=5;
    private Book[] books;
    private int bookCount;

    public Bookshelf(){
        books = new Book[MAX_SIZE];
        bookCount=0;
    }

    public void addBook(Book book){
        if(bookCount<MAX_SIZE){
            books[bookCount++]=book;
        }
    }

    public void removeBook(Book book){
        int indexToDelete=-1;
        // first compare the book to find the index
        // where the book is located
        // two books are same if title and publisher are same
        for(int i=0;i<bookCount;i++){
            Book bookAtIndex=books[i];
            if(book.getTitle().equals(bookAtIndex.getTitle()) &&
            book.getPublisher().equals(bookAtIndex.getPublisher())){
               indexToDelete=i;break; // if the book is found we break out
            }
        }
        if(indexToDelete==-1)return; // book not found
        if(indexToDelete==bookCount-1){ // if the book is the last book
            bookCount--; // we simply decrease the book count by 1
            return;
        }
        // shift the book to one place to the left
        for(int index=indexToDelete;index<bookCount-1;index++){
           books[index]=books[index+1];
        }
        // decrease the count of book by 1
        bookCount--;
    }

    public boolean isFull(){
        return bookCount==MAX_SIZE;
    }
    public boolean isEmpty(){
        return bookCount==0;
    }

    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        for(int index=0;index<bookCount;index++){
            buffer.append(books[index]).append("\n");
        }
        return buffer.toString();
    }


}

========================================================================================

import java.text.ParseException;
import java.text.SimpleDateFormat;


public class TestBookShelf {

    public static void main(String[] args) {
        SimpleDateFormat dateformat= new SimpleDateFormat("dd/MM/yyyy");
        Bookshelf shelf = new Bookshelf();
        Book[] books = new Book[5];
        try {
            books[0] = new Book("A Tale of Two Cities","Jane Austin","Pearson",16.55,dateformat.parse("12/08/190"));
            books[1] = new Book("C++ Concise","Oliver Smith","Tata McGraw Hill",24.75,dateformat.parse("13/10/1990"));
            books[2] = new Book("Concise Java","Stephen Prata","Academics",25.99,dateformat.parse("14/09/1990"));
            books[3] = new Book("Advanced Java","Samuel Wilson","Oasis",34.99,dateformat.parse("15/08/1990"));
            books[4] = new Book("Java for Dummies","Prof. Ivor Horton","Penguin",52.00,dateformat.parse("16/07/1990"));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("Is shelf empty: "+shelf.isEmpty());
        shelf.addBook(books[0]);System.out.println(shelf);
        shelf.addBook(books[1]);System.out.println(shelf);
        shelf.addBook(books[2]);System.out.println(shelf);
        shelf.addBook(books[3]);System.out.println(shelf);
        shelf.addBook(books[4]);System.out.println(shelf);
        System.out.println("Is shelf full: "+shelf.isFull());
        shelf.removeBook(books[0]);
        shelf.removeBook(books[4]);
        System.out.println(shelf);
    }
}

========================================================================================

Add a comment
Know the answer?
Add Answer to:
cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name

    Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight objects.

  • Design and implement a Java data structure class named BookShelf which has an array to store...

    Design and implement a Java data structure class named BookShelf which has an array to store books and the size data member. The class should have suitable constructors, get/set methods, and the toString method, as well as methods for people to add a book, remove a book, and search for a book. Design and implement a Java class named Book with two data members: title and price. Test the two classes by creating a bookshelf object and five book objects....

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • Write an entire class called Movie that stores an instance variable named title for the title...

    Write an entire class called Movie that stores an instance variable named title for the title of the movie as a string. Write the corresponding getter and setter methods (getTitle, setTitle), and a constructor that takes one parameter (string for the title). Include both the class definition and method definitions! Be sure to use appropriate visibility modifiers. Assume all includes and using namespace std are already in the file. Do not try to make header files.

  • Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains...

    Chapter 5 Assignment Read directions: In JAVA design and implement a class called Dog that contains instance data that represents the dog's name and dog's age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in "person years" (seven times age of dog. Include a toString method that returns a one-time description of the dog (example below)....

  • Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that...

    Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that simulates drawing a gift at random out of a box. The class is a generic class with a parameter of type T that represents a gift and where T can be a type of any class. The class must include the following An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox. A default constructors that creates the box....

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT