Results 1 to 4 of 4
  1. #1
    mrjaeyun is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Trouble with arrays Help

    The instruction for my assignment is the following

    Create a class to maintain a personal budget for a year. The name of this class is Budget. The data your class will need, at a minimum, are:

    an array of categories (like Tuition, Food, Clothes, etc). You should have at least six categories. You can use your own expenses as a guide.

    an array for EACH category, storing the total monthly expenses for that category (think – how many items would be in each array?)

    Create a constructor for the budget. You should have at least two constuctors – one if you have no expenses to record yet, and one if you are adding your first expense. Remember, each expense has two “fields” - the category name and the amount.

    Create a method to add an expense to the budget. This method should know in which month the expense occurred, so figure out how many, and what data type, parameters are needed.

    Create a method to print a report of the budget. Each category's current yearly total should appear on a separate line, and the final line would show the total expenses so far spent this year. For instance, a sample output might be:

    > Expenses as of Nov 4, 2011:

    > Tuition: $3200

    > Food: $2600

    > Clothes: $600

    > Books: $450

    > Total expenses: $6850


    This is the coding i have so far:

    public class Budget{

    ///////////////fields////////////////
    private String expenseName;
    int[] expenseAmount = {1000, 2000, 3000, 4000, 5000, 6000};
    int[] monthNumber = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};




    /////////constructors///////////////
    public Budget() {}

    public Budget(String name) {this.expenseName = name;}

    public Budget(String name, int num)
    {
    this.expenseName = name;
    this.expenseAmount = num;
    this.monthNumber = num;
    }


    /////////////methods///////////
    public String getexpenseName() {return expenseName;}

    public int getexpenseAmount() {return expenseAmount;}

    public int getmonthNumber() {return monthNumber;}

    public void setExpenseName(String name)
    {
    this.expenseName = name;
    }

    public boolean setexpenseAmount(int num)
    {
    if (this.expenseAmount == 0)
    {this.expenseAmount = num;
    return true;
    }
    else
    return false;
    }}


    I keep getting errors saying Type mismatch: cannot convert from int to int[]
    someone help

  2. #2
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Trouble with arrays Help

    First of all, there are actually alot of errors in your code. You should probably go over the basics.

    That aside, in your constructor :
    Java Code:
    /////////constructors///////////////
    public Budget() {}
    
    public Budget(String name) {this.expenseName = name;}
    
    public Budget(String name, int num)
    {
    this.expenseName = name;
    this.expenseAmount = num;
    this.monthNumber = num;
    }
    this.expenseAmounts and this.monthNumber is expecting an array object. Thats why its keeps giving you the error, cannot convert int to int[]. If you want to pass in or take out a value
    from the array you should define where in the array you want to put it in ie expenseAmounts[i] where [i] is the arrangement of the value in the array starting from 0.

  3. #3
    mrjaeyun is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Trouble with arrays Help

    public class Budget{

    ///////////////fields////////////////
    private String expenseName;
    int expenseAmount[] = {1000, 2000, 3000, 4000, 5000, 6000};
    int monthNumber[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};




    /////////constructors///////////////
    public Budget() {}

    public Budget(String name) {this.expenseName = name;}

    public Budget(String name, int num)
    {
    this.expenseName = name;
    this.expenseAmount[1] = num;
    this.monthNumber[3] = num;
    }


    /////////////methods///////////
    public String getexpenseName() {return expenseName;}

    public int getexpenseAmount() {return expenseAmount[1];}

    public int getmonthNumber() {return monthNumber[1];}

    public void setExpenseName(String name)
    {
    this.expenseName = name;
    }

    public boolean setexpenseAmount(int num)
    {
    if (this.expenseAmount[0] == 0)
    {this.expenseAmount[1] = num;
    return true;
    }
    else
    return false;
    }}


    I put the [] brackets where it was needed and put temporary numbers within them for compiling sakes. Could you direct on how to go about doing the assignment?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Trouble with arrays Help

    Sounds like you are just guessing.

    If you want an explanation of the assignment then you should talk to your teacher. If you have a specific question about your code then ask it and we can provide a specific answer. "I don't know what to do" is not specific and very difficult for us to answer.

Similar Threads

  1. More trouble with classes and arrays
    By CuddlyKittens11 in forum Advanced Java
    Replies: 9
    Last Post: 04-27-2011, 01:25 AM
  2. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 12:42 AM
  3. Arrays + Final = Trouble
    By Bgreen7887 in forum New To Java
    Replies: 8
    Last Post: 11-30-2010, 11:23 PM
  4. Arrays trouble
    By gto400no1 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 01:20 AM
  5. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 01:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •