-3

I have this coursework assignment and im supposed to create a class with a constructor with a single parameter, an array of integers specifying incomes, in increasing order. Later i have to draw a chart that shows the incomes and the taxes. I wrote a bit of code with the help of a demonstrator during a practical,but right now im stuck again

public class TaxChart {

    static int[] income;

    public TaxChart(int[] income) {
        this.income = income;
        income[0] = 100;
        income[1] = 120;
        income[2] = 130;
        income[3] = 160;
        income[4] = 320;
    }


    public static void main(String[] args) {
        draw();
    }

    public static void draw() {
        TaxChart incomes = new TaxChart();

        //  ...
    }
}

Somehow I need to get the income values to the draw method,but i get loads of errors when doing it this way. I know this is a simple question,but I mainly programmed in C++ before and I have almost zero experience in Java.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 2
    The constructor for `TaxChart` requires an array of integers. So when you do `new TaxChart()` you have to pass it an array of integers. Though I suspect there's *a lot* more wrong here. Why is *everything* `static`? Why does the constructor *overwrite* the array you send it? – David Oct 25 '16 at 17:46
  • 2
    change `TaxChart incomes = new TaxChart();` to `TaxChart incomes = new TaxChart(income);` – DimaSan Oct 25 '16 at 17:47
  • 1
    Your `income` seems to be `static` which is probably wrong. Drop this code and go read a tutorial. – Kayaman Oct 25 '16 at 17:48
  • you actually assingn the array element within the constructor of `TaxChart`, You schoul do this in `draw()`. – Timothy Truckle Oct 25 '16 at 17:48
  • Write this.draw() inside main method – Aammad Ullah Oct 25 '16 at 17:50
  • in case you have no idea where to even find some information on constructors or arrays in Java -- http://stackoverflow.com/documentation/java/682/constructors#t=201610251751458748452 http://stackoverflow.com/documentation/java/99/arrays/404/creating-and-initializing-arrays#t=201610251750252391139 – Jeutnarg Oct 25 '16 at 17:52
  • also, since you don't seem familiar with 'static', http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java – Jeutnarg Oct 25 '16 at 17:54
  • and finally (I promise), please tell me you're using an IDE like Eclipse or IntelliJ. Java IDE's are fantastic in general and are quite useful when debugging or examining compile errors. – Jeutnarg Oct 25 '16 at 17:57
  • And hint: this is **super basic stuff**. Any decent book or tutorial would give you that information. You really shouldn't come here with such stuff. You see, this is **not** a programming school were people spend their free time teaching you such elemental stuff. If we do, then just because sometimes we ignore the low quality of the question ... – GhostCat Oct 25 '16 at 18:19

1 Answers1

0

Your constructor takes a value of int[] arrays, but you're not passing any in main. Also there's no need for income to be static as that would mean it belongs to the class (not the object itself).

Do something like so:

public class TaxChart {

    private int[] income;

    public TaxChart(int[] income) {
        this.income = income;
        this.income[0] = 100;
        this.income[1] = 120;
        this.income[2] = 130;
        this.income[3] = 160;
        this.income[4] = 320;
    }

    public void draw() {
        //TaxChart incomes = new TaxChart();

        //  ...
    }

    public static void main(String[] args) {
        TaxChart taxChart = new TaxChart(new int[5]);
        taxChart.draw();
    }
}
Brunaldo
  • 324
  • 3
  • 11