Question

In: Computer Science

GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...

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.

Solutions

Expert Solution

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.


Related Solutions

Given the following code below: public class Calculation { //method that returns cube of the given...
Given the following code below: public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){    int max=0;    for(int i=1;i<arr.length;i++){        if(max<arr[i]){           max=arr[i]; }     }     return max;   } //method that returns cube of the given number   public static int cube(int n){        return n*n*n;     }   } (5 points) Define the class CalculationTest a subclass of TestCase (10 points) Define a setUp() method (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class (5 points)Define...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
3. Given the test code below, what is the output to the console? public class TestMe{...
3. Given the test code below, what is the output to the console? public class TestMe{ public TestMe(){     System.out.print(“a”); } public void setUp(){     System.out.print(“b”); } public void tearDown(){     System.out.print(“c”); } @Test public void testX(){     System.out.print(“x”); } @Test public void testY(){     System.out.print(“y”); } } A. abxcbyc B. abxcabyc C. bxcbyc D. abxyc
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
public class Sum2 { // TODO - write your code below this comment. } Download the...
public class Sum2 { // TODO - write your code below this comment. } Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4: Sum: 7
public class FirstChar { // TODO - write your code below this comment. } Download the...
public class FirstChar { // TODO - write your code below this comment. } Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line argument foo is shown below: First...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT