Write a class named Document to represent a document node in a print queue. it should contain a method name that returns the subject of the document and an abstract method called type that returns the document type . from document class derive two concrete classes named word document and pdf document. 2. Write another class named Print Queue that is implemented as a linked list of Document objects. Print Queue should have a method named Push for adding a document to the end of the list, a method named pop for removing the first document from the list , and a method named DisplayContents for listing all of the documents in the Print Queue, reporting both the Name and Type of each element Print Queue should not use any standard library classes it should be own implementation of linked list
Anonymous
/* Class: Document */ public abstract class Document { protected String name; protected String type; public Document() { } public Document(String name) { this.name = name; } public String name() { return this.name; } public abstract String type(); } --------------------------------------------------------------------------------- /* Class: PdfDocument */ public class PdfDocument extends Document { public PdfDocument(String name) { super(name); } @Override public String type() { return "pdf"; } } --------------------------------------------------------------------------------- /* Class: WordDocument */ public class WordDocument extends Document { public WordDocument(String name) { super(name); } @Override public String type() { return "word"; } } --------------------------------------------------------------------------------- /* Class: Node */ public class Node { private E data; private Node next; /** * @return the data */ public E getData() { return data; } /** * @param data the data to set */ public void setData(E data) { this.data = data; } /** * @return the next */ public Node getNext() { return next; } /** * @param next the next to set */ public void setNext(Node next) { this.next = next; } } --------------------------------------------------------------------------------- /* Class: PrintQueue */ public class PrintQueue { private Node head; public PrintQueue() { } public PrintQueue(E doc) { Node nodeToAdd = new Node(); nodeToAdd.setData(doc); nodeToAdd.setNext(null); if (this.head == null) { this.head = nodeToAdd; } Node temp = this.head; while (temp.getNext() != null) { temp = temp.getNext(); } temp.setNext(nodeToAdd); } public E push(E doc) { Node nodeToAdd = new Node(); nodeToAdd.setData(doc); nodeToAdd.setNext(null); if (this.head == null) { this.head = nodeToAdd; } else { Node temp = this.head; while (temp.getNext() != null) { temp = temp.getNext(); } temp.setNext(nodeToAdd); } return doc; } public E pop() { E docToReturn = null; if (this.head == null) { return null; } else { docToReturn = this.head.getData(); this.head = this.head.getNext(); } return docToReturn; } public void displayContents() { if (this.head == null) { System.out.println("No document in the queue"); } else { Node temp = this.head; while (temp.getNext() != null) { System.out.println("Name: " + temp.getData().name() + ", Type: " + temp.getData().type()); temp = temp.getNext(); } System.out.println("Name: " + temp.getData().name() + ", Type: " + temp.getData().type()); } } }
Check out your Company Bowl for anonymous work chats.