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

Write a Java program to sort the NBA stars based on their heights. Bryant, Kobe.

ID: 3724109 • Letter: W

Question

Write a Java program to sort the NBA stars based on their heights.

Bryant, Kobe. 198 cm

Duncan, Tim. 213 cm

Garnett, Kevin. 211 cm

Iverson, Allen. 183 cm

Kidd, Jason. 193 cm

McGrady, Tracy. 203 cm

O'Neal, Shaquille. 216 cm

Pierce, Paul. 199 cm

Webber, Chris. 208 cm

Yao, Ming. 226 cm

Notice that each item consists of a star’s name (in Last, First format) and a star’s height and all the items are arranged in the alphabetical order based on the last names. Within your program, a skeleton of a class called Star is given as follows.

class Star

{

     //data fields

     String fullName;

     int height;

     //Constructor

     Star(String name, int h) { }

}

Firstly, you are required to fill in the definition of the constructor in the Star class.

You are further asked to input the last name, first name, and height for each NBA star listed above. Record the number of NBA stars you input. Next you will define a Star array and the size of it is the same as the number of NBA stars you just input. Create Star objects based on the information for the NBA stars and store each Star object in the array defined early. Display the information of each star, according to the alphabetical order of the last names, on the console. Then use selection sort algorithm to sort the objects in the array based on the heights of the stars and display the information of each star, according to the heights of the stars, on the console. A sample run of your program is as follows.

The NBA stars are arranged by their last names

Star 1 is Bryant, Kobe. His height is 198 cm.

Star 2 is Duncan, Tim. His height is 213 cm.

Star 3 is Garnett, Kevin. His height is 211 cm.

Star 4 is Iverson, Allen. His height is 183 cm.

Star 5 is Kidd, Jason. His height is 193 cm.

Star 6 is McGrady, Tracy. His height is 203 cm.

Star 7 is O'Neal, Shaquille. His height is 216 cm.

Star 8 is Pierce, Paul. His height is 199 cm.

Star 9 is Webber, Chris. His height is 208 cm.

Star 10 is Yao, Ming. His height is 226 cm.

The NBA stars are arranged by their heights

Star 1 is Iverson, Allen. His height is 183 cm.

Star 2 is Kidd, Jason. His height is 193 cm.

Star 3 is Bryant, Kobe. His height is 198 cm.

Star 4 is Pierce, Paul. 199 cm.

Star 5 is McGrady, Tracy. His height is 203 cm.

Star 6 is Webber, Chris. His height is 208 cm.

Star 7 is Garnett, Kevin. His height is 211 cm.

Star 8 is Duncan, Tim. His height is 213 cm.

Star 9 is O'Neal, Shaquille. His height is 216 cm.

Star 10 is Yao, Ming. His height is 226 cm.

Explanation / Answer


/**The java program that prompts first name, last name
* and score and print the star players of NBA by alphatical
* sorting and sort by height*/
//NBATeams.java
import java.util.Scanner;
public class NBATeams {
  
   public static void main(String[] args) {
      
       int size=10;
       //Create Scanner class instance
       Scanner scan=new Scanner(System.in);
       String firstName;
       String lastName;
       int height;
       Star[] stars=new Star[size];
      
       for (int i = 0; i < size; i++)
       {
           System.out.println("Player "+(i+1));
           //Prompt for first name
           System.out.print("Enter first name : ");
           firstName=scan.nextLine();
          
           //Prompt for last name
           System.out.print("Enter last name : ");
           lastName=scan.nextLine();
          
           //Prompt for height
           System.out.print("Enter height : ");
           height=Integer.parseInt(scan.nextLine());
          
           //Create an instance of Star
           stars[i]=new Star(firstName+", "+lastName, height);
       }
      
      
       System.out.println("The NBA stars are arranged by their last names");
       //calling sortByName method
       sortByName(stars);
       //calling printArray method

       printArray(stars);
       System.out.println(" The NBA stars are arranged by their heights ");
       //calling sortByName method
       sortByHeight(stars);
       //calling printArray method
       printArray(stars);
      
   }
  
   /**The method sortByName that takes an array of Star
   * type and then sort the Star array by name*/
   private static void sortByName(Star[] stars)
{
int n = stars.length;
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min = i;
for (int j = i+1; j < n; j++)
if (stars[j].fullName.compareTo(stars[min].fullName)<0)
   min = j;

/*Swap the found minimum element with the first*/
Star temp = stars[min];
stars[min] = stars[i];
stars[i] = temp;
}
}
  
   /**The method sortByName that takes an array of Star
   * type and then sort the Star array by height*/
   private static void sortByHeight(Star[] stars)
{
int n = stars.length;
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min = i;
for (int j = i+1; j < n; j++)
if (stars[j].height<stars[min].height)
   min = j;

/*Swap the found minimum element with the first*/
Star temp = stars[min];
stars[min] = stars[i];
stars[i] = temp;
}
}

// Prints the array
public static void printArray(Star arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print("Star "+(i+1)+" is "+arr[i].toString());
System.out.println();
}

}

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

//Star.java
class Star
{
   //data fields
   String fullName;
   int height;

   //Constructor
   Star(String name, int h)
   {
       this.fullName=name;
       this.height=h;
   }
   //Override toString method
   public String toString() {
       return String.format("%s. His height is %d cm. ",
               fullName,height);
   }
}

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

Sample Output:

Note :Enter input (Enter input manually for 10 players):

Player 1
Enter first name :

(etc)

Sample Output :

The NBA stars are arranged by their last names
Star 1 is Bryant, Kobe. His height is 198 cm.
Star 2 is Duncan, Tim. His height is 213 cm.
Star 3 is Garnett, Kevin. His height is 211 cm.
Star 4 is Iverson, Allen. His height is 183 cm.
Star 5 is Kidd, Jason. His height is 193 cm.
Star 6 is McGrady, Tracy. His height is 203 cm.
Star 7 is O'Neal, Shaquille. His height is 216 cm.
Star 8 is Pierce, Paul. His height is 199 cm.
Star 9 is Webber, Chris. His height is 208 cm.
Star 10 is Yao, Ming. His height is 226 cm.


The NBA stars are arranged by their heights

Star 1 is Iverson, Allen. His height is 183 cm.
Star 2 is Kidd, Jason. His height is 193 cm.
Star 3 is Bryant, Kobe. His height is 198 cm.
Star 4 is Pierce, Paul. His height is 199 cm.
Star 5 is McGrady, Tracy. His height is 203 cm.
Star 6 is Webber, Chris. His height is 208 cm.
Star 7 is Garnett, Kevin. His height is 211 cm.
Star 8 is Duncan, Tim. His height is 213 cm.
Star 9 is O'Neal, Shaquille. His height is 216 cm.
Star 10 is Yao, Ming. His height is 226 cm.

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