In: Computer Science
Code in Java
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value.
Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.
Similarly, define a class for File that is derived from Document and includes an instance variable for the pathname. The textual contents of the file should be stored in the inherited variable text. Redefine the toString method to concatenate all text fields.
Finally, define a class DocumentTest that creates two objects of each type Email and File in the main method. Test your objects by passing them to the following function that returns true if the object contains the specified keyword in the text property. (5 pts)
public static boolean ContainsKeyword(Document docObject, String keyword){
if (docObject.toString().indexOf(keyword,0) >= 0) return true ;
return false;
}
/** * Document.java */ public class Document { private String text; public Document(String text) { this.text = text; } public void setText(String text) { this.text = text; } @Override public String toString() { return text; } }
/** * Email.java */ public class Email extends Document{ //private variables private String sender, recipent,title; public Email(String text, String sender, String recipent, String title) { super(text); this.sender = sender; this.recipent = recipent; this.title = title; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getRecipent() { return recipent; } public void setRecipent(String recipent) { this.recipent = recipent; } public String getTitle() { return title; } @Override public void setText(String text) { super.setText(text); } public void setTitle(String title) { this.title = title; } //pverrided parent tostring and adding additional info @Override public String toString() { return String.format("Title : %s\nSender : %s\nRecipent : %s\nText : %s",this.title, this.sender,this.recipent,super.toString()); } }
/** * File.java */ public class File extends Document{ private String pathname; public File(String pathname) { super(""); this.pathname = pathname; } @Override public void setText(String text) { super.setText(text); } @Override public String toString() { return "Pathname : "+pathname+"\nText : "+super.toString(); } }
/** * DocumentTest.java */ public class DocumentTest { public static void main(String[] args) { //created two objects Email mail = new Email("This is the body of mail","xyz at ddd dot com","abc at gg dot in","Email Title"); File file = new File("/home/"); System.out.println("=========================================================="); file.setText("file text data"); System.out.println(mail); System.out.println("***************************"); System.out.println(file); System.out.println("=========================================================="); System.out.println("Does mail have text [body] ? "+ContainsKeyword(mail,"body")); System.out.println("Does file have text [programming] ? "+ContainsKeyword(file,"programming")); } //defined the given method public static boolean ContainsKeyword(Document docObject, String keyword){ if(docObject.toString().indexOf(keyword,0)>=0) return true; return false; } }
//Output