Please reference the attached word document in the zip file for instructions.Use the file "lecture_03_code" as sample code

1 answer below »
Please reference the attached word document in the zip file for instructions.Use the file "lecture_03_code" as sample code
Answered Same DayFeb 08, 2021

Answer To: Please reference the attached word document in the zip file for instructions.Use the file...

Avnish answered on Feb 10 2021
156 Votes
Book.java
Book.java
package lecture_03_code;
public class Book implements Comparable{
    private String name;
    private int pages;
    public String getName(){
        return this.name;
    }
    public void setName(String n){
        this.name=n;
  }

    public Book(String c, int r) {
        this.name = c;
        this.pages = r;
    }
    public void setPages(int r) {
        this.pages = r;
    }
    public int getPages() {
        return this.pages;
    }
    @Override
    public String toString() {
        String output = this.name + "\t" + this.pages;
        return output;
    }
    @Override
    public int compareTo(Book otherBook){

        int output = 0;

        if (this.name.equalsIgnoreCase(otherBook.name)){
 
            if (this.pages > otherBook.pages)
                output = 1;

            else if(this.pages < otherBook.pages)
                output = -1;
            else 
                output = 0;

        }
        else if (this.name.toLowerCase().compareTo(otherBook.name.toLowerCase())<0){
            output = -1;
        }
        else{
            output = 1;
        }

        return output;

    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Book other = (Book) obj;
        if (this.getName() == null) {
            if (other.getName() != null)
                return false;
        } else if (!this.getName().equals(other.getName()))
            return false;
        if (this.getPages() != other.getPages())
            return false;
        return true;
    }
}
BookArrayBagDriver.java
BookArrayBagDriver.java
package lecture_03_code;
import java.util.Iterator;
public class BookArrayBagDriver {
    public static void main(String[] args) {


        BookSortedArrayBag bag = new BookSortedArrayBag();
        bag.add(new Book("physics",100));
        bag.add(new Book("physics",200));
        bag.add(new Book("ahysics",700));
        bag.add(new Book("bhysics",300));
        bag.add(new Book("bhysics",100));
        bag.add(new Book("zhysics",100));
        bag.add(new Book("physics",100));

        System.out.println(bag.toString());
        System.out.println("Current bag size is: "+bag.size());
        System.out.println("Index of book name physics page size 100 is: "+bag.indexOf(new Book("physics",100)));
        BookSortedArrayBag bagNew = bag;
        boolea...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here