Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The attached zip file contains aDataSetBookclass that holds Book objects.RewriteDataSetBookso that it uses inheritance, andhas no instance variables.DataSetBookshould leverage its superclass to the fullest possible

The attached zip file contains aDataSetBookclass that holds Book objects.RewriteDataSetBookso that it uses inheritance, andhas no instance variables.DataSetBookshould leverage its superclass to the fullest possible extent while maintaining its existing behavior.Write the testerclass with only a main method to test DataSetBook.

Extract the doc folder (browsers don't work inside a zip file) and open doc\index.htmlfor more documentation of DataSetBook, and more explanation of what changing DataSetBookto use inheritance means.

Grading Elements:

  • DataSetBookis a public class thatuses inheritance to implement its behavior
  • DataSetBookhas no instance variables
  • Book is unchanged
  • DataSetBookleverages its superclass as much as possible
  • A public tester class with a main method demonstrates the function of DataSetBook

public class DataSetBookTester {

public static void main(String[] args ) {

Book bl = new Book ("McGregor", "Java Mistakes I have Made", 1000);

System.out.println(bl);

Book b2 = new Book ("Jones", "Everything Java", 50000);

System.out.println(b2);

Book b3 = new Book("Gosling", "Java Shortcuts", 5);

System.out.println(b3);

DataSetBook library = new DataSetBook();

library.add(bl);

library.add(b2);

library.add(b3);

System.out.println("Books in Library : " +library.size());

System.out.println("Book with Most Pages : " +library.getMax());

System.out.println("Book with Least Pages : " +library.getMin());

}

}

import java.util.ArrayList;

/**

* A simple store for Book objects.

*

* Note that DataSetBook aggregates Books.

* Informally, a DataSetBook

* can contain multiple Book objects.

* This is DataSetBook's role in life--to contain multiple Books.

* The given implementation of DataSetBook encapsulates an instance variable of

* ArrayList to assist in this mission.

* This implementation of DataSetBook "has-a(n)" ArrayList.

* But this isn't the only way to implement DataSetBook.

*

*

* In CSC-151, we studied a class StringSet.

* StringSet's role in life is to store and analyze Strings.

* In the Inheritance lesson in CSC-151, we

* refactored (i.e., reimplemented without

* changing its external behavior) StringSet.

* The earlier StringSet "has-an" ArrayList

* to keep its String objects.

* We changed StringSet to inherit from ArrayList.

* So StringSet no longer "has-an" ArrayList; it became

* StringSet "is-an" ArrayList.

*

*

* Inheritance should be one of the tools in your box for

* class design and implementation.

*

*

* @author student

*

*/

public class DataSetBook {

private ArrayList < Book > data;

/**

* Default constructor

*/

public DataSetBook() {

data = new ArrayList < > ();

}

/**

* Add a Book to the store

*

* @param objToAdd is the Book we're adding to the store

*

* @return true if the element was added to the collection, false otherwise

*/

public boolean add(Book objToAdd) {

return data.add(objToAdd);

}

/**

* The number of Books currently in the store

*

* @return number of Book objects

*/

public int size() {

return data.size();

}

/**

* Determine the Book with the fewest pages

*

* @return 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.

*/

public Book getMin() {

if (data.isEmpty()) {

return null;

}

Book mEle = data.get(0);

for (int i = 1; i < data.size(); i++) {

if (mEle.getPages() > (data.get(i).getPages())) {

mEle = data.get(i);

}

}

return mEle;

}

/**

* Determine the Book with the most pages

*

* @return 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.

*/

public Book getMax() {

if (data.isEmpty()) {

return null;

}

Book mEle = data.get(0);

for (int i = 1; i < data.size(); i++) {

if (mEle.getPages() < (data.get(i).getPages())) {

mEle = data.get(i);

}

}

return mEle;

}

/**

* A String representation of the store.

*

* @return A String containing the number of books in the store,

* the minimum book, the largest book, and

* the contents of the entire store.

*/

@Override

public String toString() {

return "DataSetBook [ size()=" + size() + " getMin()=" + getMin() + " getMax()=" + getMax() +

" Books= " + data.toString() + "]";

}

}

// Book.java

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;

}

}

/*

*Hint Sample below - NOT a part of the question

*/

//People.java

package Lab_Hint;

import java.util.ArrayList;

public class People extends ArrayList<Person> {

public boolean add(Person p) {

return super.add(p);

}

public Person getMax() {

if (isEmpty()) return null;

Person max = get(0);

for (Person p: this) {

if (max.getAge() < p.getAge()) max = p;

}

return max;

}

}

// Person.java

package Lab_Hint;

public class Person {

private String name;

private int age;

public Person() {

}

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + "]";

}

}

// Student.java

package Lab_Hint;

public class Student extends Person {

private int studentId;

public Student() {

}

public Student(String name, int age,int studentId) {

super(name, age);

this.studentId = studentId;

}

public int getStudentId() {

return studentId;

}

public void setStudentId(int studentId) {

this.studentId = studentId;

}

@Override

public String toString() {

return "Student [studentId=" + studentId + ", toString()=" + super.toString() + "]";

}

}

// HintTester.java

package Lab_Hint;

public class HintTester {

public static void main(String[] args) {

Person p1 = new Person("George", 48);

Person p2 = new Person("Linda", 42);

Student s1 = new Student("Bill", 22, 112133);

Student s2 = new Student("Bill", 46, 112133);

People people = new People();

people.add(p1);

people.add(p2);

people.add(s1);

people.add(s2);

System.out.println(people.getMax());

}

}.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions