Link to home
Start Free TrialLog in
Avatar of Flex Tron
Flex TronFlag 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)
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of 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

Those should be in separate files. Please post them in separate sets of code tags
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

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

ASKER CERTIFIED SOLUTION
Avatar of Flex Tron
Flex Tron
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial