Question

In: Computer Science

Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.

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. public static boolean ContainsKeyword(Document docObject, String keyword) { if (docObject.toString().indexOf(keyword,0) >= 0) return true ; return false ;

Solutions

Expert Solution

Document.java:

public class Document {

private String text;

public Document(String text) {

this.text = text;

}

public String toString() {

return "text=" + text;

}

public String getText() {

return text;

}

public void setText(String text) {

this.text = text;

}

}

Email.java:

public class Email extends Document {

private String sender;

private String recipient;

private String title;

public Email(String text, String sender, String recipient, String title) {

super(text);

this.sender = sender;

this.recipient = recipient;

this.title = 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;

}

@Override

public String toString() {

return "sender=" + sender + ", recipient=" + recipient + ", title=" + title + ", getText()=" + getText();

}

}

File.java:

public class File extends Document {

private String pathname;

public File(String text, String pathname) {

super(text);

this.pathname = pathname;

}

public String getPathname() {

return pathname;

}

public void setPathname(String pathname) {

this.pathname = pathname;

}

@Override

public String toString() {

return "pathname=" + pathname + ", getText()=" + getText();

}

}

DocumentTest.java:

public class DocumentTest {

public static void main(String[] args) {

Document doc1=new Email("Hi, How are you?","Alexander","Anderea","Greetings");

Document doc2=new File("abcdefghijklmnopqrstuvwxyz","D:/Documents/alphabet.txt");

System.out.println("My Email contains the keyword 'are' :" + containsKeyword(doc1,"are") );

System.out.println("My Email contains the keyword 'the' :" + containsKeyword(doc1,"the") );

System.out.println("My File contains the keyword 'abc' :" + containsKeyword(doc2,"abc") );

System.out.println("My Email contains the keyword 'cba' :" + containsKeyword(doc2,"cba") );

}

public static boolean containsKeyword(Document docObject, String keyword) {

if (docObject.toString().indexOf(keyword,0) >= 0)

return true ;

return false ;

}

}

Output of DocumentTest Class:


Related Solutions

Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
IN JavaDefine 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...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document.
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...
Define a class named Document that contains a member variable of type String named text that stores any textual content for the document.
Define a class named Document that contains a member 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 member variables for the sender, recipient, and title of an email message. Implement appropriate accessor and mutator methods. The body of the email message should...
Define a class named Document that contains an instance variable of type String named text that...
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...
Draw UML diagram Define a class named Document that contains a member variable of type string...
Draw UML diagram Define a class named Document that contains a member variable of type string named text that stores any textual content for the document. Create a function named getText that returns the text field and a way to set this value. Next, define a class for Email that is derived from Document and that includes member variables for the sender , recipient , and title of an e-mail message. Implement appropriate accessor and mutator functions. The body of...
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...
Java Implement a class MyInteger that contains: • An int data field/instance variable named value that stores the int value represented by this object. • A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice? • A constructor that creates a MyInteger object for a specified int value. • A getter method, valueOf(), that returns value. • A setter method,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT