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
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 stars name (in Last, First format) and a stars 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
