Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with creating a class called BarChart that displays a series of aste

ID: 3585346 • 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 ***********************

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?

Explanation / Answer

Hi,

I have fixed the issues. Please find the below

BarChartTest.java

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 = input.nextInt();

BarChart barChart = new BarChart(numberOfStars);

barChart.displayBar();

}

}

}

BarChart.java

public class BarChart {

private int numberOfStars;

public BarChart(int numberOfStars) {

this.numberOfStars = numberOfStars;

}

public void displayBar() {

for(int i=0;i <numberOfStars; i++) {

System.out.print("*");

}

System.out.println();

}

}


Output:

Enter a number:

5

*****

Enter a number:

6

******

Enter a number:

7

*******

Enter a number:

8

********

Enter a number:

3

***

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote