Avatar of Flex Tron
Flex Tron
Flag for United States of America asked on

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)
Java

Avatar of undefined
Last Comment
Flex Tron

8/22/2022 - Mon
CEHJ

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 Tron

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

CEHJ

Those should be in separate files. Please post them in separate sets of code tags
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Flex Tron

ASKER
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

CEHJ

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 Tron

ASKER
Sure CEHJ...changed that.
I am wondering how to link both these classes to get the output as required.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
krakatoa

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

ASKER CERTIFIED SOLUTION
Flex Tron

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.