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
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 ***********************
BarChart Code:
public class BarChart { private int numberOfStars; public BarChart(int numberOfStars) { this.numberOfStars = numberOfStars; } public void displayBar() { for(int i=0;i System.out.print("*"); }
System.out.println();
BarChartTest Code
import java.util.Scanner;
public class BarChartTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); for(int i=0;i<5;i++){ System.out.println("Enter a number:"); int numberOfStars = scan.nextlnt(); BarChart barChart = new BarChart(numberOfStars); barChart.displayBar();
What is the problem with the code?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
