4. Inheritance. Abstract Classes and Interfaces (a) You are asked to write a ser
ID: 3920923 • Letter: 4
Question
4. Inheritance. Abstract Classes and Interfaces (a) You are asked to write a series of classes for storing document information for an i. Define an abstract Document class to store the following attributes: filename, ii. Define a WordDocument class that builds upon your previous class but in- office suite of applications. author, date created and date last modified. cludes the following additional String attributes: title, subtitle, body, header iii. Define a SpreadsheetDocument class that extends the Document class 13] and footer and stores a title and a two dimensional array of Cell objects. The Cell class is defined like so: public class Cell Object value; public Cell (Object value) this.value value; public Object getValue) return value; public void setValue (Object value) this.value value; (b) Two interfaces are required in the office suite. One indicates that a document can be printed, the other indicates a document can be saved. i. Define an interface called Printable that would ensure that any class that implements the Printable interface contains a print method. You may 12] ii. Define an interface called Saveable that would ensure that any class that im plements the Saveable interface contains a save method. The save method will take a single String parameter that holds the filename and returns a single 12] (c) With reference to your answers to (a) and (b), demonstrate how you would build a document class that can be saved and printed. You do not need to provide complete assume the print method has no parameters and no return value value indicating whether the save was successful. implementations of any method.Explanation / Answer
Document.java
***********************
package sample;
public abstract class Document {
private String fileName, author,createdData, lastModifiedDate;
public Document(String fileName, String author, String createdData, String lastModifiedDate) {
super();
this.fileName = fileName;
this.author = author;
this.createdData = createdData;
this.lastModifiedDate = lastModifiedDate;
}
/**
* @return the fileName
*/
public String getFileName() {
return fileName;
}
/**
* @param fileName the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the createdData
*/
public String getCreatedData() {
return createdData;
}
/**
* @param createdData the createdData to set
*/
public void setCreatedData(String createdData) {
this.createdData = createdData;
}
/**
* @return the lastModifiedDate
*/
public String getLastModifiedDate() {
return lastModifiedDate;
}
/**
* @param lastModifiedDate the lastModifiedDate to set
*/
public void setLastModifiedDate(String lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
WordDocument.java
***********************
package sample;
public class WordDocument extends Document {
private String title, subtitle, body, header, footer;
public WordDocument(String fileName, String author, String createdData, String lastModifiedDate, String title,
String subtitle, String body, String header, String footer) {
super(fileName, author, createdData, lastModifiedDate);
this.title = title;
this.subtitle = subtitle;
this.body = body;
this.header = header;
this.footer = footer;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the subtitle
*/
public String getSubtitle() {
return subtitle;
}
/**
* @param subtitle the subtitle to set
*/
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
/**
* @return the body
*/
public String getBody() {
return body;
}
/**
* @param body the body to set
*/
public void setBody(String body) {
this.body = body;
}
/**
* @return the header
*/
public String getHeader() {
return header;
}
/**
* @param header the header to set
*/
public void setHeader(String header) {
this.header = header;
}
/**
* @return the footer
*/
public String getFooter() {
return footer;
}
/**
* @param footer the footer to set
*/
public void setFooter(String footer) {
this.footer = footer;
}
}
SpreadsheetDocument.java
***********************
package sample;
public class SpreadsheetDocument extends Document {
private String title;
private Cell[][] cells;
public SpreadsheetDocument(String fileName, String author, String createdData, String lastModifiedDate,
String title, int row, int col) {
super(fileName, author, createdData, lastModifiedDate);
this.title = title;
this.cells = new Cell[row][col];
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the cells
*/
public Cell[][] getCells() {
return cells;
}
/**
* @param cells the cells to set
*/
public void setCells(Cell[][] cells) {
this.cells = cells;
}
public void setCell(int i, int j, Object value) {
cell[i][j].setValue(value);
}
}
Printable.java
***********************
package sample;
public interface Printable {
public void print();
}
Saveable.java
***********************
package sample;
public interface Saveable {
public boolean save(String fileName);
}
SampleDocument.java
***********************
package sample;
// This class represents a document which can be both saved and printed
public class SampleDocument extends Document implements Printable, Saveable {
private String title;
public SampleDocument(String fileName, String author, String createdData, String lastModifiedDate, String title) {
super(fileName, author, createdData, lastModifiedDate);
this.title = title;
}
@Override
public boolean save(String fileName) {
// TODO Auto-generated method stub
return false;
}
@Override
public void print() {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.