Question

In: Computer Science

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 obviously.

The code for the main class is not necessary. I am only really asking for the formula code to filterByAmenities. I do not want anything else. The code doesnt even necessarily have to work as is, but it could just work when provided data. It just needs to filter given the criteria.

public ArrayList filterByAmenities(boolean pool, boolean gym, boolean pets, boolean laundry, boolean shuttle) {
       return null;
   }
  

Solutions

Expert Solution

Hope you are doing well. I am try to solve the problem as per your requirment. If you give me some sample input and output data, I will Edit the Solution. I wll be try to solve the problem as per requirment, in a simple way. Hope below solution give you correct Answer.

--------------------------------------

import java.util.ArrayList;

public class FilterData {
   static ArrayList<FilterData> listData=new ArrayList<FilterData>();
   boolean pool;
   boolean gym;
   boolean pets;
   boolean laundry;
   boolean shuttle;
  
   public FilterData(boolean pool, boolean gym, boolean pets, boolean laundry, boolean shuttle) {
      
       this.pool = pool;
       this.gym = gym;
       this.pets = pets;
       this.laundry = laundry;
       this.shuttle = shuttle;
   }

   public FilterData() {
       // TODO Auto-generated constructor stub
   }

   public boolean isPool() {
       return pool;
   }

   public void setPool(boolean pool) {
       this.pool = pool;
   }

   public boolean isGym() {
       return gym;
   }

   public void setGym(boolean gym) {
       this.gym = gym;
   }

   public boolean isPets() {
       return pets;
   }

   public void setPets(boolean pets) {
       this.pets = pets;
   }

   public boolean isLaundry() {
       return laundry;
   }

   public void setLaundry(boolean laundry) {
       this.laundry = laundry;
   }

   public boolean isShuttle() {
       return shuttle;
   }

   public void setShuttle(boolean shuttle) {
       this.shuttle = shuttle;
   }

   public static void main(String[] args) {
       FilterData obj=new FilterData();
      
       FilterData data1=new FilterData(true, true, false, false, true);
       listData.add(data1);
       FilterData data2=new FilterData(false, true, true, true, true);
       listData.add(data1);
       ArrayList<FilterData> filterData=obj.filterByAmenities(true, true, false, false, true);
       System.out.println("---Filter data is---");
       for(FilterData data:filterData) {
           System.out.println("Pool: "+data.isPool());
           System.out.println("Gym: "+data.isGym());
           System.out.println("Pool: "+data.isPets());
           System.out.println("Pool: "+data.isLaundry());
           System.out.println("Pool: "+data.isShuttle());
       }
      
   }
   // filtering the data on basis of pool and gym
   public ArrayList<FilterData> filterByAmenities(boolean pool, boolean gym, boolean pets, boolean laundry, boolean shuttle) {
       ArrayList<FilterData> listData1=new ArrayList<FilterData>();
       for(FilterData data: listData) {
           // filtering the data on the basis of pool ang gym
           // we only filtering pool and gym have true values
           if(data.isGym() && data.isPool()) {
               listData1.add(data);
           }
       }
       return listData1;
   }

}
----------------------

SAMPLE OUTPUT:

---Filter data is---
Pool: true
Gym: true
Pool: false
Pool: false
Pool: true
Pool: true
Gym: true
Pool: false
Pool: false
Pool: true
-----------------------

SUMMARY:

I have provided the solution as per your requirement, i hope you're satisfied with the way i have approached. please dont hesitate to give me a Like if you like it, i appreciate your like. If you have any queries you can shoot them any time in comments section. I will be glad to help you.
Thank you :)


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 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...
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....
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