Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help( Extends java.lang.Object instead of ArrayList DataSetBook) Can you

ID: 3868886 • Letter: P

Question

Please help(Extends java.lang.Object instead of ArrayList DataSetBook) Can you please help me the JAVA program? DataSetBook.java must have NO instance variables; it should use inheritance instead. DataSetBook class should be as small as possible, but still meet the class specification. Please use DataSetBook extends java.lang.Object. Just need methods minBook, maxBook and toString. The code should something like public Book minBook(){ if (super.size() == 0{return null;} //so on Also please write a Tester.java for the program. --------------here is Book.java code, please don't change anything from here------------------- public class Book { private String author; private String title; private int pages; public Book(String auth, String titl, int pag) { author = auth; title = titl; pages = pag; } public int getPages() { return pages; } @Override public String toString() { return "Book [author=" + author + ", title=" + title + ", pages=" + pages + "] "; } // this is a really poor way to compare Book objects, but it will work for us /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @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 (author == null) { if (other.author != null) return false; } else if (!author.equals(other.author)) return false; if (pages != other.pages) return false; if (title == null) { if (other.title != null) return false; } else if (!title.equals(other.title)) return false; return true; } } ------------------------------------------------------------------------------------------------------------------------------ Public class DataSetBook extends java.lang.Object Constructor Detail DataSetBook.java public DataSetBook() Default constructor Method Detail add public void add(Book objToAdd) Add a Book to the store Parameters: objToAdd - Book to add size public int size() The number of Books currently in the store Returns: number of Book objects getMin public Book getMin() Determine the Book with the fewest pages Returns: null if the store is empty. The book with the fewest pages otherwise. If more than one book has the fewest number of pages, the first one is returned. getMax public Book getMax() Determine the Book with the most pages Returns: null if the store is empty. The book with the most pages otherwise. If more than one book has the most number of pages, the first one is returned. toString public java.lang.String toString() A String representation of the store. Overrides: toString in class java.lang.Object Returns: A String containing the number of books in the store, the minimum book, the largest book, and the contents of the entire store.

Explanation / Answer

Writing this class was tough. Instead of using instance variables, we used static variables to solve the crisis. I hope you like the soluton.


public class DataSetBook extends java.lang.Object {

    private static int size = 0;
    private static Book arr[] = null;
  
    public DataSetBook() {
        if (arr == null)
            arr = new Book[5];
    }
  
  
     void add(Book objToAdd) {
         if (arr.length == size) {
             Book[] newArr = new Book[size * 2];
             System.arraycopy(arr, 0, newArr, 0, size);
             arr = newArr;
         }
         arr[size++] = objToAdd;
     }
   
     public int size() {
         return size;
     }
   
     public Book getMin() {
         if (size == 0)
             return null;
         Book min = arr[0];
         for (int i = 1; i < size; i++)
             if (arr[i].getPages() < min.getPages())
                 min = arr[i];
         return min;
     }
   
     public Book getMax() {
         if (size == 0)
             return null;
         Book max = arr[0];
         for (int i = 1; i < size; i++)
             if (arr[i].getPages() > max.getPages())
                 max = arr[i];
         return max;
     }

    @Override
    public String toString() {
        String resultString = "Number of Books: " + size + " " +
                                "Minimum book: " + getMin() + " " +
                                "Maximum book: " + getMax() + " " +
                                "Books:";
      
        for (int i = 0; i < size; i++) {
            resultString += " " + arr[i];
        }
        return resultString;
    }
}