Question

In: Computer Science

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 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

DocumentTest.java

import org.junit.Test;
import static org.junit.Assert.*;

public class DocumentTest {
  
    @Test
    public void canCreateDocumentObject() {
        Document document = new Document();
        assertNotNull(document);
    }
  
    @Test
    public void aDocumentHasTextContent() {
        Document document = new Document();
        document.setText("This is the text of the document");
        assertEquals("Text is", "This is the text of the document", document.toString());
    }
  
    @Test
    public void aDocumentCanLookIfAKeywordIsPresent() {
        Document document = new Document();
        document.setText("This is the text of the document");
        assertTrue(Document.ContainsKeyword(document, "of the document"));
      
        Email email = new Email();
        email.setText("I am starting a new job");
        assertFalse(Document.ContainsKeyword(email, "of the document"));
        assertTrue(Document.ContainsKeyword(email, "I am"));
      
        File file = new File();
        file.setText("");
        assertTrue(Document.ContainsKeyword(file, ""));
    }
}


Document.java


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

    public String getText() {
        return text;
    }
  
    public static boolean ContainsKeyword(Document docObject, String keyword) {
        if(docObject.toString().indexOf(keyword, 0) >= 0 ) {
            return true;
        }
        return false;
    }
  
    @Override
    public String toString() {
        return text;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.text);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Document other = (Document) obj;
        if (!Objects.equals(this.text, other.text)) {
            return false;
        }
        return true;
    }
  
}


Email.java

public class Email extends Document {
    private String sender;
    private String recipient;
    private String title;

    public Email() {
        super();
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getSender() {
        return sender;
    }

    public void setRecipient(String abchotmailcom) {
        this.recipient = abchotmailcom;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setTitle(String starting_New_Job) {
        this.title = starting_New_Job;
    }

    public String getTitle() {
        return title;
    }
  
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Sender: ").append(getSender()).append("\n");
        builder.append("Recipient: ").append(getRecipient()).append("\n");
        builder.append("Title: ").append(getTitle()).append("\n");
        builder.append("Body: ").append(super.toString());
        return builder.toString();
    }
  
}

File.java


public class File extends Document {
    private String pathname;
  
    public File() {      
    }

    public void setPathname(String pathname) {
        this.pathname = pathname;
    }

    public String getPathname() {
        return pathname;
    }
  
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Path: ").append(getPathname()).append("\n");
        builder.append("Contents: ").append(super.toString());
        return builder.toString();
    }
}


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 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...
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...
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...
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...
Compare between document type declarations and document instance.
Compare between document type declarations and document instance.
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - 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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT