Question

In: Computer Science

I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...

I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions..

It does not need to output the values in anyway, but it should return them so they can be output elsewhere.

Please try to use the stub class below. You can edit it obviously.

public ArrayList<Property> filterByPriceRange(int min, int max) {
       return null;
   }

Solutions

Expert Solution

According to the problem statement, we want to implement a method that returns an ArrayList of Property objects. The Property objects present in the ArrayList must have their prices in the given range, that is, we are given the minimum (min) and maximum (max) prices.

Therefore, we will return the Property objects whose prices are either greater than or equal to min and smaller than or equal to max.

Please note that as we are not given the definition of the Property class in the problem statement. We are going to assume that the price data member is private and we will access the value of the price associated with the Property using a public method getPrice() defined in the Property class. Please note that this is only an assumption and if your Property class has the price data member to be public, then, we do not require to use the getPrice () method and directly access the price. In a nutshell, the class definition of Property is:

Property class:

public class Property {

    // other data members
    private int price;
    // other data members

    // constructor and other methods
    public int getPrice () {
        return price;
    }
    // other methods
}

Please note that this is NOT the full class definition and you may add other data members and methods into it.

Now, let's start implementing our method:

1. As we are given in the problem statement that we can change the stub code given. We want to add an ArrayList<Property> argument (as mentioned in the problem statement that the method takes in an ArrayList).

2. We will then, create another ArrayList<Property> object output, which will hold the Property objects whose prices are in the range specified by min and max arguments provided to the method.

3. We will traverse the ArrayList<Property> given as the argument, and check the prices using the getPrice() function, and put the ones which are in the given range into the output list.

4. Finally, we return the list.

Please find all the code explanations within the code itself as comments.

Here is the full code:

// an argument "properties" which is an ArrayList of type Property is added to the function
public ArrayList<Property> filterByPriceRange(ArrayList<Property> properties, int min, int max) {

        // create an ArrayList object to put those Property objects 
        // which are in the price range
        ArrayList<Property> output = new ArrayList<Property> ();

        // traverse the ArrayList properties provided in the argument
        // we will use the "traditional for loop"( for(init;condition;update) ) to traverse the list
        // also, we are using the size function of ArrayList to get the
        // number of Property objects in the list
        for (int i = 0; i < properties.size(); ++i) {
                        
                // get the property at ith position using get() function of ArrayList
                Property property = properties.get(i);

                // get the price of the Property using getPrice() function
                int price = property.getPrice();

                // check if it is in the range specified by min and max
                if (price >= min && price <= max) {
                        
                        // if it is in range, add the property to the output list
                        output.add(property);
                }
        }

    // finally, return the ArrayList so formed
        return output;
}

Please refer to the screenshot of the code for understanding the indentation of the code.


Related Solutions

I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT