Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Implement the following method: /** * Calculates an array of percentages from an array of values. * * Specifically, this method calculates the total

1. Implement the following method:

/** * Calculates an array of percentages from an array of values. * * Specifically, this method calculates the total of all of the * elements in the given array and divides each element into the * total to determine the percentages. * * @param values The array of non-negative values * @return The array of percentages. */ public static double[] toPercentages(double[] values) { }

2. Desk check the toPercentages() method. What conditions will cause it to fail?

3. What exception will be thrown when the following application program is executed with the method? What line will cause the exception to be thrown? Why? Include a screenshot of your code and the output pane.

private static double[] sales; public static void main(String[] args) { double[] percentages; percentages = toPercentages(sales); }

4. Modify the declaration of the toPercentages() method so that it specifies the NullPointerException. What is the declaration now?

5. Modify the first for loop in the toPercentages() method so that it throws an IllegalArgumentException if any element of values is negative. What statements are in the body of the loop now?

6. Why do you not need to include an else clause in the if statement?

7. What will happen if more than one element is negative? In particular, how many exceptions will be thrown? Include a screenshot of your code and the output pane.

8. Modify the declaration of the toPercentages() method so that it now also includes IllegalArgumentException. What is the declaration now?

9. Add a statement after the first for loop, but before the statement that allocates memory for result, that throws an IllegalArgumentException if total is 0. What statement did you add? Include a screenshot of your code and the output pane.

10. The toPercentages(double[]) method is now implemented as follows.

public static double[] toPercentages(double[] values) throws IllegalArgumentException, NullPointerException { double total; double[] result; // Calculate the total total = 0; for (int i=0; i 

Why is the statement that allocates memory for result where it is rather than earlier (for example right after the declaration or even in the declaration statement)?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

8. Explain the difference between translation and interpretation.

Answered: 1 week ago

Question

10. Discuss the complexities of language policies.

Answered: 1 week ago

Question

1. Understand how verbal and nonverbal communication differ.

Answered: 1 week ago