Question

In: Computer Science

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 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 a member 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, create several sample objects of type Email and File in your main method. Test your objects by passing them to the following method ContainsKeyword() 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

NOTE:I have included comments too for your understanding.

Please run the class named: TestFile.java

CODE:

TestFile.java


public class TestFile extends Document{

   //path variable
   String path="C\file.txt";
   /*
   * contains method to check whether a keyword is there or not.
   */
   public static boolean ContainsKeyword(Document docObject, String keyword) {
       if (docObject.toString().indexOf(keyword,0) >= 0){
           return true;
       }else{
           return false;
       }
   }
/*
* this is main method to run the class
*/
   public static void main(){
       Email tstEml = new Email();
       //Document object
       Document dc=new Document();
       tstEml.setText("hi");
       //Set sender mail
       tstEml.setSender("[email protected]");
       //set recipient mail.
       tstEml.setRecipient("[email protected]");
       //set title.
       tstEml.setTitle("This is chegg assignment,I am doing for you.");
       //Invoking the method to check whether a keyword id there or not.
       ContainsKeyword(dc,"hi");
   }
}

Email.java


/*
* Email class
*/
public class Email extends Document {
//Email varaibles..
   String sender;
   String recipient;
   String text;
   String title;
//Doc object initialization..
   Document doc=new Document();
   //setters and getters..
   public String getSender() {
       return sender;
   }

   public String toString(){
       return text+doc.text;
   }
   public void setSender(String sender) {
       this.sender = sender;
   }
   public String getRecipient() {
       return recipient;
   }
   public void setRecipient(String recipient) {
       this.recipient = recipient;
   }
   public String getText() {
       return text;
   }
   public void setText(String text) {
       this.text = text;
   }
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
  
}

Document.java


/*
* Document class
*/
public class Document {

   String text;
  
   public String toString(String text){
       return text.toString();
   }
//Setters and getters.
   private String getText() {
       return text;
   }

   public void setText(String text) {
       this.text = text;
   }
}


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 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 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...
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...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
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...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT