In: Computer Science
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;
}
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.