Question

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. Add the books to the bookshelf.

Display the contents of the bookshelf.

Now re-implement the Bookshelf class using a linkedlist and a Node class. Implement the above add, remove, and search methods again.

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

1. BookShelf program using array

//Book.java

public class Book {

      

       private String title;

       private double price;

      

       public Book()

       {

             title="";

             price=0;

       }

      

       public Book(String title, double price)

       {

             this.title = title;

             this.price = price;

       }

      

       public void setTitle(String title)

       {

             this.title = title;

       }

      

       public void setPrice(double price)

       {

             this.price = price;

       }

      

       public String getTitle()

       {

             return title;

       }

      

       public double getPrice()

       {

             return price;

       }

      

       public boolean equals(Book book)

       {

             // if both title and price of both books are same

             if((title.equalsIgnoreCase(book.title)) && (price == book.price))

                    return true;

             return false;

       }

      

       public String toString()

       {

             return title+" : "+price;

       }

}

//end of Book.java

//BookShelf.java : Java program to implement BookShelf using array

public class BookShelf {

      

       private Book books[];

       private int size;

      

       public BookShelf(int capacity)

       {

             books = new Book[capacity];

             size=0;

       }

      

       public int getSize()

       {

             return size;

       }

      

       public boolean addBook(Book book)

       {

             if(size < books.length)

             {

                    books[size] = book;

                    size++;

                    return true;

             }

             return false;

       }

      

       public boolean removeBook(Book book)

       {

             for(int i=0;i<size;i++)

             {

                    if(books[i].equals(book))

                    {

                           for(int j=i;j<size-1;j++)

                                 books[j] = books[j+1];

                           size--;

                           return true;

                    }

             }

            

             return false;

       }

      

       public boolean searchBook(Book book)

       {

             for(int i=0;i<size;i++)

             {

                    if(books[i].equals(book))

                    {

                           return true;

                    }

             }

            

             return false;

       }

      

       public String toString()

       {

             String booksStr = "";

             for(int i=0;i<size;i++)

             {

                    booksStr += books[i].toString()+"\n";

             }

            

             return booksStr;

       }

       public static void main(String args[])

       {

             BookShelf bookShelf = new BookShelf(10); // create a bookshelf object

             // create 5 books and add them to bookshelf

             Book book1 = new Book("Title1",20);

             bookShelf.addBook(book1);

             book1 = new Book("Title2",25);

             bookShelf.addBook(book1);

             book1 = new Book ("Title3",15);

             bookShelf.addBook(book1);

             book1 = new Book ("Title4",32.5);

             bookShelf.addBook(book1);

             book1 = new Book ("Title5",25);

             bookShelf.addBook(book1);

            

             System.out.println(bookShelf);

       }

}

//end of BookShelf.java

Output:

2. BookShelf program using linked list

//Book.java

public class Book {

      

       private String title;

       private double price;

      

       public Book()

       {

             title="";

             price=0;

       }

      

       public Book(String title, double price)

       {

             this.title = title;

             this.price = price;

       }

      

       public void setTitle(String title)

       {

             this.title = title;

       }

      

       public void setPrice(double price)

       {

             this.price = price;

       }

      

       public String getTitle()

       {

             return title;

       }

      

       public double getPrice()

       {

             return price;

       }

      

       public boolean equals(Book book)

       {

             // if both title and price of both books are same

             if((title.equalsIgnoreCase(book.title)) && (price == book.price))

                    return true;

             return false;

       }

      

       public String toString()

       {

             return title+" : "+price;

       }

}

//end of Book.java

//BookShelf.java : Java program to implement BookShelf program using linked list

public class BookShelf {

      

// class representing the node of the linkedlist

       private class Node{

             public Book book;

             public Node next;

       }

      

       private Node head;

       private int size;

      

       public BookShelf()

       {

             head = null;

             size=0;

       }

      

       // method to add the book at the head of the bookshelf

       public void addBook(Book book)

       {

             Node node = new Node();

             node.book = book;

             node.next = head;

             head = node;

            

             size++;

       }

      

       // method to remove a book

       public boolean removeBook(Book book)

       {

             if(head.book.equals(book))

             {

                    head = head.next;

                    return true;

             }else {

                    Node node = head;

                    Node prev = null;

                    while(node != null)

                    {

                           if(node.book.equals(book))

                           {

                                 prev.next = node.next;

                                 return true;

                           }

                           prev = node;

                           node = node.next;

                    }

                    return false;

             }

       }

      

       // method to search for a book

       public boolean searchBook(Book book)

       {

             Node node = head;

            

             while(node != null)

             {

                    if(node.book.equals(book))

                    {

                           return true;

                    }

                    node = node.next;

             }

            

             return false;

       }

      

       public String toString()

       {

             String booksStr = "";

             Node node = head;

             while(node != null)

             {

                    booksStr += node.book.toString()+"\n";

                    node = node.next;

             }

            

             return booksStr;

       }

      

       public static void main(String args[])

       {

             BookShelf bookShelf = new BookShelf(); // create a bookshelf object

             // create 5 books and add them to bookshelf

             Book book1 = new Book ("Title1",20);

             bookShelf.addBook(book1);

             book1 = new Book("Title2",25);

             bookShelf.addBook(book1);

             book1 = new Book ("Title3",15);

             bookShelf.addBook(book1);

             book1 = new Book ("Title4",32.5);

             bookShelf.addBook(book1);

             book1 = new Book ("Title5",25);

             bookShelf.addBook(book1);

            

             System.out.println(bookShelf);

       }

}

//end of BookShelf.java

Output:

Add a comment
Know the answer?
Add Answer to:
Design and implement a Java data structure class named BookShelf which has an array to store...
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
  • 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,...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • 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...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

    Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following:    • String name    • int year    • A linked list of college classes (college classes are represented using strings)    • An...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

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