Results 1 to 20 of 20
  1. #1
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Need help creating Methods

    I need to:
    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.


    My biggest issue is the methods regarding how to add values to the category arrays and how to then loop through the values to report them. I have most of the code done its just these two parts.



    Java Code:
    public class Budget() {
        public static void main(String[] args)  {
    
      
    //Months for the arrays
      private static final int Months=12;
      
      private static final int January = 0;
      private static final int February = 1;
      private static final int March = 2;
      private static final int April = 3;
      private static final int May = 4;
      private static final int June = 5;
      private static final int July = 6;
      private static final int August = 7; 
      private static final int September = 8;
      private static final int October = 9;
      private static final int November = 10;
      private static final int December = 11;  
    
      //Class Fields 
      Class Budget {
      String[] categories = {“Tuition”, “Food”, “Clothes”, “Fun”, “Car”, “Laundry”};
      double[] tuition = new double[12];
      double[] food = new double[12];
      double[] clothes = new double[12];
      double[] fun = new double[12];
      double[] car = new double[12];
      double[] laundry = new double[12];
      }
      
      
      /*Constructors
       *first is for if there are no values
       * second is for adding first expense
       */
      private class MonthlyBudgetItem  {
    
        //1st Constructor / Defaults to Zero
        private Double amount = 0.0;    
        public MonthlyBudgetItem(Double amount) {
            this.amount=amount;
        }
    
        public Double getAmount() {
            return amount;
        }
        public void setAmount(Double amount) {
            this.amount = amount;
        }
    }
      
      
      //2nd Constructor
       public Budget(String category, double amount) {
       for (i=0; i<12; i++) {
       tuition[i] = 0.0;
      //do same for all arrays
      }
      Date today = new Date();
      addExpense(today.getMonth(), category, amount);
      }
      
      
    
      
      //Methods
             
         //Adds expenses to the budget, wont accept negative numbers
        public boolean addExpense(int categories, double newExpense){
          if (newExpense<0 || this.budgetArray==null)
            return false;
          else{
            this.budgetArray[index] += newExpense;
            return type;
          }
        }
    
          //prints budget report
            int tuitionArray;
            int foodArray;
            int clothesArray;
            int booksArray;
            int travelArray;
            int funArray;
            int total;
        
        Date date1=new Date();
          public string budgetReport {
            System.out.println("Expenses as of "+date1);
            System.out.println("Tuition: $"+tuitionArray); 
            System.out.println("Food: $"+foodArray); 
            System.out.println("Clothes: $"+clothesArray); 
            System.out.println("Books: $"+booksArray); 
            System.out.println("Travel: $"+travelArray); 
            System.out.println("Fun: $"+funArray); 
            System.out.println("Total: $"+total); }
    Last edited by Poles_Apart; 12-01-2011 at 01:07 AM.

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

    Default Re: Need help creating Methods

    You will need to have some way of determining which array the expense goes into. Using the first parameter should do it. You will also need to determine where in the array (which month) the expense goes. Try writing the code, have fun and experiment. That is the best way to learn.

    When posting code you place [ code ] before and [ /code ] after (without the spaces) your code.

  3. #3
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    I have been trying to write the code for two weeks, I'm out of time haha. I'm not quite sure what lines of code I would need to make the for loop read individual values in the array and add them together.

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

    Default Re: Need help creating Methods

    In that case hand in what you have got. If you are expecting someone here to madly whip up a solution for you in the next 5 minutes then don't hold your breath.
    I'm not quite sure what lines of code I would need to make the for loop read individual values in the array and add them together.
    Do you know how to write a for loop?
    Do you know how to access elements in an array?
    Do you know how to perform addition?
    In that case you know how to do this.

  5. #5
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    Sorry if that came off rudely, I was in a rush. This statement is giving me some trouble:


    public boolean addExpense(int categories, double newExpense){
    if (newExpense<0 || this.budgetArray==null)
    return false;
    else{
    this.budgetArray[index] += newExpense;
    return type;
    }
    }

    I'm not sure what to do with it from here. Should the first parameter be string categories or int categories. I'm not sure what to replace budgetArray with to make the value go to its respective field.

    And yes I know how to write a for loop but I do not know how to access specific parts of an array to do what I want. I do not know how to take the value from the 4th month of the array and add it to the 5th month of the array. I went to my professors office hours and he told me to go look it up, which isn't helping much.

  6. #6
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help creating Methods

    Here is an array example for accessing parts, I'll try to make it clear.

    //create a new int array with 5 slots
    int[] anArray = new int[5];

    //put 10 in the first spot in the array
    anArray[0] = 10;

    //put 8 in the third slot in the array
    anArray[2] = 8;


    See what I am doing? I want something to go in the first spot of an array. The first spot has an index of 0, so I put it in "anArray[0]".

    Since I made an int array, I can only put ints into it. I could make an object array if I wanted to put both Strings and Integers, for example.

    If you want to copy something from position 4 to position 5, you can do something like this:

    anArray[5] = anArray[4];

    If there was anything in position 5 to begin with, it is now deleted. position 4 remains intact.

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

    Default Re: Need help creating Methods

    You are probably overthinking things.
    Java Code:
    int[] arr = {1,2,3,4,5};
    for( ) {
        System.out.println( );
    }
    Complete the above code to print out each value in the array. If you can do that then it is not that difficult to modify it to sum all the elements of the array.

  8. #8
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    I see how to do the array if I have the values to put, however the user needs to input the values. It confuses me because i dont know what the value of august is, nor do i know how to add it to the value of September, let alone loop through every month and add it up. This is what im looking at now, I get no errors on the first method but I still dont understand how to use the budgetArray value that the expenses are going into to actually loop individual values and add them together.
    Java Code:
       int budgetArray; 
       public boolean addExpense(string categories, double newExpense){
          if (newExpense<0 || this.budgetArray==null)
            return false;
          else{
            this.budgetArray[index] += newExpense;
            return type;
          }
        }
    
          //prints budget report
            int tuitionArray;
            int foodArray;
            int clothesArray;
            int booksArray;
            int travelArray;
            int funArray;
            int total;
            
            for(int i=0, i<12; i++)
             budgetArray=budgetArray+____________
              return ______;

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

    Default Re: Need help creating Methods

    Look, you are trying to do too much at the same time. Step away from the computer, read your assignment, if there is anything you do not fully understand get clarification from your teacher, concentrate on one thing at a time, sketch things out on a piece of paper until you have it clear in you head what needs to be done, then write the code and test it thouroughly. Only when it is fully working do you move onto the next step.

  10. #10
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help creating Methods

    Your constructors should take 3 arguments, int month, double amount, and String category. This will tell you what to put in, what array to put it in, and where in the array to put it.

    For example

    Java Code:
    //constructor example
    public void budget(int month, double amount, String category){
    if(category.equals("tuition"){
    tuition[month] = amount;
    }
    else if(category.equals("car"){
    car[month] = amount;
    }
    
    // etc etc
    } end constructor

    right now you have way too many data fields and an extra array you don't need at all.

  11. #11
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    Which array do i not need? i fixed up the constructors and a lot more of it makes sense to me now. This is what i have now. I know that the second for statement is incorrect but im not sure where to go from here.
    Java Code:
    public class Budget() {
        public static void main(String[] args)  {
    
      
    //Months for the arrays
      private static final int Months=12;
      
      private static final int January = 0;
      private static final int February = 1;
      private static final int March = 2;
      private static final int April = 3;
      private static final int May = 4;
      private static final int June = 5;
      private static final int July = 6;
      private static final int August = 7; 
      private static final int September = 8;
      private static final int October = 9;
      private static final int November = 10;
      private static final int December = 11;  
    
      //Class Fields 
      Class Budget {
      String[] categories = {“Tuition”, “Food”, “Clothes”, “Fun”, “Car”, “Laundry”};
      double[] tuition = new double[12];
      double[] food = new double[12];
      double[] clothes = new double[12];
      double[] fun = new double[12];
      double[] car = new double[12];
      double[] laundry = new double[12];
      }
      
      
      /*Constructors
       *first is for if there are no values
       * second is for adding first expense
       */
      private class MonthlyBudgetItem  {
    
        //1st Constructor / Defaults to Zero
        private Double amount = 0.0;    
        public MonthlyBudgetItem(Double amount) {
            this.amount=amount;
        }
    
        public Double getAmount() {
            return amount;
        }
        public void setAmount(Double amount) {
            this.amount = amount;
        }
    }
      
      
      //2nd Constructor
       public Budget(int Months, double amount, String category) {
         if (category.equals("tuition"){
           tuition[Months]=amount;
         }
             else if (category.equals("food"){
           food[Months]=amount;
         }
            else if (category.equals("clothes"){
           clothes[Months]=amount;
         }
            else if (category.equals("fun"){
           fun[Months]=amount;
         }
            else if (category.equals("car"){
            car[Months]=amount;
         }
           else if (category.equals("laundry"){
           laundry[Months]=amount;
           }} 
       Date today = new Date();
      addExpense(today.getMonth(), category, amount);
      }
        
    
      
      //Methods
             
         //Adds expenses to the budget, wont accept negative numbers
       int budgetArray; 
       public boolean addExpense(int categories, double newExpense){
          if (newExpense<0 || this.budgetArray==null)
            return false;
          else{
            this.budgetArray[index] += newExpense;
            return type;
          }
        }
    
         for(int i=0, i<12; i++){
             budgetArray=budgetArray+Months+categories; 
              return Months;
              return categories;
       
       
          //prints budget report
            int tuitionArray=Months+categories;
            int foodArray=Months+categories;
            int clothesArray=Months+categories;
            int booksArray=Months+categories;
            int travelArray=Months+categories;
            int funArray=Months+categories;
            int total=tuitionArray+foodArray+clothesArray+booksArray+travelArray+funArray;
           
        
        Date date1=new Date();
          public string budgetReport {
            System.out.println("Expenses as of "+date1);
            System.out.println("Tuition: $"+tuitionArray); 
            System.out.println("Food: $"+foodArray); 
            System.out.println("Clothes: $"+clothesArray); 
            System.out.println("Books: $"+booksArray); 
            System.out.println("Travel: $"+travelArray); 
            System.out.println("Fun: $"+funArray); 
            System.out.println("Total: $"+total); }
    Last edited by Poles_Apart; 12-01-2011 at 05:07 AM.

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

    Default Re: Need help creating Methods

    Actually the instructions state that each expense will have 2 values: category and amount. It aslo states that the method should know in which month the expense occurred. The logical assumption would be that all expenses are for the current month. Take a look at the Calendar class.

  13. #13
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help creating Methods

    Well there are a couple things that aren't used here. You can make use of them if you want, but it's up to you.

    You have all of the months assigned as final int numbers, but do you ever use them? You can if you want, when you go print, but it will require harder to read print statements. Same with your array of categories. Can find a use, but what for? For your int Months = 12, is that doing anything?

    Here is what you *could* do with some of these, if you must keep them. Otherwise, they're unused.

    Java Code:
     
    private static final int Months=12;     
    
    double[] tuition = new double[Months];   
    double[] food = new double[Months];   
    double[] clothes = new double[Months];   
    double[] fun = new double[Months];   
    double[] car = new double[Months];   
    double[] laundry = new double[Months]; 
      
    private static final int January = 0;   
    private static final int February = 1;   
    private static final int March = 2;   
    private static final int April = 3;   
    private static final int May = 4;   
    private static final int June = 5;   
    private static final int July = 6;   
    private static final int August = 7;    
    private static final int September = 8;   
    private static final int October = 9;   
    private static final int November = 10;   
    private static final int December = 11;       
    String[] categories = {“Tuition”, “Food”, “Clothes”, “Fun”, “Car”, “Laundry”};   
      
    /** this statement takes in your month ints, and uses them as the index for the array, and takes in your category array and uses it to print the strings. meh.
    */
    public void printStuff(){
    System.out.println("Expenses for January: ")
    System.out.println(categories[0] + ": " +  tuition[January]);
    System.out.println(categories[1] + ": " +  food[January]);
    System.out.println(categories[2] + ": " +  Clothes[January]);
    }

  14. #14
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    Ok i redid this constructors since i do need only 2 values. So they now accept what category and the amount of money being put into that category. The first method then allows you to add a new expense for the current month (not sure how that works). and the second method (the print one) runs through the current value of every month and adds that to the value of each category and then returns them so they can be added together and printed.
    Java Code:
     public class Budget() {
        public static void main(String[] args)  {
    
      
    //Months for the arrays
      private static final int Months=12;
      
    
      //Class Fields 
      Class Budget {
      String[] categories = {“Tuition”, “Food”, “Clothes”, “Fun”, “Car”, “Laundry”};
      double[] tuition = new double[12];
      double[] food = new double[12];
      double[] clothes = new double[12];
      double[] fun = new double[12];
      double[] car = new double[12];
      double[] laundry = new double[12];
      }
      
      
      /*Constructors
       *first is for if there are no values
       * second is for adding first expense
       */
      private class MonthlyBudgetItem  {
    
        //1st Constructor / Defaults to Zero
        private Double amount = 0.0;    
        public MonthlyBudgetItem(Double amount) {
            this.amount=amount;
        }
    
        public Double getAmount() {
            return amount;
        }
        public void setAmount(Double amount) {
            this.amount = amount;
        }}
    
      
      
      //2nd Constructor
       public Budget(String category, double amount) {
         for (i=0; i<12; i++) {
         tuition[i] = amount;
         }
         for (i=0; i<12; i++) {
         food[i] = amount;
         }
         for (i=0; i<12; i++) {
         clothes[i] = amount;
         }
         for (i=0; i<12; i++) {
         fun[i] = amount;
         }
         for (i=0; i<12; i++) {
         car[i] = amount;
         }
         for (i=0; i<12; i++) {
         laundry[i] = amount;
         }} 
        
      //Methods
             
         //Adds expenses to the budget, wont accept negative numbers
       int budgetArray; 
       public boolean addExpense(int categories, double newExpense){
          if (newExpense<0 || this.budgetArray==null)
            return false;
          else{
            this.budgetArray[index] += newExpense;
            return type;
          }
        }
             
             for(int i=0, i<12; i++){
             budgetArray=Months+categories; 
              return Months;
              return categories;
       
       
          //prints budget report
            int tuitionArray=Months+categories;
            int foodArray=Months+categories;
            int clothesArray=Months+categories;
            int booksArray=Months+categories;
            int travelArray=Months+categories;
            int funArray=Months+categories;
            int total=tuitionArray+foodArray+clothesArray+booksArray+travelArray+funArray;
           
        
        Date date1=new Date();
          public string budgetReport {
            System.out.println("Expenses as of "+date1);
            System.out.println("Tuition: $"+tuitionArray); 
            System.out.println("Food: $"+foodArray); 
            System.out.println("Clothes: $"+clothesArray); 
            System.out.println("Books: $"+booksArray); 
            System.out.println("Travel: $"+travelArray); 
            System.out.println("Fun: $"+funArray); 
            System.out.println("Total: $"+total); }
    Last edited by Poles_Apart; 12-01-2011 at 05:28 AM.

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

    Default Re: Need help creating Methods

    No!

    Why are you adding the amount to every slot in every array? All your constructor should do is call the addExpense method and pass on the parameters. Then you need to get your logic correct in the addExpense method. Once again I strongly urge you to step away from the computer until you have sketched out an algorithm on paper.

  16. #16
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Need help creating Methods

    Now your constructor is taking the amount and putting it into every slot of every category.

    You can do this:

    public Budget(String category, double amount) {
    if(category.equals("tuition"){ // choose the right category
    for(int i = 0; i<tuition.length;i++){ // go through the correct array looking for the first empty spot to place the amount, so you don't override existing data
    if(tuition[i] == null){ // if you find an empty spot
    tuition[i] = amount; // add the amount to that spot

    }
    }
    }
    }

  17. #17
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    Believe me, I would love to step away and take a break but its getting late and i need to finish this tonight. i understand what you mean with the constructors and i fixed that up using herahs example. The part that i dont understand is how to make the categories go into a specific month to be added together. I set up the second method and named it addValues so that it runs through each month and adds the total value of each month together.

    Java Code:
     ....
     public Budget(String category, double amount) {
       if(category.equals("laundry"){
       for(int i = 0; i<laundry.length;i++){
       if(laundry[i] == null){ 
       laundry[i] = amount; 
       }}} 
         
      //Methods
             
         //Adds expenses to the budget, wont accept negative numbers
       int budgetArray; 
       public boolean addExpense(int categories, double newExpense){
          if (newExpense<0 || this.budgetArray==null)
            return false;
          else{
            this.budgetArray[index] += newExpense;
            return type;
          }
        }
          
          int finalvalue;
          
          public boolean addValues(int months){
             for(int i=0, i<12; i++){
             finalvalue += months+i; 
              return months;
              
       
       
          //prints budget report
            int tuitionArray=finalvalue;
            int foodArray=finalvalue;
            int clothesArray=finalvalue;
            int carArray=finalvalue;
            int travelArray=finalvalue;
            int funArray=finalvalue;
            int total=tuitionArray+foodArray+clothesArray+carArray+travelArray+funArray;
           
        
        Date date1=new Date();
          public string budgetReport {
            System.out.println("Expenses as of "+date1);
            System.out.println("Tuition: $"+tuitionArray); 
            System.out.println("Food: $"+foodArray); 
            System.out.println("Clothes: $"+clothesArray); 
            System.out.println("Books: $"+carArray); 
            System.out.println("Travel: $"+travelArray); 
            System.out.println("Fun: $"+funArray); 
            System.out.println("Total: $"+total); }

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

    Default Re: Need help creating Methods

    In that case you are never going to get this finished. I hate to be blunt but you will just continue bashing away at the keyboard going around in circles typing rubbish code because you have no clear idea what you are doing.

  19. #19
    Poles_Apart is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Need help creating Methods

    Alright man i appreciate the help.

  20. #20
    IamJava is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default Re: Need help creating Methods

    If you truly need help just let me know. I helped someone in your class with this assignment.

    Check your powerpoint slides. Your teacher gave the constructor and also the addExpense method.There is also examples in your notes on how to read the scanner file to read the input file and parse. And also create an output file. You need to use the filechooser class. PickAfile method to get the path and filename.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Creating Static Methods
    By strugglingbeginner in forum New To Java
    Replies: 9
    Last Post: 07-11-2010, 12:35 AM
  3. Creating Methods Assistance
    By Desmond in forum New To Java
    Replies: 10
    Last Post: 03-15-2010, 03:21 PM
  4. creating objects by methods automatically
    By thedarlington in forum New To Java
    Replies: 13
    Last Post: 02-07-2010, 10:48 PM
  5. Creating Blocking Methods
    By Singing Boyo in forum Advanced Java
    Replies: 5
    Last Post: 06-11-2009, 10:44 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
  •