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...
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.
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 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...
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
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
[JAVA] How to do this problem. we have to tell if given list of three number...
[JAVA] How to do this problem. we have to tell if given list of three number is in ascending, descending or no order. Instructions You will be given three integers: ?, ? and ? (read in for you). Then you will be given two boolean values that tell you which way the numbers are going - if the numbers are in increasing order or in decreasing order, respectively. Details Input The program reads in: a sequence of three integers: ?,...
Figure the total balance owed for each of the following (round to the nearest hundredth): 1)...
Figure the total balance owed for each of the following (round to the nearest hundredth): 1) Retailer purchase items from whole seller with the following terms; shovels@ 12/ea; purchased 12 shovels on June 20 "10/10 net 30" brooms @8/ea ; purchased 50 brooms on June 30 "15/5 net 30" boxes of nails @ 50/each;purchased 20 boxes July 1 "2/10 net 30" **days of Holiday included towards total days counted Purchaser will pay bill "in full" today. How much does she...
Figure the total balance owed for each of the following (round to the nearest hundredth): 1)...
Figure the total balance owed for each of the following (round to the nearest hundredth): 1) Retailer purchase items from whole seller with the following terms; shovels@ 12/ea; purchased 12 shovels on June 20 "10/10 net 30" brooms @8/ea ; purchased 50 brooms on June 30 "15/5 net 30" boxes of nails @ 50/each;purchased 20 boxes July 1 "2/10 net 30" **days of Holiday included towards total days counted Purchaser will pay bill "in full" today. How much does she...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT