Answer to Question #176884 in Java | JSP | JSF for Hari Chandana T

Question #176884
  1. Write a custom sorting method that rearranges a List of LivingSpace objects so that all the properties that do not have an assigned address are moved to the front of the list, in order of descending number of bedrooms. All properties with an address may be left unsorted at the end of the list.
  2. An insertion sort is probably the best-suited algorithm for this task. Do not implement a selection or merge sort algorithm. Java's built-in sorting methods use a highly-optimized merge sort, so there is rarely a good reason to write your own version. Selection sort is rarely useful when speed is the primary concern Your solution should have a method header that looks like: public static void customSort(List properties);
  3. In your main method, give an example where the sorting algorithm you created in step 1 is faster than a solution using Collections#sort(List, Comparator), and an example where it is slower
1
Expert's answer
2021-03-30T16:33:56-0400
house.java


public class House extends LivingSpace {

        private int noOfBedRooms;
        private int noOfBathRooms;
        private double sellingPrice;
        private String address;

        public House(int noOfRooms, double sellingPrice, int l, int b, int noOfbathRooms, String address) {
                super("House", l, b);
                this.noOfBedRooms = noOfRooms;
                this.sellingPrice = sellingPrice;
                this.noOfBathRooms = noOfbathRooms;
                this.address = address;
        }

        public double getSellingPrice() {
                return sellingPrice;
        }

        public int getNoOfBathRooms() {
                return noOfBathRooms;
        }

        public int getNoOfBedRooms() {
                return noOfBedRooms;
        }

        public String getAddress() {
                return address;
        }

        @Override
        public String toString() {
                return "House [ noOfBathRooms=" + noOfBathRooms + ", sellingPrice=" + sellingPrice + ", address=" + address
                                + "]";
        }

}

Livingspace.java


public class LivingSpace{

        private String propertyType;
        private int lengthInFeet;
        private int breadthInFeet;

        public LivingSpace(String propertyType, int lengthInFeet, int breadthInFeet) {
                super();
                this.propertyType = propertyType;
                this.lengthInFeet = lengthInFeet;
                this.breadthInFeet = breadthInFeet;
        }

        public String getPropertyType() {
                return propertyType;
        }

        public int getLengthInFeet() {
                return lengthInFeet;
        }

        public int getBreadthInFeet() {
                return breadthInFeet;
        }
        
        public int getSquareFeet(){
                return lengthInFeet*breadthInFeet;
        }

        @Override
        public String toString() {
                return "LivingSpace [propertyType=" + propertyType + " Square feet "+getSquareFeet()+"]";
        }
}

Apartment.java


public class Apartment extends LivingSpace {

        private int noOfBathrooms;
        private int streetNumber;
        private String address;

        public Apartment(int lengthInFeet, int breadthInFeet,int noOfBathrooms, int streetNumber, String address) {
                super("Apartment", lengthInFeet, breadthInFeet);
                this.streetNumber = streetNumber;
                this.noOfBathrooms=noOfBathrooms;
                this.address=address;
        }

        public int getNoOfBathrooms() {
                return noOfBathrooms;
        }

        public int getStreetAddress() {
                return streetNumber;
        }

        public int getStreetNumber() {
                return streetNumber;
        }

        public String getAddress() {
                return address.toLowerCase();
        }

        @Override
        public String toString() {
                return "Apartment [noOfBathrooms=" + noOfBathrooms + ", streetNumber=" + streetNumber + ", address=" + address
                                + "]";
        }
        
        
}

Task.java

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;;

public class Task {

        static Double findMin(Double[] doubles) {
                List<Double> list = Arrays.asList(doubles);
                return Collections.min(list);
        }

        static String findLastStringInOrder(List<String> list) {
                return Collections.max(list);
        }

        static Integer findSmallestEvenValue(List<Integer> list) {
                List<Integer> temp = list;
                temp.removeIf((x) -> (x % 2 == 1));
                return Collections.min(temp);
        }

        static void sortListOfHousesBySP(List<House> houses) {
                Comparator c = Comparator.comparing(House::getSellingPrice);
                Collections.sort(houses, c);
        }

        static Integer maxLivingSpaceByArea(List<LivingSpace> ls) {
                Comparator c = Comparator.comparing(LivingSpace::getSquareFeet);
                return Collections.max(ls, c).getSquareFeet();
        }

        static void sortListOfApartments(List<Apartment> ls) {
                Comparator c = Comparator.comparing(Apartment::getNoOfBathrooms).thenComparing(Apartment::getSquareFeet);
                Collections.sort(ls, c);
        }

        static House houseWithMostBathrooms(List<House> list) {
                List<House> temp = list;
                temp.removeIf(a -> (a.getAddress() == null));
                Comparator c = Comparator.comparing(House::getNoOfBathRooms).thenComparing(House::getNoOfBedRooms);
                return Collections.max(list, c);
        }

        static Apartment apartmentWithHighestStreetNumber(List<Apartment> list) {
                List<Apartment> temp = list;
                Comparator c = Comparator.comparing(Apartment::getStreetNumber).thenComparing(Apartment::getAddress);
                return Collections.max(list, c);
        }
        

        public static void main(String[] args) {

                Double[] doubles = { 10.2, 34.3, 2.5, 17.8 };
                System.out.println("minium double value is " + findMin(doubles));

                List<String> list1 = new ArrayList<>();
                list1.add("A");
                list1.add("C");
                list1.add("X");
                list1.add("Y");
                System.out.println("last word in lexical order is " + findLastStringInOrder(list1));

                List<Integer> list2 = new ArrayList<>();
                list2.add(10);
                list2.add(5);
                list2.add(3);
                list2.add(4);
                list2.add(8);
                System.out.println("Smallest even number is " + findSmallestEvenValue(list2));

                House h1 = new House(2, 1000, 30, 50, 4, "Address - 1");
                House h2 = new House(3, 1500, 20, 60, 5, "Address - 1");
                House h3 = new House(4, 1800, 40, 40, 6, null);
                House h4 = new House(2, 1000, 20, 50, 3, "Address - 1");
                //House h5 = new House(3, 1500, 20, 30, 7, "Address - 1");      
                //House h6 = new House(5, 2000, 50, 50, 5, null);

                List<House> houses = new ArrayList<>();
                houses.add(h1);
                houses.add(h2);
                houses.add(h3);
                houses.add(h4);

                Apartment a1 = new Apartment(100, 200, 20, 5, "Address-2");
                Apartment a2 = new Apartment(150, 200, 15, 3, "Address-2");
                Apartment a3 = new Apartment(50, 300, 25, 3, "Address-2");
                Apartment a4 = new Apartment(200, 100, 19, 1, "Address-2");

                List<Apartment> apartments = new ArrayList<>();
                apartments.add(a1);
                apartments.add(a2);
                apartments.add(a3);
                apartments.add(a4);

                List<LivingSpace> livingSpaceList = new ArrayList<>();
                livingSpaceList.add(new LivingSpace("house", 10, 20));
                livingSpaceList.add(new LivingSpace("Apartment", 100, 200));
                livingSpaceList.add(new LivingSpace("house", 30, 50));
                livingSpaceList.add(new LivingSpace("Apartment", 100, 50));
                
                sortListOfHousesBySP(houses);
                sortListOfApartments(apartments);
                        
                System.out.println("Houses after sorting by Selling price");
                System.out.println(houses);
                
                System.out.println("Apartments after sorting ");
                System.out.println(apartments);
                
                System.out.println("Maximum Living space area is "+maxLivingSpaceByArea(livingSpaceList));
                
                System.out.println("House with most bathrooms "+houseWithMostBathrooms(houses));
                
                System.out.println("Apartment with highest Street number is "+apartmentWithHighestStreetNumber(apartments));
                
        }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog