Ask Question

g Create a package named lab7 (no spaces, ALL in lowercase) where you will place your lab files. Write a class FilterWith with a method called filterRange that accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max (inclusive). For example, if a variable called list stores the values [4, 7, 9, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7], the call of filterRange (list, 5, 7); should remove all values between 5 and 7, changing the list to store [4, 9, 2, 3, 1, 8]. If no elements in range min-max are found in the list, or if the list is initially empty, the list's contents are unchanged. Create an application that creates an object of FilterWith and invokes the filterRange method and show the results

+4
Answers (1)
  1. 1 August, 14:07
    0
    See explaination

    Explanation:

    import java. util. ArrayList;

    import java. util. Scanner;

    public class ListFilter

    {

    public static void filterRange (ArrayList list, int min, int max)

    {

    ArrayList listNew=new ArrayList ();

    for (int i=0; i
    {

    if (list. get (i) >=min && list. get (i) <=max) / /Checking if element is between min and max

    {

    listNew. add (list. get (i));

    }

    }

    list. removeAll (listNew); / /removing all elements from list.

    }

    public static void main (String[] args)

    {

    ArrayList list=new ArrayList ();

    list. add (4);

    list. add (7);

    list. add (9);

    list. add (2);

    list. add (7);

    list. add (7);

    list. add (5);

    list. add (3);

    list. add (5);

    list. add (1);

    list. add (7);

    list. add (8);

    list. add (6);

    list. add (7);

    Scanner sc=new Scanner (System. in);

    System. out. println ("Enter min : ");

    int min=sc. nextInt ();

    System. out. println ("Enter Max : ");

    int max = sc. nextInt ();

    filterRange (list, min, max);

    //Displaying new List

    for (int i=0; i
    {

    System. out. print (list. get (i) + " ");

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “g Create a package named lab7 (no spaces, ALL in lowercase) where you will place your lab files. Write a class FilterWith with a method ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers