[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

bubbleSort an array of Competitor attributes.

Basically I have to use BubbleSort to sort an array of objects.
here is what I have!

I have a Competitor.txt file:
public class Competitor implements Comparable {

      public int competitorID;
      public String competitorName;
      public double competitorMark;

      //constructor with parameters
      public Competitor(int ID, String n, double m) {
            competitorID = ID;
            competitorName = n;
            competitorMark = m;
      }//end constructor

      //(competitor ID accessor)
      public int getCompetitorID() {
            return competitorID;
      }//end method

      //(competitor name accessor)
      public String getCompetitorName() {
            return competitorName;
      }//end method

      //(competitor mark accessor)
      public double getCompetitorMark() {
            return competitorMark;
      }//end method

      //(competitor mark mutator)
      public void setCompetitorMark(double dMark) {
            competitorMark = dMark;
      }//end method

      //toString method
      public String toString() {
            return (competitorID + " " + competitorName + " " + competitorMark);
      }//end method

      //compareTo method
      public int compareTo(Object o) {
            // ... method implementation
            if (competitorMark < ((Competitor)o).competitorMark)
                  return -1;
            else if (competitorMark == ((Competitor)o).competitorMark)
                  return 0;
            else
                  return +1;
      }//end method

      //equals method
      public boolean equals(Object o) {
            if(competitorID == ((Competitor)o).getCompetitorID()) return true;
            else return false;
      }//end method
}

I have a ContestManagement.txt file(which holds the main method):
import java.lang.*;
import java.util.*;
import java.io.*;
import ch10.*;

public class ContestManagement {
      
    public static void main (String [] args)  throws IOException{

      //define local variables
      final int MAX_LENGTH = 20;
      Competitor competitorArray[] = new Competitor[MAX_LENGTH];
      int arraySize = 0;

      Scanner stdin = new Scanner(System.in);

      //set up file stream
        Scanner fileIn = null;
       
      //read and process the file Competitors.txt
      try {
            fileIn = new Scanner(new File("Competitors.txt"));
            while(fileIn.hasNext()) {
                  int ID = fileIn.nextInt();
                  String n = fileIn.next();
                  double m = 0.0;
                  Competitor c = new Competitor(ID, n, m);
                  competitorArray[arraySize] = c;
                  ++arraySize;
            }
      }
      catch (InputMismatchException e) {
            System.out.println("uh oh, got an exception error!");
            }

      //read and process the file Marks.txt
      //when same ID is found mark is reset to new value
      //display unsorted list of competitors
      try {
            fileIn = new Scanner(new File("Marks.txt"));
            while(fileIn.hasNext()) {
                  int ID = fileIn.nextInt();
                  double m = fileIn.nextDouble();
                  for (int i = 0; i < arraySize; ++i) {
                        if (competitorArray[i].getCompetitorID() == ID) {
                              competitorArray[i].setCompetitorMark(m);
                        }
                  }
            }
            for (int i = 0; i < arraySize; ++i) {
                  System.out.println(competitorArray[i]);
            }
      }
      catch (NullPointerException e) {
            System.out.println("uh oh, got an exception error!");
            }
    }
}      

at this point, the main method will take two files listed above that contain the following information.
Competitors.txt
6241 Cathy
3792 Patrick
2910 John
8371 Mary
9372 Tony
3532 Jane

Marks.txt
3792 8.25
3532 6.37
9372 9.12
6241 8.53
2910 7.32
8371 9.72

Up to this point i am fine, I have entered both files and added the information to the array!   when it prints it prints like this.
6241 Cathy 8.53
3792 Patrick 8.25
2910 John 7.32
8371 Mary 9.72
9372 Tony 9.12
3532 Jane 6.37
However now I must use BubbleSort to sort the array by way of their marks(the double value) from highest to lowest.  I am totally lost.  The textbook doesnt give but a half page example, and I don't even know where to start.  Could anyone help me!
0
rebelcowgirl_73
Asked:
rebelcowgirl_73
  • 4
  • 2
2 Solutions
 
rebelcowgirl_73Author Commented:
It is basically the same example that is in the book but how do I add the algorithm to the ContestManagement.txt file
0
 
objectsCommented:
     for (int i = 0; i < a.length; i++)     // a is the array of items to be sorted
          for (int j = a.length-1; j > i; j--)
          {
            if (a[j-1] > a[j])      // this is the comparison (use your compareTo() method instead)
            {   int T = a[j-1];     // if a[j-1] is greater than a[j], then swap them
                a[j-1] = a[j];
                a[j] = T;
            }
          }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rebelcowgirl_73Author Commented:
Instructions word for word are:
Class ContestManagement contains the main method. It also defines the array of
Competitor objects and the static methods used for bubble sorting. The bubble sort
methods are presented in the textbook beginning with page 664.(copied below)

static boolean bubbleUp2(int startIndex, int endIndex) {
      //switches adjacent pairs that are out of order
      //between values[startIndex]...values[endIndex]
      //beginning at values[endIndex]
      //returns false if a swap was made: otherwise, returns true.
      boolean sorted = true;
      for (int index = endIndex; index > startIndex; --index)
            if (values[index] < values[index - 1]) {
                  swap(index, index - 1);
                  sorted = false;
            }
      return sorted;            
}

static void shortBubble() {
      //sorts the values array using the bubble sort algorithm
      //the process stops as soon as values is sorted
      int current = 0;
      boolean sorted = false;
      while ((current < (SIZE - 1)) && !sorted) {
            sorted = bubbleUp2(current, SIZE - 1);
            ++current;
      }
}

i dont know how to add methods in the main method sort of speak or if I do I am doing it wrong because they are not working!
0
 
rebelcowgirl_73Author Commented:
Ok I think I have added the bubbleSort methods correctly I am not getting any errors and the program runs as if I had not added them yet!  My question is this!  how would I now in the main method implement the shortBubble() so that it could sort the array?

Here is my new code!

import java.lang.*;
import java.util.*;
import java.io.*;
import ch10.*;

public class ContestManagement {

    //define local variables
    static final int MAX_LENGTH = 20;
    static Competitor competitorArray[] = new Competitor[MAX_LENGTH];
    static int arraySize = 0;
      
    //define method swap for bubbleSort
    static public void swap(int index1, int index2) {
      //precondition: index 1 and index 2 are >= 0 and < MAX_LENGTH
      //swaps the integers at locations index1 and index2 of the competitorsArray
      Competitor temp = competitorArray[index1];
      competitorArray[index1] = competitorArray[index2];
      competitorArray[index2] = temp;
    }

    //define method bubbleUp2 for bubbleSort
    static boolean bubbleUp2(int startIndex, int endIndex) {
      //switches adjacent pairs that are out of order
      //between values[startIndex]...values[endIndex]
      //beginning at values[endIndex]
      //returns false if a swap was made: otherwise, returns true.
      boolean sorted = true;
      for (int index = endIndex; index > startIndex; --index)
            if (competitorArray[index].compareTo(competitorArray[index - 1]) < 0) {
                  swap(index, index - 1);
                  sorted = false;
            }
      return sorted;            
    }

    //define method shortBubble for bubbleSort
    static void shortBubble() {
      //sorts the values array using the bubble sort algorithm
      //the process stops as soon as values is sorted
      int current = 0;
      boolean sorted = false;
      while ((current < (MAX_LENGTH - 1)) && !sorted) {
            sorted = bubbleUp2(current, MAX_LENGTH - 1);
            ++current;
      }
    }

    //define main method
    public static void main (String [] args)  throws IOException{

      
      Scanner stdin = new Scanner(System.in);

      //set up file stream
        Scanner fileIn = null;
       
      //read and process the file Competitors.txt
      try {
            fileIn = new Scanner(new File("Competitors.txt"));
            while(fileIn.hasNext()) {
                  int ID = fileIn.nextInt();
                  String n = fileIn.next();
                  double m = 0.0;
                  Competitor c = new Competitor(ID, n, m);
                  competitorArray[arraySize] = c;
                  ++arraySize;
            }
      }
      catch (InputMismatchException e) {
            System.out.println("uh oh, got an exception error!");
            }

      //read and process the file Marks.txt
      //when same ID is found mark is reset to new value
      //display unsorted list of competitors
      try {
            fileIn = new Scanner(new File("Marks.txt"));
            while(fileIn.hasNext()) {
                  int ID = fileIn.nextInt();
                  double m = fileIn.nextDouble();
                  for (int i = 0; i < arraySize; ++i) {
                        if (competitorArray[i].getCompetitorID() == ID) {
                              competitorArray[i].setCompetitorMark(m);
                        }
                  }
            }
            for (int i = 0; i < arraySize; ++i) {
                  System.out.println(competitorArray[i]);
            }
      }
      catch (NullPointerException e) {
            System.out.println("uh oh, got an exception error!");
            }

      
    }
}            
0
 
rebelcowgirl_73Author Commented:
I figured it out!!!!!  Thanks So Much Objects!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now