Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Nowadays, international treaties are registered with the United Nations. so t

ID: 3569868 • Letter: 2

Question

2. Nowadays, international treaties are registered with the United Nations. so the UN must store all those treaties, which are usually lengthy. Before printing a treaty, we would like to know how many pages it will take. a. Write a method called nPages that takes as argument the number of words in a treaty_ and the number of words per page desired. Your method throws an ''IllegalArgumentException'' if any of the arguments is negative. otherwise it computes and returns the number of pages to print the treaty. For example. if a treaty has 2410 words. and you want 200 words per page. the method should return 12.5. b. Write a statement that calls your method with the example above. c. Trace your method call step by step. For extra credit: Instead of returning the number of pages as a double. return the number of pages as an integer. If the treaty had 2401 words. printing 200 words per page would require 13 page, as the last word would require a new page. You may want to call the method Math . cell( ) that rounds a number up to the next integer.

Explanation / Answer

2)
a)

public static double nPages(int words, int wordsPerPage)
{
   double pages = 0;
   if(words < 0 || wordsPerPage < 0)
       throw IllegalArgumentException("Error: score is negative.");
   pages = Math.ceil((double)(words) / wordsPerPage);
}

b)
int numOfPages = nPages(2410, 200);

c)
The method nPages() will take the 2 arguments as words = 2410 and wordsPerPage = 200
Then it initialises pages to 0
It checks whether any of the argument is negative... If so it throws an IllegalArgumentException with a message
Otherwise it computes the number of pages to print the treaty and returns it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote