In: Computer Science
JAVA Exercise
BasketBall Bar Chart
Write a program that allows you to create a bar chart for the active members of a basketball team during a game. (Recall: there are only 5 active players on the court per team in a standard basketball game.)
Your program should do the following tasks:
• Prompt the user to store the first name of the five players
• Prompt the user to enter the points scored by each player
a. Use a while or do…while loop to check for input validation for the points (which cannot be negative)
• Calls a method to construct the chart for each player (based on the number of points). The method should have the following characteristics:
a. The method accepts two parameters – the player name and the points scored by that player
b. The method does not return a value
c. The method generates an asterisk (i.e. *) for every point scored. For instance, it should display *** if the player scored 3 points. Use a for loop to generate the asterisks.
d. Use the format method from the String class to align your player’s names neatly. (See Week 3 Lecture 1 Class code for a recap of this concept.)
A sample of the output is shown below (note: the text does not have to be exact. This output included to show you how the ‘chart’ would be generated):
Enter the first names of the 5 players: Jaden Tommy Danny Mason Bobby
Enter points earned by Jaden: 12
Enter points earned by Tommy: 6
Enter points earned by Danny: 4
Enter points earned by Mason: 1
Enter points earned by Bobby: 9
Points for Game
Jaden ************
Tommy ******
Danny ****
Mason *
Bobby *********
All the explanations is given in the comments of the code itself.
Code--
import java.util.*;
public class Program
{
   //method to print the chart
   public static void constructChart(String []name,int[]
points)
   {
       int i,j;
       for(i=0;i<5;i++)
       {
           //allign the
string properly while printing
           String
temp="";
          
for(j=1;j<=points[i];j++)
           {
          
    temp=temp+"*";
           }
          
temp=String.format("%s\t%s",name[i],temp);
          
System.out.println(temp);
       }
   }
   //main method
   public static void main(String[] args)
   {
       Scanner sc=new
Scanner(System.in);
       String[] name=new String[5];
       int[] points=new int[5];
       int i;
       //prompt the user to store the
names
       System.out.println("Enter the first
names of the 5 players:");
       for(i=0;i<5;i++)
       {
          
name[i]=sc.nextLine();
       }
       //validate the input using do while
loop
       //and get input for the
scores
       i=0;
       do
       {
          
System.out.print("Enter points earned by "+name[i]+": ");
          
points[i]=sc.nextInt();
          
if(points[i]>=0)
           {
          
    i++;
           }
       }while(i<5);
       System.out.println("Points for
game:");
       //call the method
       constructChart(name,points);
   }
}
Code Screenshot--


Output Screenshot--

Note--
Please upvote if you like the effort.