In: Computer Science
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 several sample objects of type Email and File in the main method. Test your objects by passing them to the following subroutine 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:
package cheggseptember;
public class Document {
private String text;
public Document(){
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String toString(){
return "Text:\n"+text;
}
}
File:
package cheggseptember;
public class File extends Document{
private String pathName;
public String getPathName() {
return pathName;
}
public void setPathName(String pathName) {
this.pathName = pathName;
}
public String toString(){
return "Path Name:"+this.pathName+"\n"+super.toString();
}
}
Email:
package cheggseptember;
public class Email extends Document{
private String sender;
private String recipient;
private String title;
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String toString(){
return "From:"+this.sender+"\nTo: "+this.recipient+"\nTitle :"+this.title+"\n"+super.toString();
}
}
DocumentTester:
package cheggseptember;
public class DocumentTest {
public static void main(String[] args) {
Email email=new Email();
email.setRecipient("[email protected]");
email.setSender("[email protected]");
email.setTitle("Happy birthday");
email.setText("Dear john ,\n"
+ "How are you .I hope everything is good.Happy birthday");
System.out.println(email.toString());
File file=new File();
System.out.println("\nFile\n");
file.setPathName("c://Desktop");
file.setText("This has periodic elements data");
System.out.println(file.toString());
System.out.println("\nTesting contains keyword functionality\n");
System.out.println(DocumentTest.ContainsKeyword(email,"birthday"));//contains birthday
System.out.println(DocumentTest.ContainsKeyword(email,"birthday1"));//does not contain birthday1
System.out.println(DocumentTest.ContainsKeyword(file,"c:"));//contains c:
System.out.println(DocumentTest.ContainsKeyword(file,"birthday"));//does not contain birthday
}
public static boolean ContainsKeyword(Document docObject, String keyword) {
if (docObject.toString().indexOf(keyword,0) >= 0) {
return true ;
}
return false;
}
}
Expected output: