Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help SORTING AN ARRAY

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help SORTING AN ARRAY

    I am a beginner level java programmer. I have three class files:
    AddressBook.class
    BanffMarathonRunnerDemo.class
    BanffMarathonRunner.class

    BanffMarathonRunner extends the AddressBook class. And BanffMarathonRunnerDemo creates the array and calls BanffMarathonRunner.

    I have made numerous attempts to sort my array by Time in BanffMarathonRunner. But can not seem to figure it out. Can someone show me how this is done?



    public class BanffMarathonRunnerDemo {

    public static void main(String[] args) {

    BanffMarathonRunner[] runners = new BanffMarathonRunner[15];

    runners[0] = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
    runners[1] = new BanffMarathonRunner("Thomas", "Molson", 273, 2);
    runners[2] = new BanffMarathonRunner("Hamilton", "Winn", 278, 5);
    runners[3] = new BanffMarathonRunner("Suzie", "Sarandin", 329, 7);

    BanffMarathonRunner firstplace = BanffMarathonRunner.getFastestRunner(runners);
    printWinner(firstplace);

    } // End of main

    static void printWinner(BanffMarathonRunner runner) {
    System.out.println("Banff Marathon Winner");
    System.out.println(runner.getFirstName() + " " + runner.getLastName());
    System.out.println(runner.getTime());
    }

    } // End of class BanffMarathonRunnerDemo





    public class BanffMarathonRunner extends AddressBook {
    private int time;
    private int years;

    public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
    super(firstName);
    super.setLastName(lastName);
    this.time = min;
    this.years = yr;}

    public int getTime() {return this.time;}
    public int getYears() {return this.years;}
    public void setTime(int time) {this.time = time;}
    public void setYears(int years) {this.years = years;}

    public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
    BanffMarathonRunner fastestRunner = runners[0];

    // SORT ARRAY HERE BY TIME
    return fastestRunner;
    }
    } // End of BanffMarathonRunner

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help SORTING AN ARRAY

    Where is the code for doing the sorting?
    There are many types of sorts,
    What algorithm are you trying to implement?

    Does the code need a sort or just a search?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help SORTING AN ARRAY

    Sorry about that. I need to search for the lowest integer (time) and the second lowest integer (time) in the array. But I was thinking I could sort them to get it.

    Here's the BanffMarathonRunnerDemo with the array

     
    public class BanffMarathonRunnerDemo {
     
        public static void main(String[] args) {
     
            BanffMarathonRunner[] runners = new BanffMarathonRunner[15]; 
     
            	runners[0] = new BanffMarathonRunner("Elena", "Brandon", 341, 1); 
            	runners[1] = new BanffMarathonRunner("Thomas", "Molson", 273, 2);
            	runners[2] = new BanffMarathonRunner("Hamilton", "Winn", 278, 5);
            	runners[3] = new BanffMarathonRunner("Suzie", "Sarandin", 329, 7);
     
            	BanffMarathonRunner firstplace = BanffMarathonRunner.getFastestRunner(runners); 
            	printWinner(firstplace); 
     
        } //  End of main
     
    static void printWinner(BanffMarathonRunner runner) {
    	System.out.println("Banff Marathon Winner");
    	System.out.println(runner.getFirstName() + " " + runner.getLastName()); 
        	System.out.println(runner.getTime()); 
        } 
     
    } // End of class BanffMarathonRunnerDemo

    And here's the BanffMarathonRunner code

     
    public class BanffMarathonRunner extends AddressBook {
        private int time; 
        private int years; 
     
    public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
        super(firstName);
        super.setLastName(lastName);
        this.time = min;
        this.years = yr;}
     
    public int getTime() {return this.time;}
    public int getYears() {return this.years;}
    public void setTime(int time) {this.time = time;}
    public void setYears(int years) {this.years = years;}
     
    public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
    	BanffMarathonRunner fastestRunner = runners[0];
                      //  SORT ARRAY HERE BY TIME
    	return fastestRunner;
        	} 
    }

    I looked at endless examples online but could not figure it out. Here was one of my approaches.

     
     
    import java.util.ArrayList; 
    import java.util.Collections; 
     
      public ArrayList<JobCandidate> getSortedJobCandidateByAge() {         
        Collections.sort(jobCandidate, JobCandidate.ageComparator);         
        return jobCandidate;     
      }

    This, of course is sample code from a web site. And not applicable to my classes. I just could not figure out what was what.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help SORTING AN ARRAY

    Look at the bubble sort. Wiki has pseudo code that shows you the needed steps. Use Google for bubble sort
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help SORTING AN ARRAY

    Thank you for your suggestion. I took some time to review and understand the bubble sort class. As a stand alone class it makes sense to me. However, I don't understand how it applies to my two classes.

    BanffMarathonRunnerDemo.
    Creates the array strings and integers. (firstname, lastname, time, year)

    AddressBook
    I have a class with objects firstName and lastName

    BanffMarathonRunner
    extends addressbook to include objects time and year.

    I understand what bubble sort does. I don't undestand how to use it in BanffMarathonRunner to reference the array in BanffMarathonRunnerDemo or the objects in AddressBook.

    Forgive me if my termonology is off. This is all new to me.

     
    public class BanffMarathonRunner extends AddressBook {
        private int time; 
        private int years; 
     
    public BanffMarathonRunner(String firstName, String lastName, int min, int yr) {
        super(firstName);
        super.setLastName(lastName);
        this.time = min;
        this.years = yr;}
     
    public int getTime() {return this.time;}
    public int getYears() {return this.years;}
    public void setTime(int time) {this.time = time;}
    public void setYears(int years) {this.years = years;}
     
    public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
    	BanffMarathonRunner fastestRunner = runners[0];
     
     
            int n = arr.length;
            for (int i = 0; i < n-1; i++)
                for (int j = 0; j < n-i-1; j++)
                    if (arr[j] > arr[j+1])
                    {
                        // swap temp and arr[i]
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
     
    	return fastestRunner;
        	} 
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help SORTING AN ARRAY

    how it applies to my two classes.
    I need to search for the lowest integer (time) and the second lowest integer (time)
    Use the sort logic to sort the array with the times in ascending order. Once the array is sorted, the code can pick off the two lowest times from the first and second slot in the array.

    The method getFastestRunner() sounds like it would only return one value, the first one in the array AFTER the sort.

    The sorting logic needs to look at the times field in the BanffMarathonRunner class to make the sorting decisions.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help SORTING AN ARRAY

    I'm getting closer. Getting a couple errors.


    public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners) {
     
     
    	int n = runners.length;
            		for (int i = 0; i < n-1; i++)
                		for (int j = 0; j < n-i-1; j++)
                    			if (runners[j].getTime() > runners[j+1].getTime())
                    			{
                        			// swap temp and arr[i]
                        			int temp = runners[j];
                        			runners[j]= runners[j+1];
                        			runners[j+1] = temp;
                    			}
    	BanffMarathonRunner fastestRunner = runners[0];
    	return fastestRunner;
        	}



    Error: incompatible types: BanffMarathonRunner cannot be converted to int.
    int temp = runners[j];


    Error: incompatible types: int cannot be converted to BanffMarathonRunner
    runners[j+1] = temp;

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help SORTING AN ARRAY

    int cannot be converted to BanffMarathonRunner
    Define temp as BanffMarathonRunner instead of int.

    These statements:
    	BanffMarathonRunner fastestRunner = runners[0];
    	return fastestRunner;

    could be:
           return runners[0];
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help SORTING AN ARRAY

    closer...

    No compile errors. I am getting an error when running it.
    java exception in thread main java.lang.nullpointerexception.
    at BanffMarathonRunner.getFastestRunner(BanffMarathon Runner.java:23)
    at BanffMarathonRunnerDemo.main(BanffMarathonRunnerDe mo.java:12)

    I looked up the error but it isn't very clear.

    --- Update ---

    got it. My array was defined too long. Thank you so much.

    --- Update ---

    How can something be so frustrating and satisfying at the same time! Again....many thanks for your patience.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help SORTING AN ARRAY

    I'm glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Need help SORTING AN ARRAY

    another way is using standard Java API

    import java.util.Arrays;
    import java.util.Comparator;
    ...
    Arrays.sort(runners, 
      Comparator.nullsLast( 
      Comparator.comparing(Runner -> Runner.getTime() )
    ));

Similar Threads

  1. Array Sorting
    By keepStriving in forum Collections and Generics
    Replies: 4
    Last Post: November 18th, 2013, 06:35 PM
  2. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  3. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  4. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM