Question

Complete the following Java method. The purpose of the method is to modify the input array...

Complete the following Java method. The purpose of the method is to modify the input array of doubles so that each number in the array is rounded to the nearest integer. It also creates and returns a new array of doubles that have all the (rounded) numbers in nums that are greater than bound. Note that the returned array may not be the same size as the input array. Also, note that the numbers in the input array might be negative. You must use one or more for loops. You are NOT allowed to use the Math class.

public static double[] round(double[] nums, double bound){

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static double[] round(double[] nums, double bound) {
    int count = 0;
    for(int i = 0; i < nums.length; i++) {
        if(Double.valueOf(String.format("%.0f", nums[i])) > bound) {
            count++;
        }
    }
    double[] result = new double[count];
    int index = 0;
    for(int i = 0; i < nums.length; i++) {
        if(Double.valueOf(String.format("%.0f", nums[i])) > bound) {
            result[index++] = Double.valueOf(String.format("%.0f", nums[i]));
        }
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
Complete the following Java method. The purpose of the method is to modify the input array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT