I need help with creating a class called BarChart that displays a series of aste
ID: 3585167 • Letter: I
Question
I need help with creating a class called BarChart that displays a series of asterisks (*) for a specific number. The class's constructor should have one integer parameter that represents the number of asterisks to display. The class should also have a method called displayBar that displays the series of asterisks.
Create a second class called BarChartTester, that contains the main method, that reads five numbers and uses the BarChart class to display the appropriate number of asterisks. The BarChartTester class should produce the following example input/output:
Enter a number: 5 *****
Enter a number: 15 ***************
Enter a number: 7 *******
Enter a number: 3 ***
Enter a number: 23 ***********************
Explanation / Answer
BarChartTester.java
import java.util.Scanner;
public class BarChartTester {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
for(int i=0;i<5;i++) {
System.out.println("Enter a number: ");
int n = scan.nextInt();
BarChart b = new BarChart(n);
b.displayBar();
}
}
}
BarChart.java
public class BarChart {
private int n;
public BarChart(int n) {
this.n = n;
}
public void displayBar() {
for(int i=0;i<n;i++) {
System.out.print("*");
}
System.out.println();
}
}
Output:
Enter a number:
5
*****
Enter a number:
15
***************
Enter a number:
7
*******
Enter a number:
3
***
Enter a number:
23
***********************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.