This is my assignment

Starting with last week's Property project, build an array of Properties in your main(). Add static methods to the Property class:

void ArraySort(Property[] property_list,  String key_field) 

This will sort the array, property_list, based on the argument, key_field, passed in. These are the legal choices for key_field:
"byPrice"
"byNumRooms"
"byDaysOnMarket"
any other String should cause nothing to be sorted, and a silent return results.

void PrintArray(String title, Property property_list[]) 

This will print the array, property_list, with a header line that contains the String title. No changes to the array should be attempted.

Additional static class finals such as PRICE, DAYS, etc. should be defined to hold the strings "byPrice", "byDaysOnMarket", etc., so that you do not have to use the literals "byPrice", etc. anywhere in your code. This will allow a future programmer to change each string by modifying these constants in one place and one place, only, namely in their static definitions.

For your test main(), create a an array of five or more Properties, then sort and list them, passing different sort keys each time

HINT: For those of you who are rusty on sorting (a topic from CIS 27a), you can review this topic in Week 3, which is open to you now. You will have to create a private instance method to the Property class, compareTo() with the signature.

int compareTo(Property prop, String key_field)

and have this method return a number that is < 0, = 0 or > 0 depending on whether the calling object, this, is <, = or > the parameter prop (based on the key_field criterion, of course).

I can't figure FloatLargestToTop and CompareTo part.
This is my code so far

// CIS 27B Assignment 1A Instructor's Solution
import javax.swing.*;
import java.util.Arrays;
public class Foothill
{
   public static void main(String[] args)
   { 
      Property[] Properties ;
      Properties = new Property[5] ;
      Properties[1]= new Property(Property.SFD, 999000, 2, 10, "Sheridan");
      Properties[2]=new Property(Property.CND, 650000, 2, 14, "100 El Camino");
      Properties[3]=new Property(Property.SFD, 999000, 2, 10, "Sheridan");
      Properties[4]=new Property();


   }
}

class Property
{
   // class constants
   public final static int RNT = 0;
   public final static int CND = 1;
   public final static int SFD = 2;
   public final static int MAX_ROOMS = 20;
   static final int MAX_PRICE = 10000000;
   public final static int CONVERSION_FACTOR = 300;
   public final static int MAX_ADDY_LENGTH = 500;
   public final static int MAX_DAYS_ON_MARKET = 365 * 5; // 5 years
   public final static String UNDEFINED = "[ undefined ]";  // for undefined strings
   static final String PRICE = "ByPrice";

   // private members
   private int type;
   private int price;
   private int bedrooms;
   private int days_on_market;
   private String address;

   static void ArraySort(Property[] property_list,  String PRICE)
   {
      for (int k = 0; k < property_list.length; k++)
         if ( !FloatLargestToTop(property_list, property_list.length-1-k) )
            return;
   }
   private static boolean FloatLargestToTop(Property[] data, int top)
   {
   }
   private int compareTo(Property prop, String PRICE)
   {
      return 0;
   }
   static void PrintArray(String title, Property property_list[])
   {

   }

   // constructors
   public Property()
   {
      InitDefaults();
   }
   public Property(int type, int price, int bedrooms, 
      int days_on_market, String address)
   {
      // set defaults by calling sibling constructor in case of bad parameters
      this();

      // use the mutators to filter bad data - do not repeat here
      SetType(type);
      SetPrice(price);
      SetBedrooms(bedrooms);
      SetDaysOnMarket(days_on_market);
      SetAddress(address);   
   }

   // mutators
   public boolean SetType(int type)
   {
      switch (type)
      {
         case RNT: case CND: case SFD:
            this.type = type;
            return true;
         default:
            return false;
      }
   }
   public boolean SetPrice(int price)
   {
      if ( price > 0 && price <= MAX_PRICE)
      {
         this.price = price;
         return true;
      }
      else 
         return false;
   }
   public boolean SetBedrooms(int bedrooms)
   {
      if (bedrooms > 0 && bedrooms < MAX_ROOMS)
      {
         this.bedrooms = bedrooms;
         return true;
      }
      else
         return false;
   }
   public boolean SetDaysOnMarket(int days_on_market)
   {
      if (days_on_market > 0 && days_on_market < MAX_DAYS_ON_MARKET)
      {
         this.days_on_market = days_on_market;
         return true;
      }
      else
         return false;
   }
   public boolean SetAddress(String address)
   {
      if (address.length() > 0 && address.length() < MAX_ADDY_LENGTH)
      {
         this.address = address;
         return true;
      }
      else
         return false;
   }

   // accessors
   public int GetType()
   {
      return type;
   }
   public int GetPrice()
   {
      return price;
   }
   public int GetBedrooms()
   {
      return bedrooms;
   }
   public int GetDaysOnMarket()
   {
      return days_on_market;
   }
   public String GetAddress()
   {
      return address;
   }

   // Stringizers
   public String StringizeType()
   {
      switch (type)
      {
         case RNT: 
            return "Rental";
         case CND: 
            return "Condo";
         case SFD:
            return "Single Family Dwelling";
         default:
            return "";
      }
   }

   public void ConvertToRental()
   {
      if (type != RNT)
      {
         type = RNT;
         days_on_market = 1;
         price = price / CONVERSION_FACTOR;
      }
   }

   public void PrintListing()
   {
      String return_string = "\n--- Listing ---\n";
      return_string = return_string + "Price: " + price + "\n";
      return_string = return_string + address + "\n";
      return_string = return_string + StringizeType() + "\n";
      return_string = return_string + "Number of Bedrooms: " + bedrooms + "\n";
      return_string = return_string + "Days On Market: " + days_on_market + "\n";

      System.out.println(return_string);
   }

   void InitDefaults()
   {
      type = SFD;
      price = bedrooms = days_on_market = 0;
      address = UNDEFINED;
   }

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I come up with this.
I can sort the address but if i change address to price, there's an error.
Any suggestion ?

public class test
{
   public static void main (String[] args)
   {
      Property[] Properties = 
      {
         new Property(12344,"San Jose"), 
         new Property(2222,"Olive"),
         new Property(3333,"Mountain View"), 
         new Property(4444,"Palo Altos"),
         new Property(5555,"Foothill"),
      };
  
      Property.PrintArrayConsole("before:", Properties);
      Property.ArraySort(Properties);
      Property.PrintArrayConsole("After:", Properties);

    }
}

class Property
{
 
   // print out array with string as a title for the message box
   public static void PrintArrayConsole(String title, Property[] data)
   {
      
      System.out.println("\n********** " + title + " **********\n");
      for (int k =0; k < data.length; k++)
         System.out.println(  data[k].price+"  " + data[k].address );
      System.out.println();
  }

   // returns true if a modification was made to the array
   private static boolean FloatLargestToTop(Property[] data, int top)
   {
      boolean changed = false;
      Property temp;
      
      // notice we stop at length -2 because of expr. k+1 in loop
      for (int k =0; k < top; k++)
         if (data[k].address.compareToIgnoreCase(data[k+1].address) > 0)
         {
            temp = data[k];
            data[k] = data[k+1];
            data[k+1] = temp;
            changed = true;
         }
       return changed;
   }
   
    public static void ArraySort(Property[] array)
    {
       for (int k = 0; k < array.length; k++)
          if ( !FloatLargestToTop(array, array.length-1-k) )
             return;
    
    }
    
    private int price;
    private String address;
    public final static int RNT = 0;
    public final static int CND = 1;
    public final static int SFD = 2;
    public final static int MAX_ROOMS = 20;
    static final int MAX_PRICE = 10000000;
    public final static int CONVERSION_FACTOR = 300;
    public final static int MAX_ADDY_LENGTH = 500;
    public final static int MAX_DAYS_ON_MARKET = 365 * 5; // 5 years
    public final static String UNDEFINED = "[ undefined ]";  // for undefined strings
    static final String PRICE = "ByPrice";
    public String GetAddress()
    {
       return address;
    }
    public boolean SetAddress(String address)
    {
       if (address.length() > 0 && address.length() < MAX_ADDY_LENGTH)
       {
          this.address = address;
          return true;
       }
       else
          return false;
    }
    public boolean SetPrice(int price)
    {
       if ( price > 0 && price <= MAX_PRICE)
       {
          this.price = price;
          return true;
       }
       else 
          return false;
    }
    public int GetPrice()
    {
       return price;
    }
    public Property()
    {
       price = 0;
       address = UNDEFINED;
    }
    public Property(int price,String address)
       {
          SetPrice(price);
          SetAddress(address);   
       }
}
********** before: **********

12344  San Jose
2222  Olive
3333  Mountain View
4444  Palo Altos
5555  Foothill


********** After: **********

5555  Foothill
3333  Mountain View
2222  Olive
4444  Palo Altos
12344  San Jose

Change address to price in FloatLargestToTop.
got this error

********** before: **********

12344  San Jose
2222  Olive
3333  Mountain View
4444  Palo Altos
5555  Foothill

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Cannot invoke compareToIgnoreCase(int) on the primitive type int

	at Property.FloatLargestToTop(test.java:42)
	at Property.ArraySort(test.java:55)
	at test.main(test.java:15)

The error is pretty clear:

Cannot invoke compareToIgnoreCase(int) on the primitive type int

String is an object, so it has methods that you can call, like: compareToIgnoreCase. But ints, as well as doubles, ... , are primitive types and don't have methods to call.
Why don't you use this:

private static boolean FloatLargestPriceToTop(Property[] data, int top)
   {
      boolean changed = false;
      Property temp;
      
      // notice we stop at length -2 because of expr. k+1 in loop
      for (int k =0; k < top; k++)
         if (data[k].price > data[k+1].price)
         {
            temp = data[k];
            data[k] = data[k+1];
            data[k+1] = temp;
            changed = true;
         }
       return changed;
   }
This article has been dead for over six months. Start a new discussion instead.