Create a class called BarChart that displays a series of asterisks (*) for a specific number.

Dear Java Gurus,
I have an assignment where I need your help.
This is the Question:
Create 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 mainmethod. The method should ask the user fora number and use the BarChart class to create a BarChart object that displays the appropriate number of asterisks. This should be repeated five times.

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
***********************

I have attached my code in the question.
I am using Netbeans
The error I am getting is :
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
      at barchart.BarChart.main(BarChart.java:18)
Flex TronDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
That clearly tells you where the error is - your code won't compile. We can't say anything else as you haven't posted your code
0
Flex TronDeveloperAuthor Commented:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package barchart;
import java.util.Scanner;
/**
 *
 * @author X
 */
public class BarChart {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        private int numberOfStars;

    public BarChart(int numberOfStars) {
        this.numberOfStars = numberOfStars;
    }

    public void displayBar() {
        
        for(int i=0;i<=5;i++);
        { 
        
        System.out.print("*");
        
        System.out.println();
    }
    
};

public class BarChartTester {
    public  void main(String [] args) {
        Scanner input = new Scanner(System.in);
        int i;
         // Loop 5 times asking for a number and creating a
        // BarChart object, and calling the displayBar method

        for (i=0;i<=5;i++)
        {System.out.print("Enter a number: ");
        int numberOfStars = input.nextInt();
        BarChart BarChart = new BarChart(numberOfStars);
        BarChart.displayBar();
    }
    };
 

Open in new window

0
CEHJCommented:
Those should be in separate files. Please post them in separate sets of code tags
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Flex TronDeveloperAuthor Commented:
package barchart;
import java.util.Scanner;
/**
 *
 * @author X
 */
public class BarChart {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        private int numberOfStars;

    public BarChart(int numberOfStars) {
        this.numberOfStars = numberOfStars;
    }

    public void displayBar() {
        
        for(int i=0;i<=5;i++);
        { 
        
        System.out.print("*");
        
        System.out.println();
    }
    
};

Open in new window


public class BarChartTester {
    public  void main(String [] args) {
        Scanner input = new Scanner(System.in);
        int i;
         // Loop 5 times asking for a number and creating a
        // BarChart object, and calling the displayBar method

        for (i=0;i<=5;i++)
        {System.out.print("Enter a number: ");
        int numberOfStars = input.nextInt();
        BarChart BarChart = new BarChart(numberOfStars);
        BarChart.displayBar();
    }
    };
 

Open in new window

0
CEHJCommented:
The first thing you need to do is check your curly braces. You're trying to write too much code before compiling it. You should write/compile in small increments. A class never ends in a semicolon
0
Flex TronDeveloperAuthor Commented:
Sure CEHJ...changed that.
I am wondering how to link both these classes to get the output as required.
0
krakatoaCommented:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
package barchart;
import java.util.Scanner;
/**
 *
 * @author X
 */
public class BarChart {

int numberOfStars;

    /**
     * @param args the command line arguments
     */
     
     public BarChart(int numberOfStars) {
        this.numberOfStars = numberOfStars;
    }
    
    public void displayBar() {
        
        for(int i=0;i<=5;i++);
        { 
        
        System.out.print("*");
        
        System.out.println();
    }
    
    }
    
    public static void main(String[] args) {
        /*private*/ int numberOfStars;

    }

    

public class BarChartTester {
    public  void main(String [] args) {
        Scanner input = new Scanner(System.in);
        int i;
         // Loop 5 times asking for a number and creating a
        // BarChart object, and calling the displayBar method

        for (i=0;i<=5;i++)
        {System.out.print("Enter a number: ");
        int numberOfStars = input.nextInt();
        BarChart BarChart = new BarChart(numberOfStars);
        BarChart.displayBar();
    }
    }
    }
 }

Open in new window

0
Flex TronDeveloperAuthor Commented:
Had to define a new variable
 String displayStar = "";
        for(int i=0;i< numberOfStars;i++)

Open in new window


in barchart.java
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.