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

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 methods getText and setText to get and set this value, respectively. Include the constructor that set all of the instance variables. Redefine equals method to test whether two documents are equal.

Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, hasAttachement and subject 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. Include the constructor that set all of the instance variables. Redefine equals method to test whether two emails are equal.

Similarly, define a class for File that is derived from Document and includes an instance variable for the pathName, size (e.g., 10KB, 100KB, etc) and fileType (e.g., pdf, doc, .). The textual contents of the file should be stored in the inherited variable text. Redefine the toString method to concatenate all the text fields. Include the constructor that set all of the instance variables. Redefine equals method to test whether two files are equal.

Finally, make the Email and the File classes implement the Comparable interface. Define the compareTo method to order Email objects based on the sender name. Also, define compareTo method to order File objects based on the file size.

Note: hasAttachment is a boolean variable. True when an email has an attachment, otherwise false.

Here's the Driver class.

public class Driver {

public static void main(String[] args) {

// create to instances of Document doc1 and doc2 and call toString

// on each one of them

Document doc1 = new Document("This is some text to ....");

Document doc2 = new Document("This is some more text. Some more text.");

System.err.println("**Test Document Class**");

System.out.println(doc1.toString());

System.out.println(doc2.toString());

// check if doc1 is equal to doc2 by calling equals method

boolean isEqual1 = doc1.equals(doc2);

System.out.println("Is doc1 equals to doc2: " + isEqual1);

// declare and initialize arguments required for Email class constructor

String sender = "John";

String receiver = "Joshua";

String subject = "CSCI";

String text = "Hi Joshua, "

+ "Can you please tell what's wrong with my program."

+ "Thanks,"

+ "John";

boolean attachment = false;

// create an instance of email class

Email email1 = new Email(sender, receiver, subject, attachment, text);

// another instance of email class

Email email2 = new Email(sender, receiver, subject, attachment, text);

System.err.println("**Test Email Class**");

System.out.println(email1.toString());

boolean isEqual2 = email1.equals(email2);

System.out.println("is email1 equals to email2: " + isEqual2);

// declare and initialize arguments required for File class constructor

String path = "c:\\Users\\foo\\tmp.txt";

String type = "pdf file";

String fileText = "When the user quits .... list of all entered Students.";

// create an instance of File class

File file1 = new File(path, type, fileText);

File file2 = new File("c:\\Users\\fo\\tmp.txt", type, fileText);

System.err.println("**Test File Class**");

System.out.println(file1.toString());

System.out.println("is file1 equals to file2: " + file1.equals(file2));

System.err.println("**more equals test**");

// let's try to compare a file to a document

System.out.println("is file1 equals to doc1: " + file1.equals(doc1));

System.out.println("is doc1 equals to file1: " + doc1.equals(file1));

System.out.println("is doc2 equals to email2: " + doc2.equals(email2));

System.out.println("is file1 equals to null: " + file1.equals(null));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!