The topic is ARRAYS! I need a program in JAVA! for BEGINNERS. Please can you put
ID: 3865684 • Letter: T
Question
The topic is ARRAYS! I need a program in JAVA! for BEGINNERS. Please can you put comment lines explaining each step? I need a working program.
Write a program that reads a sequence of input values and displays a bar chart of the values in data, using asterisks, like this:
*********************
*************************
*********
**************************
You may assume that all values are positive. First figure out the maximum value in data. That value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks. Improve your program by adding caption to your bar. Prompt the user for the captions and data values. The output should look like this:
Egypt ********************* France ************************* Norway ********* Germany **************************Explanation / Answer
import java.util.Scanner;
public class Bars {
static double largestNum=0;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Please enter No of values you want to enter ");
//reading total number of entries
int length=sc.nextInt();
String[] names=new String[length]; //array for storing captions
int[] values=new int[length]; //array for storing values for the captions
//reading captions and values
for (int i = 0; i < length; i++) {
System.out.println("please enter the caption :");
String tempName=sc.next();
names[i]=tempName;
System.out.println("please enter the value for "+names[i] +" :");
int tempValue=sc.nextInt();
values[i]=tempValue;
}
//getting the largest number
largestNum=getLargestNumber(values);
//printing the result
for(int i=0;i<length;i++){
System.out.println (names[i] + " "+(getStars(values[i])));
}
sc.close();
}
/**
* getting the largest number
* @param values
* @return
*/
public static double getLargestNumber(int[] values){
double large=0.0;
for(int i=0;i<values.length;i++){
if(values[i]>large){
large=values[i];
}
}
return large;
}
/**
* For printing the stars
* @param value
* @return
*/
public static String getStars(int value){
String stars="";
double result=value/largestNum ;
result=result*40;
for(int j=0;j<result;j++){
stars=stars+"*";
}
return stars;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.