Results 1 to 4 of 4
Thread: Trouble with arrays Help
- 11-07-2011, 05:54 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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
- 11-07-2011, 08:35 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
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 :
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 valueJava 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; }
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.
- 11-07-2011, 10:05 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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?
- 11-07-2011, 10:14 PM #4
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
-
More trouble with classes and arrays
By CuddlyKittens11 in forum Advanced JavaReplies: 9Last Post: 04-27-2011, 01:25 AM -
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
Arrays + Final = Trouble
By Bgreen7887 in forum New To JavaReplies: 8Last Post: 11-30-2010, 11:23 PM -
Arrays trouble
By gto400no1 in forum New To JavaReplies: 1Last Post: 04-14-2010, 01:20 AM -
HELP: Still having trouble getting arrays :(
By Psyclone in forum New To JavaReplies: 4Last Post: 02-06-2010, 01:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks