Question

Create a class class called library which will contain the list of books user should be...

Create a class class called library which will contain the list of books user should be able to add,delete view list of books in a particular library.

Override hashcode and equal methods, and use hashset to store the data

library class: libcode,libname

book class: bookcode,bookname and price

Please answer with the directions above in java. Thanks

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

Code For Above Problem:

Book Class Code:


public class Book {
        // Attributes of Book
        private String bookcode;
        private String bookname;
        private double price;

        // Constructor to create Book Object
        public Book(String bookcode, String bookname, double price) {
                this.bookcode = bookcode;
                this.bookname = bookname;
                this.price = price;
        }

        // accessor methods
        public String getBookcode() {
                return bookcode;
        }

        public String getBookname() {
                return bookname;
        }

        public double getPrice() {
                return price;
        }

        // Mutator methods
        public void setBookcode(String bookcode) {
                this.bookcode = bookcode;
        }

        public void setBookname(String bookname) {
                this.bookname = bookname;
        }

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

        // Overrided hashCode method
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((bookcode == null) ? 0 : bookcode.hashCode());
                result = prime * result + ((bookname == null) ? 0 : bookname.hashCode());
                long temp;
                temp = Double.doubleToLongBits(price);
                result = prime * result + (int) (temp ^ (temp >>> 32));
                return result;
        }

        // Overrided equals method
        public boolean equals(Object obj) {
                // if both references are same
                if (this == obj)
                        return true;
                // if provided obj is null
                if (obj == null)
                        return false;
                // if class name is different
                if (getClass() != obj.getClass())
                        return false;
                // type cast obj to Book
                Book other = (Book) obj;

                // if this object bookcode is null and other bookcode is not null
                if (bookcode == null) {
                        if (other.bookcode != null)
                                return false;
                }
                // if this object bookcode is not equals to other book bookcode
                else if (!bookcode.equals(other.bookcode))
                        return false;
                // if this object bookname is null and other bookname is not null
                if (bookname == null) {
                        if (other.bookname != null)
                                return false;
                }
                // if this object bookname is not equals to other book bookname
                else if (!bookname.equals(other.bookname))
                        return false;
                // if prices are not same
                if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
                        return false;
                // if above all conditions are not satisfied both objects are same return true
                return true;
        }

        // toString method to return String representation of Book Object
        public String toString() {
                return "Book [bookcode=" + bookcode + ", bookname=" + bookname + ", price=" + price + "]";
        }
}

Library Class Code:


import java.util.HashSet;

public class Library {
        // attributes of Library
        String libcode;
        String libname;
        HashSet<Book> books;

        // Constructor to create Library Object
        public Library(String libcode, String libname) {
                this.libcode = libcode;
                this.libname = libname;
                this.books = new HashSet<Book>();
        }

        /*
         * Method to add book to book list return true if successfully added and return
         * false if already present
         */
        public boolean add(Book b) {
                return books.add(b);
        }

        /*
         * Method to delete book from book list return true if successfully delete book
         * from list and return false it book not present in list
         */
        public boolean delete(Book b) {
                return books.remove(b);
        }

        // Method to show the books in list
        public void showList() {
                if (books.size() == 0) {
                        System.out.println("No Books in list");
                } else {
                        System.out.println("Books List: ");
                        for (Book b : books) {
                                System.out.println(b);
                        }
                }
        }
}

Images Of Code:

Book Class Code Images:

4 13 } } } 3 public class Book { // Attributes of Book 5 private String bookcode; 6 private String bookname; 7 private double

48 49 50 51 52 53 Long tempi temp = Double doubleToLongBits(price); result = prime * result + (int) (temp (temp >>> 32)); ret

Library Class Code Image:

2 6 7 9 11 } ܢܟ 3 import java.util.HashSet; 4 5 public class Library { // attributes of Library String libcode; 8 String Libn

Add a comment
Know the answer?
Add Answer to:
Create a class class called library which will contain the list of books user should be...
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
  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • In this assignment you will implement software for your local library. The user of the software...

    In this assignment you will implement software for your local library. The user of the software will be the librarian, who uses your program to issue library cards, check books out to patrons, check books back in, send out overdue notices, and open and close the library. class Calendar We need to deal with the passage of time, so we need to keep track of Java. Declare this variable as private int date within the class itself, not within any...

  • List of Candles Create a class called CandleNode which has fields for the data (a Candle)...

    List of Candles Create a class called CandleNode which has fields for the data (a Candle) and next (CandleNode) instance variables. Include a one-argument constructor which takes a Candle as a parameter. (For hints, see the PowerPoint on "Static vs. Dynamic Structures”.) public CandleNode (Candle c) { . . } The instance variables should have protected access. There will not be any get and set methods for the two instance variables. Create an abstract linked list class called CandleList. This...

  • Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String),...

    Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String), numberOfStudents (int) and courseLecturer (String). ○ A constructor that constructs a Course object with the specified courseName, numberOfStudents and courseLecturer. ○ The relevant get and set methods for the data fields. ○ A toString() method that formats that returns a string that represents a course object in the following format: (courseName, courseLecturer, numberOfStudents) ● Create a new ArrayList called courses1, add 5 courses to...

  • You are going to model a Book in a library. The Book class should contain a...

    You are going to model a Book in a library. The Book class should contain a title and an author (exactly 1 author is OK). Also, a book can be checked out and returned by a student. A Book object should be able to identify the student who currently holds the book. 1.Design the public interface (public constructor and public methods) for a class Book. You can use String to represent title, author, and student. 2. Implement the class Book...

  • Java coding. Create a class called Printer and a class called Job that will simulate a...

    Java coding. Create a class called Printer and a class called Job that will simulate a computer printer. The printer should store a queue of Job objects until they are printed. Each Job should consist of a title and the number of pages the job contains. The printer should also store the number of jobs it can print per minute. The class should include methods to add jobs and print jobs. The method that prints a job should return the...

  • Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able...

    Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects. 2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

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