Question

In: Computer Science

JAVA Exercise BasketBall Bar Chart Write a program that allows you to create a bar chart...

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 *********

Solutions

Expert Solution

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.


Related Solutions

JAVA PROGRAM Overview : You will create a card deck program that allows the player to...
JAVA PROGRAM Overview : You will create a card deck program that allows the player to shuffle a deck of cards and play a game that draws and plays cards until all cards in the deck have been used. Instructions: The application must be able to perform the following tasks : 1. At the start of a new game, it shuffles all cards using a deck shuffler 2. The player then draws X number of cards from the deck and...
Create a java program that allows people to buy tickets to a concert. Using java create...
Create a java program that allows people to buy tickets to a concert. Using java create a program that asks for the users name, and if they want an adult or teen ticket. As long as the user wants to purchase a ticket the program with "yes" the program will continue. When the user inputs "no" the program will output the customer name, total amount of tickets, and the total price. The adult ticket is $60 and the child ticket...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
Using the data shown, you will create two charts. The first chart is a bar chart...
Using the data shown, you will create two charts. The first chart is a bar chart that will display % growth in different job titles between 2010 and 2020Est. The second chart will be a pie chart showing the 2020Est. Jobs in different categories as a % of overall 2020Est. jobs. Computer-Related Jobs 2010 2020 Est. % Change Systems Analysts         544,400         664,800 Software App Developers         520,800         664,500 Programmers         363,100         406,800 Network/System Admins         347,200...
(JAVA) Create a program that creates a mini database of numbers that allows the user to:...
(JAVA) Create a program that creates a mini database of numbers that allows the user to: reset the database, print the database, add a number to the database, find the sum of the elements in the database, or quit. In main, you will declare an array of 10 integers (this is a requirement). Then you will define the following methods: • printArray (int[ ] arr) – this takes in an array and prints it • initArray (int[ ] arr) –...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT