NO SKYPE .. Please answer here .. Define a class named Document that contains an
ID: 3539305 • Letter: N
Question
NO SKYPE .. Please answer here ..
Define a class named Document that contains an instance variable of type String named text that stores the text content for the document. Create a toString method that returns the text field and also include a method to set this value. Create a find method that returns a String and takes a parameter of type String called word. It should return a String containing all the line numbers and position that the word occurred: e.g. 3:20 , 14:18 etc.. or null if the word does not exist at all in text.
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 (all String type). Implement appropriate getters and setters. The body of the email message should be stored in the inherited variable text. Override the toString method to return all the email information. Override the find method so that in addition to returning the line numbers it also checks the subject line.
Similarly, define a class for File that is derived from Document and includes an instance variable for the pathname, extension and size. The contents of the file should be stored in the inherited variable. Override the toString method to return all the information. Override the find method so that in addition to returning the line numbers it also checks the pathname.
Finally, make a program to test everything: create sample objects of type Email, File and Document in its main method. Then write a static method called search that returns void and has 2 parameters: one of type Document called doc and the other of type String called word. It should print out the result of calling the find method of its Document parameter (print not found if its null) and the class name of its parameter doc. Call this method with the objects you created in main.
Explanation / Answer
Content of the text file
saved as
Hi my name is lav and live
do you lav and live
lov is love
lav is lav
and live is lav
do you get me lav
the thing is lav need to undrstand
bull lav
Sample output
Document [text=Hi my name is lav and live &&& do you lav and live &&& lov is love &&& lav is lav &&& and live is lav &&& do you get me lav &&& the thing is lav need to undrstand &&& bull lav &&& ]
Email [recipent=James Willy, sender=Richie Anderson, title=Asking for a day off, getText()=Hi my name is lav and live &&& do you lav and live &&& lov is love &&& lav is lav &&& and live is lav &&& do you get me lav &&& the thing is lav need to undrstand &&& bull lav &&& ]
File [extension=.txt, pathName=C:lav, size=1 kb, getText()=Hi my name is lav and live &&& do you lav and live &&& lov is love &&& lav is lav &&& and live is lav &&& do you get me lav &&& the thing is lav need to undrstand &&& bull lav &&& ]
Enter the word you want to search
lav
Printing using find method in Document class
1:5 2:3 4:1 4:3 5:4 6:5 7:4 8:2
Printing using find method in Email class
subject is : Asking for a day off : 1:5 2:3 4:1 4:3 5:4 6:5 7:4 8:2
Printing using find method in File class
Path of the file is : C:lav : 1:5 2:3 4:1 4:3 5:4 6:5 7:4 8:2
Code
Document class
public class Document {
protected String text;
// to get the value
protected String getText() {
return text;
}
// to set the value
public void setText(String text) {
this.text = text;
}
// super constructor;
public Document() {
super();
}
// Parameterised constructor
public Document(String text) {
super();
this.text = text;
}
//toString method
@Override
public String toString() {
return "Document [text=" + text + "]";
}
public String find(String word)
{System.out.println(" Printing using find method in Document class ");
word = word.toLowerCase();
int count = 1;
String wordAndLine = null;
String[] linesInTotal =text.split("&&&");
for (int i = 0; i <linesInTotal.length; i++)
{
if (linesInTotal[i].contains(word))
{
linesInTotal[i].replaceAll(" ", "");
String[] splited = linesInTotal[i].split("\s+");
int wordCount = 1;
for (String p : splited)
{
if (p.equals(word)){
if(linesInTotal[i]== linesInTotal[0])
{
wordAndLine += count +":"+wordCount+ " ";
wordCount++;
}
else if (linesInTotal[i] != linesInTotal[0])
{
wordAndLine += count +":"+ --wordCount+ " ";
wordCount+=2;
}
}
else
{
wordCount++;
}
}
}
count++;
}
if(wordAndLine!= null)
wordAndLine= wordAndLine.substring(4);
return wordAndLine;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.