JAVA IS THE LANGUAGE USED. THE BOOK USED IS HOW TO PROGRAM EARLY OBJECTS 10TH ED
ID: 3601309 • Letter: J
Question
JAVA IS THE LANGUAGE USED. THE BOOK USED IS HOW TO PROGRAM EARLY OBJECTS 10TH ED BY DEITEL DEITEL Consider the management and maintenance of a "library database" in a certain school. It holds the information of material resources in use for students in that school. This information is recorded based on the object-oriented design. The skeleton of the database structure is shown using the Unified Modeling Language (UML) diagramming method illustrated below. UML's standard notion is still being finalized for global use, and so annotations such as (has-a) to express object composition and (implements) to express inheritance are added to the diagram:
The above structure shows object variables and constructors only. Plus sign (+) indicates public visibility whereas minus sign (-) sets out private scope. The Database may use ArrayList instead of Array to hold the collection of Item's references. Add an appropriate set of overloading/overriding methods to support the object hierarchy assumed in this context. Implement the hierarchy, populate Database with at least two items per non-abstract class, and show the "unsorted" and "sorted" list of the set of those instances. The minimal requirement of this assignment is to realize the database sort based on the lexicographical order of id as well as any combination of data variables. For instance, database records are shown based on the lexicographical order of "title," followed by"addedOn," and followed by "director." Requirements Realize your implementation that satisfies the following minimal requirements: 2 pt: properly exhibits right logic, i.e., readable and compilable coding 2 pt: properly realizes object hierarchy with added methods 2 pt: properly realizes object listing 2 pt: properly realizes sorting of database items based on their id values 2 pt: properly realizes sorting of database with any fields designated sorting order of items in database extra credit: implementing with ArrayList instead of Array. See Comparator example here(http://www.java2s.com/Code/Java/Apache-Common/ComparatorExampleForUserDefinedClass.htm) to realize all the requirements defined above. As a hint of your implementation, the following coding of main function should list the output of what this assignment mandates: public static void main(String args[]) { ComparatorChain chain = new ComparatorChain(); Database library = new Database(); Calendar cal = Calendar.getInstance(); // adding database entries cal.set(1890, Calendar.AUGUST, 10); Date date = (Date) cal.getTime(); library.addItem(new Textbook("TB15", "TextX", date, "John Doe")); cal.set(1954, Calendar.JANUARY, 18); date = (Date) cal.getTime() ; library.addItem(new Video("V09", "VideoB", date, 70000, "J. Smith")); cal.set(2000, Calendar.FEBRUARY, 29); date = (Date) cal.getTime() ; library.addItem(new Textbook("TB01", "TextY", date, "John Doe")); cal.set(2000, Calendar.FEBRUARY, 29); date = (Date) cal.getTime() ; library.addItem(new CD("CD07", "CD1", date, 1000, "B.D.")); cal.set(1990, Calendar.APRIL, 30); date = (Date) cal.getTime() ; library.addItem(new CD("CD10", "CD1", date, 800, "X.Y.")); cal.set(2000, Calendar.FEBRUARY, 29); date = (Date) cal.getTime(); library.addItem(new CD("CD05", "CD1", date, 1000, "B.C.")); cal.set(1890, Calendar.JULY, 2); date = (Date) cal.getTime(); library.addItem(new Video("V12", "VideoA", date, 7000, "Joe Smith")); // print unsorted database System.out.println("----- DATABASE BEFORE SORTING: ----- "); library.list(); // sort and print sorted database (by id) Collections.sort(library.item); System.out.println("----- DATABASE AFTER SORTING BY ID (default): ----- "); library.list(); // sort by other fields System.out.println("----- DATABASE AFTER SORTING BY OTHER FIELDS: -----"); System.out.println("------------ (title, addedOn, director) ----------- "); chain.addComparator(new sortByTitle()); chain.addComparator(new sortByAddedOn()); chain.addComparator(new sortByDirector()); Collections.sort(library.item, chain); library.list(); } Show transcribed image text «interface» Comparable + CompareTo0 (implements) «abstract» Item Database - item: Item + Database(0) + addltem(item: Item) + list) - id: String - title: String - addedOn: Date + Item(id: String, title: String, addedOn: Date) (has-a) (extends) «abstract» MultiMedialtem Textbook - plavingTime: integer + MultiMedialtem(id: String, title: String addedOn: Date, playingTime: Integer) - author: Strin +Textbook(id: String, title: String, addedOn: Date, author: String) (extends) CD Video - artist: Strin +CD(id: String, title: String, addedOn: Date, playingTime: integer, artist: String) - director: Strin + Video(id: String, title: String addedOn: Date, playingTime: integer, director: String)
«interface» Comparable + CompareTo0 (implements) «abstract» Item Database - item: Item + Database(0) + addltem(item: Item) + list) - id: String - title: String - addedOn: Date + Item(id: String, title: String, addedOn: Date) (has-a) (extends) «abstract» MultiMedialtem Textbook - plavingTime: integer + MultiMedialtem(id: String, title: String addedOn: Date, playingTime: Integer) - author: Strin +Textbook(id: String, title: String, addedOn: Date, author: String) (extends) CD Video - artist: Strin +CD(id: String, title: String, addedOn: Date, playingTime: integer, artist: String) - director: Strin + Video(id: String, title: String addedOn: Date, playingTime: integer, director: String)Explanation / Answer
Please find the code below:
Item.java
import java.util.Date;
public abstract class Item implements Comparable<Item>{
private String id;
private String title;
private Date addedOn;
public Item(String id, String title, Date addedOn)
{
this.id = id;
this.title = title;
this.addedOn = addedOn;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getAddedOn() {
return addedOn;
}
public void setAddedOn(Date addedOn) {
this.addedOn = addedOn;
}
@Override
public int compareTo(Item o) {
return id.compareTo(o.id);
}
public String toString()
{
return "ID: " + id + ", Title: " + title + ", Added On: " + addedOn;
}
}
Database.java
import java.util.ArrayList;
public class Database {
ArrayList<Item> item;
public Database()
{
item = new ArrayList<Item>();
}
public void addItem(Item it)
{
item.add(it);
}
public void list()
{
for(Item i : item)
System.out.println(i);
}
}
TextBook.java
import java.util.Date;
public class TextBook extends Item {
private String author;
public TextBook(String id, String title, Date addedOn, String author) {
super(id, title, addedOn);
this.author = author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String toString()
{
return "TextBook: [" + super.toString() + ", Author: " + author + "]";
}
}
MultiMediaItem.java
import java.util.Date;
public abstract class MultiMediaItem extends Item {
private int playingTime;
public MultiMediaItem(String id, String title, Date addedOn, int playingTime) {
super(id, title, addedOn);
this.playingTime = playingTime;
}
public int getPlayingTime() {
return playingTime;
}
public void setPlayingTime(int playingTime) {
this.playingTime = playingTime;
}
public String toString()
{
return super.toString() + ", Playing Time: " + playingTime;
}
}
CD.java
import java.util.Date;
public class CD extends MultiMediaItem {
private String artist;
public CD(String id, String title, Date addedOn, int playingTime, String artist) {
super(id, title, addedOn, playingTime);
this.artist = artist;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String toString()
{
return "CD: [ "+ super.toString() + ", Artist: " + artist + "]";
}
}
Video.java
import java.util.Date;
public class Video extends MultiMediaItem {
private String director;
public Video(String id, String title, Date addedOn, int playingTime, String director) {
super(id, title, addedOn, playingTime);
this.director = director;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String toString()
{
return "Video: [" + super.toString() + ", Director: " + director + "]";
}
}
// Please do rate if you find this helpful thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.