We help IT Professionals succeed at work.

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

Flex Tron
Flex Tron asked
on
212 Views
1 Endorsement
Last Modified: 2018-10-01
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

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

CERTIFIED EXPERT
Top Expert 2016

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

CERTIFIED EXPERT
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.
CERTIFIED EXPERT

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 {

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:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions