Results 1 to 10 of 10

Thread: What can I do to make this generic sorting method work?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    57

    What can I do to make this generic sorting method work?

    Hello. This question is kind of long... so bear with me please.

    I have to convert a SelectionSort method that was covered in my book that was built to sort arrays, and make it generic so that I can enter either doubles or ints into it and have it work.

    It doesn't seem to allow generic arrays, so I'm attempting to use an ArrayList. The problem here is since the int and doubles are now in Integer and Double wrappers, it breaks the SelectionSort method.

    I've attempted to fix it, but I'm having no luck. I'll post the original SelectionSort method below, and then the class and driver that I'm creating.

    Original SelectionSort:
    Code:
    public class SelectionSort {
    	private int[] data;
    	private static final Random generator = new Random();
    	
    	public SelectionSort(int size) {
    		data = new int[size];
    		
    		for(int i = 0; i < size; i++) {
    			data[i] = 10 + generator.nextInt(90);
    		}
    	}
    	
    	public void sort() {
    		int smallest;
    		
    		for(int i = 0; i < data.length - 1; i++) {
    			smallest = i;
    			
    			for(int index = i + 1; index < data.length; index++) {
    				if(data[index] < data[smallest]) {
    					smallest = index;
    				}
    			}
    			
    			swap(i, smallest);
    		}
    	}
    	
    	public void swap(int first, int second) {
    		int temporary = data[first];
    		data[first] = data[second];
    		data[second] = temporary;
    	}
    	
    }
    My simple driver program:
    Code:
    public class GenericsDriver {
    
    	public static void main(String[] args) {
    		
    		SelectionSort<Integer> intSort = new SelectionSort<Integer>();
    		
    		intSort.AddGrade(100);
    		intSort.AddGrade(90);
    		intSort.AddGrade(50);
    		intSort.AddGrade(80);
    		intSort.AddGrade(95);
    		
    		intSort.PrintNumbers();
    		
    		//sort here
    		
    		intSort.PrintNumbers();
    		
    		SelectionSort<Double> doubleSort = new SelectionSort<Double>();
    		
    		doubleSort.AddGrade(100.1);
    		doubleSort.AddGrade(90.4);
    		doubleSort.AddGrade(50.7);
    		doubleSort.AddGrade(100.2);
    		doubleSort.AddGrade(100.5);
    		
    		doubleSort.PrintNumbers();
    		
    		//sort here
    		
    		doubleSort.PrintNumbers();
    
    	}
    
    }
    The new class and my attempt to repurpose the SelectionSort method:
    Code:
    import java.util.*;
    
    public class SelectionSort <T> {
    
    	private Array<T> numbers;
    	
    	public SelectionSort() {
    		numbers = new ArrayList<T>();
    	}
    	
    	public void AddGrade(T number) {
    		numbers.add(number);
    	}
    	
    	public void PrintNumbers() {
    		System.out.println(numbers.toString());
    
    	}
    	
    	public <T extends Comparable<T>> selectionSort() {
    		int smallest;
    		
    		for(int  i = 0; i < numbers.size(); i++) {
    			smallest = i;
    			
    			for(int index = i + 1; index < numbers.size(); index++) {
    				if(numbers.) {
    					
    				}
    			}
    		}
    	}
    	
    	public void swap(int first, int second) {
    		
    	}
    	
    }
    As you can see... I haven't had any luck with sort or swap within my new class. I can't get it to work. My instructions have a hint that I should use <T extends Comparable<T>> in my sort method...but nothing is giving me the ability to use a .compareTo() method.

    Could someone please give me some guidance here? Thanks.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What can I do to make this generic sorting method work?

    How are you sorting the array?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    57

    Re: What can I do to make this generic sorting method work?

    I posted the code. There's a sort method in the first block on code. I have to repurpose it to sort int OR doubles from least to greatest, as it's doing in the original sort method.

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What can I do to make this generic sorting method work?

    Shouldn't that be "implements" comparable instead of "extends"? Also You need to add a "compareto" function to the class. I have examples I could post if you want me to?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    57

    Re: What can I do to make this generic sorting method work?

    It isn't allowing implements in this case. Plus my instructions say to use extends.

    If you have examples, then yes, I would absolutely love for you to post them. I'm struggling with figuring this out and it's important that I get it to work.

    Thanks for you responses.

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What can I do to make this generic sorting method work?

    Collections demo:

    java Code:
    1. //demo of various algorithms in Collections class
    2. import java.util.*;
    3.  
    4. public class CollectionsDemo {
    5.  
    6.   @SuppressWarnings("unchecked")
    7.   public static void main(String args[]) {
    8.  
    9.  
    10.     // create and initialize a linked list
    11.     List<Integer> aList = new LinkedList<Integer>();
    12.  
    13.     aList.add(new Integer( -8));
    14.     aList.add(new Integer(20));
    15.     aList.add(new Integer( -20));
    16.     aList.add(new Integer(8));
    17.  
    18.     Collections.sort(aList);
    19.     System.out.println(aList);
    20.  
    21.     // create a reverse order comparator
    22.     Comparator<Integer> r;
    23.     r = Collections.reverseOrder();
    24.     // sort list by using the comparator
    25.     Collections.sort(aList, r);
    26.     System.out.println(aList);
    27.  
    28.     Collections.shuffle(aList);
    29.     System.out.println(aList);
    30.  
    31.     System.out.println("Minimum: " + Collections.min(aList));
    32.     System.out.println("Maximum: " + Collections.max(aList));
    33.  
    34.     // Example of using a Comparator class that we create to enable a sort in particular order
    35.     // (age order in this example)
    36.     List<Person> personList;
    37.     personList = new LinkedList<Person>();
    38.  
    39.     personList.add(new Person("Aaron", "Spehr", 10,
    40.             new Address("stnum1","stname1","sub1","SA","AUS","5050"), "",new PhoneNumbers()));
    41.     personList.add(new Person("Bill", "Jones", 45,
    42.             new Address("stnum2","stname1","sub1","SA","AUS","5043"), "",new PhoneNumbers()));
    43.     personList.add(new Person("Scott", "Welsh", 6,
    44.             new Address("stnum1","stname1","sub1","SA","AUS","5049"), "",new PhoneNumbers()));
    45.     personList.add(new Person("Scott", "Welsh", 15,
    46.             new Address("stnum1","stname1","sub1","SA","AUS","5050"), "",new PhoneNumbers()));
    47.  
    48.     Collections.sort(personList);
    49.     System.out.println(personList);
    50.     Collections.sort(personList, new AgeComparator());
    51.     System.out.println("\n IN AGE ORDER - \n" + personList);
    52.  
    53.     Collections.sort(personList, new AddressComparator());
    54.     System.out.println("\n IN ADDRESS ORDER - \n" + personList);
    55.  
    56.   }
    57. }
    58.  
    59. /**
    60.  *
    61.  * <p>Description: Class that allows Person objects to be compared based on the persons age</p>
    62.  * @author Santi Ruiz
    63.  * @version 1
    64.  */
    65. class AgeComparator  implements Comparator
    66. {
    67.   public int compare(Object object1, Object object2) {
    68.     Person p1;
    69.     Person p2;
    70.     int result;
    71.  
    72.     p1 = (Person)object1;
    73.     p2 = (Person)object2;
    74.     if (p1.getAge() > p2.getAge()){
    75.       result = 1;
    76.     }
    77.     else if (p1.getAge() < p2.getAge()){
    78.       result = -1;
    79.     }
    80.     else{
    81.       result = 0;
    82.     }
    83.  
    84.     return result;
    85.   }
    86.  
    87. }
    88.  
    89. class AddressComparator  implements Comparator
    90. {
    91.   public int compare(Object object1, Object object2) {
    92.     Person p1;
    93.     Person p2;
    94.     int result;
    95.  
    96.     p1 = (Person)object1;
    97.     p2 = (Person)object2;
    98.  
    99.     return p1.getAddress().compareTo(p2.getAddress());
    100.   }
    101.  
    102.   class PersonAndAddressComparator  implements Comparator
    103. {
    104.     @Override
    105.   public int compare(Object object1, Object object2) {
    106.     Person p1;
    107.     Person p2;
    108.     int result;
    109.  
    110.     p1 = (Person)object1;
    111.     p2 = (Person)object2;
    112.  
    113.     result = p1.compareTo(p2);
    114.     if (result == 0){
    115.         result = p1.getAddress().compareTo(p2.getAddress());
    116.   }
    117.     return result;
    118.   }
    119.   }
    120.  
    121. }

    Person:

    java Code:
    1. /**
    2.  * This version of Person class has the following features
    3.  *  - private instance variables
    4.  *  - three constructors no-arg, all argument and key argument (assumed to
    5.  *      be surname+firstname)
    6.  *  -   Symbolic constants
    7.  *  - Uses this keyword for the instance variables
    8.  *  - Has a customised toString method
    9.  *  - Uses setter and getters
    10.  *  - Uses the this keyword to call one constructor from another
    11.  *  - Includes an Address object
    12.  *  - Contains a main method as a test-harness
    13.  *  -
    14.  */
    15.  
    16. import java.io.*;
    17.  
    18. public class Person implements Comparable {
    19.  
    20.   /*-----------------------------------------------------------------------*/
    21.   public static final String DEFAULT_FIRSTNAME = "No FNAME";
    22.   public static final String DEFAULT_SURNAME = "No SNAME";
    23.   public static final int DEFAULT_AGE = 0;
    24.   public static final Address DEFAULT_ADDRESS = Address.DEF_ADDRESS;
    25.   public static final Person DEFAULT_PERSON = new Person();
    26.   public static final String DEFAULT_DOB = "No birthdate";
    27.   public static final PhoneNumbers DEFAULT_PHONE = new PhoneNumbers();
    28.  
    29.   /*-----------------------------------------------------------------------*/
    30.   private String firstName;
    31.   private String surname;
    32.   private int age;
    33.   private Address address;
    34.   private String birthDate;
    35.   private PhoneNumbers phoneNumbers;
    36.  
    37.   /*-----------------------------------------------------------------------*/
    38.   public Person() {
    39.     this(DEFAULT_FIRSTNAME, DEFAULT_SURNAME, DEFAULT_AGE, new Address(),
    40.       DEFAULT_DOB, DEFAULT_PHONE);
    41.   }
    42.  
    43.   /*-----------------------------------------------------------------------*/
    44.   public Person(String firstName, String surname) {
    45.     this(firstName, surname, DEFAULT_AGE, DEFAULT_ADDRESS, DEFAULT_DOB,
    46.       DEFAULT_PHONE);
    47.   }
    48.  
    49.   /*-----------------------------------------------------------------------*/
    50.   public Person(String firstName, String surname, int age) {
    51.     this(firstName, surname, age, DEFAULT_ADDRESS, DEFAULT_DOB, DEFAULT_PHONE);
    52.   }
    53.  
    54.   /*-----------------------------------------------------------------------*/
    55.   public Person(String firstName, String surname, Address a) {
    56.     this(firstName, surname, DEFAULT_AGE, a, DEFAULT_DOB, DEFAULT_PHONE);
    57.   }
    58.  
    59.   /*-----------------------------------------------------------------------*/
    60.   public Person(String firstName, String surname, String dob, Address a,
    61.     PhoneNumbers ph) {
    62.     this(firstName, surname, DEFAULT_AGE, a, dob, ph);
    63.   }
    64.  
    65.   /*-----------------------------------------------------------------------*/
    66.   public Person(String firstName, String surname, int age,
    67.     Address a, String dob, PhoneNumbers ph) {
    68.     this.firstName = firstName;
    69.     this.surname = surname;
    70.     this.age = age;
    71.     this.address = a;
    72.     this.birthDate = dob;
    73.     this.phoneNumbers = ph;
    74.   }
    75.  
    76.   public Person(Person p) {
    77.     this(p.getFirstName(), p.getSurname(), p.getAge(), p.getAddress(),
    78.       p.getBirthDate(), p.getPhoneNumbers());
    79.   }
    80.  
    81.   /*-----------------------------------------------------------------------*/
    82.   public String getFirstName() {
    83.     return (this.firstName);
    84.   }
    85.  
    86.   public String getSurname() {
    87.     return (this.surname);
    88.   }
    89.  
    90.   public Address getAddress() {
    91.     return (this.address);
    92.   }
    93.  
    94.   public int getAge() {
    95.     return (this.age);
    96.   }
    97.  
    98.   /*-----------------------------------------------------------------------*/
    99.   public void setAddress(Address p) {
    100.     this.address = new Address(p);
    101.   }
    102.  
    103.   public void setFirstName(String firstName) {
    104.     this.firstName = firstName;
    105.     return;
    106.   }
    107.  
    108.   public void setSurname(String surname) {
    109.     this.surname = surname;
    110.     return;
    111.   }
    112.  
    113.   public void setAge(int age) {
    114.     this.age = age;
    115.     return;
    116.   }
    117.  
    118.   /*-----------------------------------------------------------------------*/
    119.   public void writeToBinaryFile(DataOutputStream dataFile) throws IOException {
    120.    
    121.     dataFile.writeUTF(this.firstName);
    122.     dataFile.writeUTF(this.surname);
    123.     dataFile.writeInt(this.age);
    124.     this.address.writeToFile(dataFile);
    125.   }
    126.  
    127.   /*-----------------------------------------------------------------------*/
    128.   public void readFromBinaryFile(DataInputStream dataFile) throws IOException {
    129.    
    130.     this.firstName = dataFile.readUTF();
    131.     this.surname = dataFile.readUTF();
    132.     this.age = dataFile.readInt();
    133.     this.address.readFromFile(dataFile);
    134.     ;
    135.   }
    136.  
    137.   /*-----------------------------------------------------------------------*/
    138.   public String toString() {
    139.     String result;
    140.    
    141.     result = "\n[" +
    142.       super.toString() +
    143.       " ,firstname= " + this.firstName +
    144.       " ,surname= " + this.surname +
    145.       " ,age= " + this.age +
    146.       " ,Address= " + this.address +
    147.       " ,BirthDate=" + this.birthDate +
    148.       " ]";
    149.    
    150.     return result;
    151.   }
    152.  
    153.   /*-----------------------------------------------------------------------*/
    154.   /**
    155.    * Define key for Person as surname and firstname
    156.    * @param p - the Person object being compared to
    157.    * @return true of p is equal to this object
    158.    */
    159.   public int compareTo(Object o) {
    160.     Person person;
    161.     int result;
    162.     String targetKey;
    163.     String thisKey;
    164.    
    165.     person = (Person) o;
    166.     targetKey = person.surname + person.firstName;
    167.     thisKey = this.surname + this.firstName;
    168.     return thisKey.compareTo(targetKey);
    169.   }
    170.  
    171.   /*-----------------------------------------------------------------------*/
    172.   /**
    173.    * Define equality as surname and firstname being the same
    174.    * @param p - the Person object being compared to
    175.    * @return true of p is equal to this object
    176.    */
    177.   public boolean equals(Object p) {
    178.     Person person;
    179.     boolean isEqual;
    180.    
    181.     if (! (p instanceof Person)) {
    182.       isEqual = false;
    183.     } else {
    184.       person = (Person) p;
    185.       isEqual = (this.surname.equals(person.surname) &&
    186.         this.firstName.equals(person.firstName));
    187.     }
    188.     return isEqual;
    189.   }
    190.  
    191.   /*-----------------------------------------------------------------------*/
    192.   /**
    193.    * Clones a Person object or any sub-class of Person
    194.    * @return - A shallow clone of a this object
    195.    */
    196.   public Object clone() {
    197.     Object o = null;
    198.     try {
    199.       o = super.clone();
    200.     } catch (CloneNotSupportedException e) {
    201.       System.out.println("INTERNAL SYSTEM ERROR - see adminisrator");
    202.       e.printStackTrace();
    203.     }
    204.     return o;
    205.   }
    206.  
    207.   public void setBirthDate(String newBirthDate) {
    208.     birthDate = newBirthDate;
    209.   }
    210.  
    211.   public String getBirthDate() {
    212.     return birthDate;
    213.   }
    214.  
    215.   public void setPhoneNumbers(PhoneNumbers newPhoneNumbers) {
    216.     phoneNumbers = newPhoneNumbers;
    217.   }
    218.  
    219.   public PhoneNumbers getPhoneNumbers() {
    220.     return phoneNumbers;
    221.   }
    222.  
    223.   public int hashCode(){
    224.    
    225.     String key;
    226.    
    227.     key = this.surname.toUpperCase() + this.firstName.toUpperCase();
    228.     return (key.hashCode());
    229.   }
    230.  
    231. }
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What can I do to make this generic sorting method work?

    PhoneNumbers:
    java Code:
    1. import java.util.StringTokenizer;
    2. import java.io.*;
    3.  
    4. public class PhoneNumbers {
    5.  
    6.   private String homePhoneNumber;
    7.   private String workPhoneNumber;
    8.   private String mobilePhoneNumber;
    9.  
    10.   public static final String DEF_HOME_PHONE_NUMBER ="No home number";
    11.   public static final String DEF_WORK_PHONE_NUMBER ="No work number";
    12.   public static final String DEF_MOBILE_PHONE_NUMBER ="No mobile number";
    13.  
    14.  
    15.   public PhoneNumbers(String hPhone, String wPhone,String mPhone) {
    16.  
    17.   /*  this.homePhoneNumber = MyUtils.checkForNulls(hPhone,DEF_HOME_PHONE_NUMBER) ;
    18.     this.workPhoneNumber = MyUtils.checkForNulls(wPhone,DEF_WORK_PHONE_NUMBER);
    19.     this.mobilePhoneNumber=MyUtils.checkForNulls(mPhone,DEF_MOBILE_PHONE_NUMBER);
    20.     */}
    21.  
    22.   public PhoneNumbers(){
    23.      this(DEF_HOME_PHONE_NUMBER, DEF_WORK_PHONE_NUMBER, DEF_MOBILE_PHONE_NUMBER);
    24.     }
    25.  
    26. //getter and Setters-------------------------------------------
    27.    public String getHomePhoneNumber() {
    28.     return this.homePhoneNumber;
    29.   }
    30.  
    31.   public String getWorkPhoneNumber() {
    32.     return this.workPhoneNumber;
    33.   }
    34.  
    35.   public String getMobilePhoneNumber() {
    36.     return this.mobilePhoneNumber;
    37.   }
    38.  
    39.     public void setHomePhoneNumber(String homePhoneNumber) {
    40.     this.homePhoneNumber = homePhoneNumber;
    41.   }
    42.  
    43.   public void setWorkPhoneNumber(String workPhoneNumber) {
    44.     this.workPhoneNumber = workPhoneNumber;
    45.   }
    46.  
    47.   public void setMobilePhoneNumber(String mobilePhoneNumber) {
    48.     this.mobilePhoneNumber = mobilePhoneNumber;
    49.   }
    50.  
    51. //Methods------------------------------------------------------
    52.   public String toString() {
    53.   return ""+homePhoneNumber+"\t"+workPhoneNumber+"\t"+mobilePhoneNumber;
    54.   }
    55.  
    56.   public static PhoneNumbers readFromTextFile(BufferedReader br) throws IOException {
    57.     StringTokenizer phoneTok = new StringTokenizer(br.readLine(),"\t");
    58.     PhoneNumbers phones = new PhoneNumbers(phoneTok.nextToken(), phoneTok.
    59.                                             nextToken(),phoneTok.nextToken());
    60.     return phones;
    61.   }
    62.  
    63.   public void writeToTextFile(PrintWriter pw) {
    64.     pw.println(this);
    65.   }
    66. }

    Addresses:

    java Code:
    1. /**
    2.  *  The <code>Address</code> class stores postal address data.
    3.  * @author  Aaron Spehr
    4.  * @version 1
    5.  * @since   March 2002
    6.  *
    7.  */
    8. import java.io.*;
    9.  
    10. public class Address implements Comparable {
    11.  
    12. //---------------------- CONSTANTS ---------------------------------------------
    13.   public static final Address DEF_ADDRESS = new Address();
    14.   public static final String DEF_STREET_NUMBER = "NOT SET";
    15.   public static final String DEF_STREET_NAME = "NOT SET";
    16.   public static final String DEF_SUBURB = "NOT SET";
    17.   public static final String DEF_STATE = "S.A.";
    18.   public static final String DEF_COUNTRY = "Australia";
    19.   public static final String DEF_POSTCODE = "NOT SET";
    20. //---------------------- INSTANCE VARIABLES-------------------------------------
    21.   private String streetNumber;
    22.   private String streetName;
    23.   private String suburb;
    24.   private String state;
    25.   private String country;
    26.   private String postcode;
    27.  
    28. //---------------------- CONSTRUCTORS ------------------------------------------
    29.   public Address() {
    30.     this(DEF_STREET_NUMBER, DEF_STREET_NAME, DEF_SUBURB,
    31.             DEF_STATE, DEF_COUNTRY, DEF_POSTCODE);
    32.   }
    33.  
    34. //------------------------------------------------------------------------------
    35.   public Address(Address a) {
    36.     this(a.getStreetNumber(), a.getStreetName(), a.getSuburb(), a.getState(),
    37.             a.getCountry(), a.getPostcode());
    38.   }
    39.  
    40. //------------------------------------------------------------------------------
    41.   public Address(String streetNumber, String streetName, String suburb,
    42.           String state, String country, String postcode) {
    43.     this.streetNumber = streetNumber;
    44.     this.streetName = streetName;
    45.     this.suburb = suburb;
    46.     this.state = state;
    47.     this.country = country;
    48.     this.postcode = postcode;
    49.   }
    50.  
    51. //-------------------------- GETTERS -------------------------------------------
    52.   public String getStreetNumber() {
    53.     return this.streetNumber;
    54.   }
    55.  
    56.   public String getStreetName() {
    57.     return this.streetName;
    58.   }
    59.  
    60.   public String getSuburb() {
    61.     return this.suburb;
    62.   }
    63.  
    64.   public String getState() {
    65.     return this.state;
    66.   }
    67.  
    68.   public String getCountry() {
    69.     return this.country;
    70.   }
    71.  
    72.   public String getPostcode() {
    73.     return this.postcode;
    74.   }
    75.  
    76. //------------------------ SETTERS --------------------------------------------
    77.   public Address setCountry(String country) {
    78.     this.country = country;
    79.     return this;
    80.   }
    81.  
    82.   public Address setPostcode(String postcode) {
    83.     this.postcode = postcode;
    84.     return this;
    85.   }
    86.  
    87.   public Address setStreetName(String streetName) {
    88.     this.streetName = streetName;
    89.     return this;
    90.   }
    91.  
    92.   public Address setStreetNumber(String streetNumber) {
    93.     this.streetNumber = streetNumber;
    94.     return this;
    95.   }
    96.  
    97.   public Address setState(String state) {
    98.     this.state = state;
    99.     return this;
    100.   }
    101.  
    102.   public Address setSuburb(String suburb) {
    103.     this.suburb = suburb;
    104.     return this;
    105.   }
    106.  
    107.   public void writeToFile(DataOutputStream theFile) throws IOException {
    108.     theFile.writeUTF(this.streetNumber);
    109.     theFile.writeUTF(this.streetName);
    110.     theFile.writeUTF(this.suburb);
    111.     theFile.writeUTF(this.state);
    112.     theFile.writeUTF(this.country);
    113.     theFile.writeUTF(this.postcode);
    114.   }
    115.  
    116.   public void readFromFile(DataInputStream theFile) throws IOException {
    117.     this.streetNumber = theFile.readUTF();
    118.     this.streetName = theFile.readUTF();
    119.     this.suburb = theFile.readUTF();
    120.     this.state = theFile.readUTF();
    121.     this.country = theFile.readUTF();
    122.     this.postcode = theFile.readUTF();
    123.   }
    124.  
    125.   //---------------------------- STANDARD OBJECT METHODS --------------------------------
    126.   public String toString() {
    127.     return ("\n[" +
    128.             super.toString() +
    129.             ",streetNumber= " + this.streetNumber +
    130.             ",streetName= " + this.streetName +
    131.             ",suburb= " + this.suburb +
    132.             ",state= " + this.state +
    133.             ",postcode= " + this.postcode +
    134.             ",country= " + this.country +
    135.             "]");
    136.   }
    137.  
    138.  
    139.   //---------------------------- COMPARABLE INTERFACE METHODS --------------------------------
    140.   public int compareTo(Object obj) {
    141.     // compareTo should return a 0 if this is equal to the oobject passed in obj
    142.     // -1 if this is less than (<) than obj and +1 if this is greater than (>)
    143.     // obj
    144.     Address otherAddress;
    145.  
    146.     otherAddress = (Address) obj;
    147.  
    148.     // A simple but poor compareTo would be based on postcode. This, of course
    149.     // would cause many Address objects to be considered equal (all addresses
    150.     // from the same suburb). UnComment the following line if you wish to try
    151.     // out the demo programs using postcode as the basis for comparing Address
    152.     // objects
    153.     // return (postcode.compareTo(otherAddress.getPostcode()));
    154.  
    155.     // A more sensible comparison would be based on street number and street
    156.     // name and postcode although the definition of "<" and ">" has little
    157.     // real meaning
    158.  
    159.     String thisKey;
    160.     String otherKey;
    161.     int result;
    162.  
    163.     thisKey = this.postcode;
    164.     otherKey = otherAddress.postcode;
    165.     result = thisKey.compareTo(otherKey);
    166.     if (result == 0) {
    167.       thisKey = this.streetName;
    168.       otherKey = otherAddress.streetName;
    169.       result = thisKey.compareTo(otherKey);
    170.       if (result == 0) {
    171.         thisKey = this.streetNumber;
    172.         otherKey = otherAddress.streetNumber;
    173.         result = thisKey.compareTo(otherKey);
    174.       }
    175.     }
    176.  
    177.     return result;
    178.   }
    179.  
    180.  
    181.   //---------------------------- METHODS REQUIRED FOR HASHING ------------
    182.   public boolean equals(Object obj) {
    183.     Address otherAddress = (Address) obj;
    184.     return (postcode.equals(otherAddress.getPostcode()) &&
    185.             streetName.equalsIgnoreCase(otherAddress.getStreetName()) &&
    186.             streetNumber.equals(otherAddress.getStreetNumber()));
    187.   }
    188.  
    189.   /* The following method is needed to get any classes that use hashing
    190.    * (hashSet or hashMap) to work properly.
    191.    *
    192.    */
    193.   public int hashCode() {
    194.     // NOTE : it is quite common for classes to create a String from the data
    195.     // and then rely on the String classes hashCode method for the hash code
    196.     String thisHashStr;
    197.  
    198.     thisHashStr = this.getStreetNumber() + this.getStreetName() + this.getPostcode();
    199.  
    200.     return (thisHashStr.hashCode());
    201.  
    202.   }
    203. }

    All that code is for the same project! Not sure if it will be helpful or not?

    Edit:

    I found this which, looks like what you are trying to accomplish.
    Last edited by Nightwalker83; Nov 17th, 2012 at 08:56 PM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    57

    Re: What can I do to make this generic sorting method work?

    Well... I don't know if it will help just yet, but I will read over it, and I appreciate the responses. The main trouble here tweaking the prewritten sort algorithm for my purposes as I'm instructed to do. It's hard to find an example that demonstrates something that specific.

    Would you be interested in hearing the directions my book gives me?

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: What can I do to make this generic sorting method work?

    Quote Originally Posted by English Fire View Post
    Would you be interested in hearing the directions my book gives me?
    Yes, that could help in determining what you are suppose to achieve and allow other perspective helpers to know what needs to be done.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2011
    Posts
    57

    Re: What can I do to make this generic sorting method work?

    Here's the complete instructions. It's going over my head.

    Write a generic method selectionSort based on the sort program of Fig 19.6 and 19.7 (That's the code I gave above). Write a test program that inputs, sorts and outputs an Integer array and a Float array. Hint: Use <T extents Comparable<T>> in the type-parameter section for method selectionSort, so that you can use method compareTo() to compare the objects of the type that T represents.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width