In: Computer Science
GIVEN THE CODE BELOW
Given the following class:
/**
* Document class.
*/
public class Document {
private int words;
/**
* constructor
* pre: none
* post: A Document object created. Words initialized to 0.
*/
public Document() {
words = 0; //default words
}
/**
* Changes the number of document words.
* pre: none
* post: Words has been changed.
*/
public void setWords(int numWords) {
words = numWords;
}
/**
* Calculates the number of pages.
* pre: none
* post: The number of pages has been returned.
*/
public int calculatePages() {
final int WORDS_PER_PAGE = 300;
int pages;
pages = words / WORDS_PER_PAGE;
if (words % WORDS_PER_PAGE > 0) {
pages += 1;
}
return(pages);
}
/**
* Returns the number of words in a document.
* pre: none
* post: The number of document words has been returned.
*/
public int getWords() {
return(words);
}
}
Create an Essay class that inherits the Document class. The Essay
class should include member constants MIN_WORDS and
MAX_WORDS.
An essay should be between 1500 and 3000 words. The Essay class
should also contain
a member method validLength(), which returns true when the Essay
object contains between MIN_WORDS and MAX_WORDS and false
otherwise.
**Create an EssayAssignment application that tests the Essay class.
EssayAssignment will contain the main() method.
Program:
/**
* Document class.
*/
public class Document {
private int words;
/**
* constructor
* pre: none
* post: A Document object created. Words initialized to 0.
*/
public Document() {
words = 0; //default words
}
/**
* Changes the number of document words.
* pre: none
* post: Words has been changed.
*/
public void setWords(int numWords) {
words = numWords;
}
/**
* Calculates the number of pages.
* pre: none
* post: The number of pages has been returned.
*/
public int calculatePages() {
final int WORDS_PER_PAGE = 300;
int pages;
pages = words / WORDS_PER_PAGE;
if (words % WORDS_PER_PAGE > 0) {
pages += 1;
}
return(pages);
}
/**
* Returns the number of words in a document.
* pre: none
* post: The number of document words has been returned.
*/
public int getWords() {
return(words);
}
}
/**
* Essay class extends Document class
*/
public class Essay extends Document
{
/* member constants */
final int MIN_WORDS = 1500;
final int MAX_WORDS = 3000;
/**
* Returns returns true when the Essay object contains
between
* MIN_WORDS and MAX_WORDS and false otherwise
* pre: none
* post: return true when the Essay object contains
between
* MIN_WORDS and MAX_WORDS and false otherwise
*/
public boolean validLength()
{
if(getWords() >= MIN_WORDS
&& getWords()<=MAX_WORDS)
return
true;
return false;
}
}
/**
* EssayAssignment class
*/
public class EssayAssignment
{
//main method
public static void main (String[] args)
{
//create an object of Essay
class
Essay essay = new Essay();
//set the number of words
essay.setWords(2000);
//display the number of words
System.out.println ("Number of
words: " + essay.getWords());
//display the number of pages
System.out.println ("The number of
pages: " + essay.calculatePages());
//check valid length
if(essay.validLength())
System.out.println ("Valid length");
else
System.out.println ("Invalid length");
}
}
Output:
Number of words: 2000
The number of pages: 7
Valid length
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.