Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t figure out how to sort the arrays collected from a file with a buble or

ID: 3830824 • Letter: I

Question

I can't figure out how to sort the arrays collected from a file with a buble or selection sort.array() - assignment description below

Create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a file. Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.

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

//file content

Colorado 700000.00 Duplex
Texas 250000.00 Ranch
Phoenix 475000.00 Two-Story

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

//This program runs fine - doesn't need correction

import java.util.*;
import java.io.*;


public class RealEstate

{

String location;
double price;
String description;

   public RealEstate (String location, double price, String script)
   {
   super();
   this.location = location;
   this.price = price;
   description = script;
   }
  



   public String getLocation()
   {
   return location;
   }
   public void setLocation(String location)
   {
   this.location = location;
   }
   public double getPrice()
   {
   return price;
   }
   public void setLastname(double price)
{
   this.price = price;
   }
   public String getDescription()
   {
   return description;
   }
   public void setDescription(String script)
   {
   description = script;
   }
  

  
   @Override
   public String toString()
   {
   return "RealEstate [location=" + location + ", price=" + price
   + ", description=" + description + "]";
   }




}

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

//this program doesn't work as I don't know how to sort the arrays

import java.io.*;
import java.util.*;


public class RealestateFinder
{

   public static void main(String[] args)
   {


   String local;
   double prices;
   String des;
RealEstate rl =null;
Scanner sc=null;

ArrayList<RealEstate> arl=new ArrayList<RealEstate>();

  
File inFile = new File("realestatedata");

   try {
   sc = new Scanner (inFile);


   while (sc.hasNextLine())
   {
local = sc.next();
prices=Double.parseDouble(sc.next());
des = sc.next();

   rl = new RealEstate(local,prices,des);

arl.add(rl);
   }

   }
catch (FileNotFoundException e)
{

   e.printStackTrace();
}
finally
   {
   sc.close();
  
}
   int number[] ;
   int temp;
   boolean fixed = false;
  
while(fixed == false)
{
   fixed = true;
   for (int i = 0; i < number.length -1 ; i ++)
{
if (number[i] > number [ i +1 ])
{
   temp = number[i+1];
   number [i +1] = number[i];
   number[i] = temp;
   fixed = false;
}


}

   }

   }

}

Explanation / Answer

realestatedata.txt (Save this file under D Drive.Then the path of the file pointing to it is D: ealestatedata.txt)

Colorado 700000.00 Duplex
Texas 250000.00 Ranch
Phoenix 475000.00 Two-Story

________________________

RealEstate.java

public class RealEstate {
   String location;
   double price;
   String description;

   public RealEstate(String location, double price, String script) {
       super();
       this.location = location;
       this.price = price;
       description = script;
   }

   public String getLocation() {
       return location;
   }

   public void setLocation(String location) {
       this.location = location;
   }

   public double getPrice() {
       return price;
   }

   public void setLastname(double price) {
       this.price = price;
   }

   public String getDescription() {
       return description;
   }

   public void setDescription(String script) {
       description = script;
   }

   @Override
   public String toString() {
       return "RealEstate [location=" + location + ", price=" + price+ ", description=" + description + "]";
   }

}

_____________________

SortByPrice.java

import java.util.Comparator;

public class SortByPrice implements Comparator<RealEstate> {

   @Override
   public int compare(RealEstate re1, RealEstate re2) {
      
       int val= 0;

       if(re1.getPrice() < re2.getPrice()){
       val = -1;
       }else if(re1.getPrice() > re2.getPrice()){
       val = 1;
       }else if(re2.getPrice() == re2.getPrice()){
       val = 0;
       }
       return val;

       }
   }
  

___________________________

SortPerLocation.java

import java.util.Comparator;

public class SortPerLocation implements Comparator<RealEstate> {

   @Override
   public int compare(RealEstate re1, RealEstate re2) {
      
       return re1.getLocation().compareTo(re2.getLocation());
   }

}

_________________________

RealestateFinder.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class RealestateFinder {

   public static void main(String[] args) {
       String local;
       double prices;
       String des;
       RealEstate rl = null;
       Scanner sc = null;

       ArrayList<RealEstate> arl = new ArrayList<RealEstate>();

       File inFile = new File("D: ealestatedata.txt");
       try {

           sc = new Scanner(inFile);

           while (sc.hasNextLine()) {
               local = sc.next();
               prices = Double.parseDouble(sc.next());
               des = sc.next();

               rl = new RealEstate(local, prices, des);

               arl.add(rl);
           }

       } catch (FileNotFoundException e) {

           e.printStackTrace();
       } finally {
           sc.close();

       }

       sc = new Scanner(System.in);
       int choice;

       while (true) {
           System.out.println("1.Sort per Price.");
           System.out.println("2.Sort per Location.");

           System.out.print("Enter Choice :");
           choice = sc.nextInt();

           switch (choice) {
           case 1: {
               Collections.sort(arl, new SortByPrice());
               System.out
                       .println(" ___________Sorting based on Price __________");
               for (RealEstate re : arl) {
                   System.out.println(re.getLocation() + " " + re.getPrice()
                           + " " + re.getDescription());
               }
               break;
           }
           case 2: {
               Collections.sort(arl, new SortPerLocation());
               System.out
                       .println(" ___________Sorting based on Location __________");
               for (RealEstate re : arl) {
                   System.out.println(re.getLocation() + " " + re.getPrice()
                           + " " + re.getDescription());
               }
               break;
           }
           default: {
               System.out.println("** Invalid.Enter valid Input **");
               continue;
           }
           }
           break;
       }
      

   }

}

______________________

Output:

1.Sort per Price.
2.Sort per Location.
Enter Choice :2


___________Sorting based on Location __________
Colorado 700000.0 Duplex
Phoenix 475000.0 Two-Story
Texas 250000.0 Ranch

_______________________

output#2:

1.Sort per Price.
2.Sort per Location.
Enter Choice :1


___________Sorting based on Price __________
Texas 250000.0 Ranch
Phoenix 475000.0 Two-Story
Colorado 700000.0 Duplex

_________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote