Question

In: Computer Science

Java, no imports: Given a list of integers, round each number to the nearest multiple of...

Java, no imports:

Given a list of integers, round each number to the nearest multiple of 5.
       2.5-7.49 round to 5. 7.5-12.49 round to 10. 12.5-17.49 round to 15. etc.
       return the sum of all the rounded elements.
       Implement this way for credit:
       Fill in the method directly below this one called helperRound() to round each number.

Call that method and use the sum of the returned values.

       helperSum({4.3, 16.7}) returns 20
       helperSum({25.7, 21.2, 27.5}) returns 75
       helperSum({2.5}) returns 5
       helperSum({2.49, 12.49, 40.2, 42.5}) returns 95
       @param nums is an arrayList of doubles
       @return the sum of the all elements after rounding
   */
public static int helperSum(ArrayList<Double> nums)
{
       int sum = 0;
       return sum;
}//end helperSum

   /**
        Method HelperRound will round a number up or down to the nearest 5.

        @param num double number,
        @return the rounded up or down number to the nearest multiple of 5.
    */
public static int helperRound(double n){
           int rounded = 0;

           return rounded;
}//end HelperRound

Solutions

Expert Solution

Program Code Screenshot

Sample Output

Program Code to Copy

import java.util.ArrayList;
import java.util.Arrays;

class Main{
    public static int helperSum(ArrayList<Double> nums)
    {
        int sum = 0;
        //Loop through all elements in the array
        for(double x : nums){
            //Find sum of helperRounds
            sum += helperRound(x);
        }
        return sum;
    }//end helperSum

    /**
     Method HelperRound will round a number up or down to the nearest 5.

     @param n double number,
     @return the rounded up or down number to the nearest multiple of 5.
     */
    public static int helperRound(double n){
        //l5 and r5 are the nearest multiples of 5 on either side
        int l5 = (int)n;
        while(l5%5!=0){
            l5--;
        }
        int r5 = (int)n;
        //Check the closest l5, or r5
        while(r5%5!=0){
            r5++;
        }
        if(n-l5<r5-n){
            return l5;
        }
        return r5;
    }//end HelperRound

    public static void main(String[] args) {
        double d[] = {4.3,16.7};
        ArrayList<Double> list = new ArrayList<>();
        for(double x : d){
            list.add(x);
        }
        System.out.println(Arrays.toString(d)+" : "+helperSum(list));
        d = new double[]{25.7, 21.2, 27.5};
        list = new ArrayList<>();
        for(double x : d){
            list.add(x);
        }
        System.out.println(Arrays.toString(d)+" : "+helperSum(list));
        d = new double[]{2.5};
        list = new ArrayList<>();
        for(double x : d){
            list.add(x);
        }
        System.out.println(Arrays.toString(d)+" : "+helperSum(list));
        d = new double[]{2.49, 12.49, 40.2, 42.5};
        list = new ArrayList<>();
        for(double x : d){
            list.add(x);
        }
        System.out.println(Arrays.toString(d)+" : "+helperSum(list));
    }
}

Related Solutions

Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
When doing your calculations, round to the nearest whole dollar amount AND the nearest whole number...
When doing your calculations, round to the nearest whole dollar amount AND the nearest whole number of shares,  Enter your answers in whole dollar amounts, without '$' signs and without commas. This fact pattern spans three years. All eight requirements are based on this fact pattern. However, #1 only asks the balance in the Common Stock account at the end of Year 2.  ●Issuance of Shares: Company issued 6,000 common shares with a $10 per share par value for $95,000...
Do not round intermediate calculations and round your answers to the nearest whole number, e.g., 32....
Do not round intermediate calculations and round your answers to the nearest whole number, e.g., 32. A negative answer should be indicated by a minus sign. Problem: Project Evaluation Suppose you have been hired as a financial consultant to Defense Electronics, Inc. (DEI), a large, publicly traded firm that is the market share leader in radar detection systems (RDSs). The company is looking at setting up a manufacturing plant overseas to produce a new line of RDSs. This will be...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...
In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4. the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.
(Programming Language: Python) Complete the function remove number such that given a list of integers and...
(Programming Language: Python) Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. # DO NOT ADD ANY OTHER IMPORTS from typing import List def remove_number(lst: List[int], number: int) -> None: """ Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a...
Find the equation of the regression line for the given data. Round values to the nearest...
Find the equation of the regression line for the given data. Round values to the nearest thousandth. x=-5,-3,4,1,-1,-2,0,2,3,-4 y=11,-6,8,-3,-2,1,5,-5,6,7 OPTIONS = 0.206x - 2.097 = -2.097x + 0.206 = 2.097x - 0.206 = -0.206x + 2.097
In Java, a set of integers is given. write a function to find 2 integers in...
In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value. i.e [1,5,2,0,11,3] target = 7 result [5,2] i.e [1,5,4,0,14,-7] target = 9 result [5,4] NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
Given a list of negative integers, write a Python program to display each integer in the...
Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly divisible by either 5 or 7. Enter...
Suppose you are given a list of n integers, each of size at most 100n. How...
Suppose you are given a list of n integers, each of size at most 100n. How many operations would it take you to do the following tasks (in answering these questions, we are interested primarily in whether it will take logn, sqrt(n), n, n^2, n^3, 2^n, ... steps. In other words, ignore multiplicative constants.) 1. Determine the longest sequence of consecutive integers belonging to the list. 2. Determine the number of primes in the list. 3. Determine whether there are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT