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

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...
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 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.
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...
********JAVA************ This program deals with graphs and path-finding within them. write a program which allows the...
********JAVA************ This program deals with graphs and path-finding within them. write a program which allows the user to enter multiple levels of a text, consisting of a 'start', an 'exit', 'walls', and 'floors'. The letter S represents the start. E is the exit. O (the letter 'oh') represents a 'floor' (meaning a path you can follow). X is a wall (meaning a block you cannot pass). The program will accept Standard Input (System.in), It is up to you whether you...
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT