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

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 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...
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...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT