Write a program that reads a sequence of input values and displays a bar chart o
ID: 3690188 • Letter: W
Question
Write a program that reads a sequence of input values and displays a bar chart of the values, using asterisks, like this:
**********************
****************************************
****************************
**************************
**************
You may assume that all values are positive. First figure out the maximum value. That value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks.
You may assume that all values are positive. First figure out the maximum value. That value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks
Instructions.
Print a chart of asterisks based on the input list and largest element in the list.
import java.util.ArrayList; import java.util.Scanner;
public static void main(String[] args) {
ArrayList<Integer> list = readValues();
printBarChart(list); }
MAIN
2 methods called
public static ArrayList<Integer> readValues() {
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.println("Enter positive integers. " + "Enter a negative value to quit: ");
Scanner in = new Scanner(System.in); int n = in.nextInt();
while (n >= 0)
{
list.add(n);
n = in.nextInt(); }
return list; }
Reading the input
public static void printBarChart(ArrayList<Integer> list) {
int max = findMax(list);
for (int i = 0; i < list.size(); i++) {
int n = list.get(i);
int stars = (int) (40.0 * n / max);
for (int j = 0; j < stars; j++) {
System.out.print("*"); }
System.out.println(); }}
max
The chart must be scaled
Using 40 as
public static int findMax(ArrayList<Integer> list) {
int max = list.get(0); for (int x : list)
{
if (x > max) {
max = x; }
}
return max; }
Finding the largest input
(If you get the jre 5.1 error, click on the quick fix)
I
Explanation / Answer
Algorithm:
program:
Declare Class StarStat { Integer numOfValues Declare Array String[] valueName Declare Array Integer[] value Declare Integer currentMax Declare Public Void setValues() { Display (“Type how many values to input: “) Input numOfValues new valueName[numOfValues] new value[numOfValues] For (Integer counter=0 to counter<valueName.length, counter++) { Display (“Please enter the title for the value # “, counter+1) Input valueName[counter] Display (“Please enter the value for the # “, counter+1) Input value[counter] } } Declare Public Void findMax() { currentMax = value[0] For (Integer counter=0 to counter < value.length(), counter++) { If( currentMax < value[counter]) { currentMax = value[counter] } } } Declare public Void displayStarStat() { Integer valueStars For (Integer counter=0 to counter < value.length(), counter++) { valueStars = double(value[counter]/currentMax) * 40 For (Integer count=0 to count < valueStars, count++) { Display (“*”) } } } } Declare Class StarStatDemo { Create a new object named demoVer1 of StarStatDemo Call demoVer1.setValues() Call demoVer1.findMax() Call demoVer1.displayStarStat() }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.