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

Flex Tron
Flex Tron used Ask the Experts™
on
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)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
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
Flex TronDeveloper

Author

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

Top Expert 2016

Commented:
Those should be in separate files. Please post them in separate sets of code tags
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Flex TronDeveloper

Author

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

Top Expert 2016

Commented:
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
Flex TronDeveloper

Author

Commented:
Sure CEHJ...changed that.
I am wondering how to link both these classes to get the output as required.
/*
 * 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

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

Open in new window


in barchart.java

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial