Question

In: Computer Science

Create a static method with the appropriate inputs and outputs. Call each of them in the...

Create a static method with the appropriate inputs and outputs. Call each of them in the main method.

Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list should contain [25, 5, 30, 25].

Please do this in Java.

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.List;

public class AllMultiplesList {
    public static List<Integer> allMultiples(List<Integer> list, int n){
        List<Integer> result = new ArrayList<>();
        for(int i = 0;i<list.size();i++){
            if(list.get(i)%n==0){
                result.add(list.get(i));
            }
        }
        return result;
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(25);
        list.add(2);
        list.add(5);
        list.add(30);
        list.add(19);
        list.add(57);
        list.add(2);
        list.add(25);

        List<Integer> result = allMultiples(list, 5);
        System.out.println(result);
    }
}

[25, 5, 30, 25]


Related Solutions

Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2,...
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist()....
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist(). • Norm.Inv(). • T.Dist().
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block....
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block. Assume the elevator has 4 floors. Draw a state diagram for the elevator controller and briefly explain what the input, state variables, and ouput represent.
Consider the following inputs and outputs associated with a DCS system.
  Consider the following inputs and outputs associated with a DCS system. It is interesting to note that EXACTLY the same could be applicable to a SCADA system (although the SCADA system could use either a dedicated RTU or else a PLC). Break the following signals up (either going to, or away from the RTU-type devices) and indicate which are: i) digital inputs, ii) digital outputs, iii) analog inputs and iv) analog outputs: 18.1 ( 1 mark) START signal 18.2...
This is a question on the inputs and outputs on Basic javascript application What is the...
This is a question on the inputs and outputs on Basic javascript application What is the displayed output of the application if the user inputs Are: 1    in the first box, 2 in the second box, 3 in the third box, and 4 in the fourth box Modify the HTML and javascript file in order to Creates an h1 and a span element ii)          Accesses all the html elements, then adds the four input values as strings and sends the...
describe the inputs, the plants, and the outputs of open-loop control systems.
describe the inputs, the plants, and the outputs of open-loop control systems.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT