Results 1 to 7 of 7

Thread: Assignment Help

  1. #1
    Albany is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Assignment Help

    Im having trouble with this assignment I do not seem to understand how to send a copy of the print out of my Budget() my teacher also wants us to use the split on "\t" for example s.split("\t")....im not sure how this works as well. Any help at all with this assignment would be greatly appreciated. Here is the assignment below as well as what I have so far.



    You will be adding methods to the Budget class you created in Assignment 4. First of all, you will add two new data fields. One will contain the year when these expenses are incurred. This allows us to maintain annual objects for archival (and perhaps tax) purposes. The other field will be the name of the person incurring the expenses. Add necessary accessor and modifier methods.
    Create an accessor method called getTotal. It will return the current year’s total expenses for a particular category. The category will be passed as a parameter.
    Create a method called load. This method receives the path and name of a file as input. The referenced file will contain expense data. The first line of the file will be the year in which the expenses occurred. The remaining lines have the following format: Month Category Amount. A sample line might be this:
    February Food $ 21.89
    Create a method to send a copy of the budget report to a file named Personal Expenses yyyy.txt, where yyyy is the four-digit year that these expenses are for. The file will contain output that looks much like that created in Assignment 4, showing the current date, the total expenses in each category, and the total annual expenses. A sample is shown below:
    Expenses as of Nov 4, 2011:
    Tuition: $3200
    Food: $2600
    Clothes: $600
    Books: $450
    Total expenses: $6850





    Java Code:
    import java.io.*;
    public class Budget
    {
      //////////////////////////////////////////////FIELDS////////////////////////////////////////////////
      private int[] food;
      private int[] clothes;
      private int[] gas;
      private int[] tuition;
      private int[] cable;
      private int[] books;
      private String[] month;
      private int numFood;
      private int numClothes;
      private int numGas;
      private int numTuition;
      private int numCable;
      private int numBooks;
      private int numMonth;
      private String totalExpense;
      //////Fields for assignment 5////////
      private String yearOfExpense;
      private String namePerson;
      private int total;
      
      
      ////////////////////////////////////////////CONSTRUCTORS/////////////////////////////////////////////
      
      public Budget()
      {
        food = new int[12];
        clothes = new int[12];
        gas = new int[12];
        tuition = new int[12];
        cable = new int[12];
        books = new int[12];
        month = new String[12];
        numFood = 0;
        numClothes = 0;
        numGas = 0;
        numTuition = 0;
        numCable = 0;
        numBooks = 0;
        numMonth = 1;
      }
      
      ///////////////////////////////////////////////////METHODS////////////////////////////////////////////
      /////////////accessors and modifiers
        public String getYearOfExpense() {return yearOfExpense;}
        public void setYearOfExpense(String yearOfExpense)
        {
          this.yearOfExpense = yearOfExpense;
        }
        public String getNamePerson() {return namePerson;}
        public void setNamePerson(String namePerson)
        {
          this.namePerson = namePerson;
        }
        public int getTotal() 
        {
          return total;
        }
    ///////////////////recieves path and name of file as input////////////////////
        public void load(String fileName)
        {
          String line = null;
        try{
          BufferedReader reader = new BufferedReader(new FileReader(fileName));
          while((line = reader.readLine()) !=null)
          {
            System.out.println(line);
          }
          reader.close();
        }
        catch(FileNotFoundException ex)
        {
          line =  "Could not fine " + fileName;
          line = line + ". Please select one:";
          SimpleOutput.showError(line);
          fileName = FileChooser.pickAFile();
          load(fileName);
        }
        catch(Exception ex){
          SimpleOutput.showError("Error reading file " + fileName);
          ex.printStackTrace();
        }
      }
    ///////////Method to send copy/////////
        public void sendACopy()
        {
         Budget k = new Budget();
         k.addFood(500);
           k.addClothes(750);
           k.addGas(670);
           k.addTuition(430);
           k.setMonth(3);
           
        }
        
        
      ////////////////adds expense to food//////////////
      public boolean addFood(int foodExpense)
      {
        if( numFood < 12)
        {
          numFood = numFood + 1;
          food[numFood] = foodExpense;
          foodExpense = total;
          return true;
        }
        else
        {
          return false;
        }
      }
      /////////////adds expense to clothes////////////
      public boolean addClothes(int clothesExpense)
      {
        if( numClothes < 12)
        {
          numClothes = numClothes + 1;
          clothes[numClothes] = clothesExpense;
          clothesExpense = total;
          return true;
        }
        else
        {
          return false;
        }
      }
      ////////////adds expense to gas////////////
      public boolean addGas(int gasExpense)
      {
        if( numGas < 12)
        {
          numGas = numGas + 1;
          gas[numGas] = gasExpense;
          gasExpense = total;
          return true;
        }
        else 
        {
          return false;
        }
      }
      //////////////adds expense to tuition///////////////
      public boolean addTuition(int tuitionExpense)
      {
        if( numTuition < 12)
        {
          numTuition = numTuition + 1;
          tuition[numTuition] = tuitionExpense;
          tuitionExpense = total;
          return true;
        }
        else
        {
          return false;
        }
      }
      ///////////////adds expense to cable/////////////
      public boolean addCable(int cableExpense)
      {
        if(numCable < 12)
        {
          numCable = numCable + 1;
          cable[numCable] = cableExpense;
          cableExpense = total;
          return true;
        }
        else
        {
          return false;
        }
      }
      /////////////////adds expense to books//////////////////////
      public boolean addBooks(int booksExpense)
      {
        if(numBooks < 12)
        {
          numBooks = numBooks + 1;
          books[numBooks] = booksExpense;
          booksExpense = total;
          return true;
        }
        else
        { 
          return false;
        }
      }
      ///////////sets month for expenses//////////////
      public void setMonth(int monthinput)
      {
        month[0] = "Janurary";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";
        }
      
      ///////////////////////////////////REPORT//////////////////////////////
      public String toString(String totalExpense)
      {
        //////////////////////////////////////////////////////Prints Total Food Expense////////////////////////
        String totalFood = "Food: $";
        for(int f = 0; f < numFood; f++)
        {
          totalFood = totalFood + food[f+1];
        }
        System.out.println(totalFood);
        
        //////////////////////////////////////prints Total Clothes Expense////////////////////////////////
        String totalClothes = "clothes: $";
        for(int e = 0; e < numClothes; e++)
        {
          totalClothes = totalClothes + clothes[e+1];
        }
        System.out.println(totalClothes);
        
        //////////////////////////////////////prints Total Gas Expense///////////////////////////////////////
        String totalGas = "Gas: $";
        for(int g = 0; g < numGas; g++)
        {
          totalGas = totalGas + gas[g+1];
        }
        System.out.println(totalGas);
        
        ///////////////////////////////////////prints Total Tuition Expense///////////////////////////////
        String totalTuition = "Tuition: $";
        for(int t = 0; t < numTuition; t++)
        {
          totalTuition = totalTuition + tuition[t+1];
        }
        System.out.println(totalTuition);
        
        ///////////////////////////////////prints Total Cable Expense/////////////////////////////
        String totalCable = "Cable: $";
        for(int c = 0; c < numCable; c++)
        {
          totalCable = totalCable + cable[c+1];
        }
        System.out.println(totalCable);
        
        /////////////////////////////////prints Total books expense///////////////////////////////////////
        String totalBooks = "Books: $";
        for(int b = 0; b < numBooks; b++)
        {
          totalBooks = totalBooks + (books[b+1]);
        }
        System.out.println(totalBooks);
        
        
        
        
        /////////////////Prints out total expenses per year according to month/////////////////////
        for( int i = 0; i < month.length; i++)
        {
          System.out.println( "Total expenses as of " + month[i]);
        for(int y = 0; y < numMonth; y++)
        {
          totalExpense = totalExpense + (food[y+1] + cable[y+1] + gas[y+1] + tuition[y+1] + books[y+1] + clothes[y+1]);
        
        }}
        
        return totalExpense;
         }
    public void output()
    {
       System.out.println(totalExpense);
    }
    }
    Last edited by Norm; 12-02-2011 at 11:23 PM. Reason: added code tags

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: Assignment Help

    Holy jeebus code dump. Please use [code][/code] tags when posting code and please use a Short, Self Contained, Correct Example when posting questions with code.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default Re: Assignment Help

    Ok sorry man this is my first time posting but is there anyway you can help me

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Assignment Help

    Quote Originally Posted by Albany View Post
    Ok sorry man this is my first time posting but is there anyway you can help me
    Yes, read my link below titled "So, You Need to Write a Program but Don't Know How to Start". Then after following the suggestions found, if you're still stuck, you can post back and be able to ask an answerable question.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Assignment Help

    im not sure how this works
    First read the API.
    Second do a search on the Forum of code samples that use that method.
    Third write a small simple program that uses that method and test it and see what happens.
    You could use the Arrays toString() method to format the array for printing.

  6. #6
    Albany is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Assignment Help

    Ok I think I have a more general question in the directions here it says:
    /////////////////
    Create a method to send a copy of the budget report to a file named Personal Expenses yyyy.txt, where yyyy is the four-digit year that these expenses are for. The file will contain output that looks much like that created in Assignment 4, showing the current date, the total expenses in each category, and the total annual expenses. A sample is shown below:
    Expenses as of Nov 4, 2011:
    Tuition: $3200
    Food: $2600
    Clothes: $600
    Books: $450
    /////////
    My coding here for this part of the assignment:
    //////////////////
    public String copyAFile()
    {
    Budget k = new Budget();
    k.addFood(500);
    k.addClothes(600);
    k.addBooks(700);
    k.addGas(850);
    k.setMonth(1);
    }
    ////////
    Now my question is How to send the copy of the budget report to the file.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: Assignment Help

    How to send the copy of the budget report to the file.
    Use a class like FileWriter that writes to a file. Do a Search on the forum for code samples that use it.
    It will need to be wrapped in another class that has better methods for writing output, like the BufferedWriter. The BufferedWriter's API doc has a small example of how to use it.

Similar Threads

  1. Help With Assignment
    By EpyonCustom in forum New To Java
    Replies: 10
    Last Post: 04-03-2011, 06:04 AM
  2. Can somebody help me in this assignment?
    By lulzim in forum New To Java
    Replies: 25
    Last Post: 03-01-2011, 11:02 PM
  3. Can anyone help me in my Assignment
    By nono in forum New To Java
    Replies: 18
    Last Post: 11-15-2010, 05:52 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 04:23 AM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 07:29 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
  •