Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By JosAH

Thread: Issue With Methods

  1. #1
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Issue With Methods

    Hello everyone! I am quite new to java so I'm sorry in advance if my code in horrible. However, I am trying to write a program that will calculate the amount of CO2 released into the environment based on a couple of things: if you recycle, how many people are in the household, etc. My code is pretty much finished, however the output isn't working correctly. Here is the code for the "main" class with the main method inside of it:

    Java Code:
    public static void main(String[] args) //beginning of the main method
        {
            ArrayList<CO2FromWaste> CO2 = new ArrayList<CO2FromWaste>(); //declares a new ArrayList that uses objects
            //add a whole bunch of different objects and values to our variables over in the other class
            CO2.add(new CO2FromWaste(1, true, true, true, true));
            CO2.add(new CO2FromWaste(2, false, false, false, false));
            CO2.add(new CO2FromWaste(3, true, true, false, false));
            CO2.add(new CO2FromWaste(4, false, false, true, true));
            CO2.add(new CO2FromWaste(5, true, false, true, false));
            CO2.add(new CO2FromWaste(6, false, true, false, true));
        
            CO2FromWaste dataRecord; //declare the data record
            
            //print the beginning of the output
            System.out.printf("%15s%50s%n", "Household Waste Recycled", "Pounds of CO2");
            System.out.printf("%5s%10s%10s%10s%10s%10s%20s%20s%20s%n", "Index", "People", "Paper", "Plastic", "Glass", "Cans", "Total Emission", "Reduction", "Net Emission");
            System.out.println("_______________________________________________________________________________________________________________________________________");
            
            for(int i = 0; i < CO2.size(); i++) //make a for loop that will loop for the total size of our CO2 ArrayList
            {
                //use the data record to call on the methods and then print the rest of the output
                dataRecord = CO2.get(i);
                dataRecord.calcGrossWasteEmission(); 
                dataRecord.calcWasteReduction(); 
                dataRecord.calcNetWasteReduction(); 
                System.out.printf("%5d %9d %9s %9s %9s %9s %19.2f %19.2f %19.2f %n", i, dataRecord.getNumPeople(), dataRecord.isPaper(), dataRecord.isPlastic(), dataRecord.isGlass(), dataRecord.isCans(), dataRecord.getGrossWaste(), dataRecord.getGrossReduction(), dataRecord.getGrossNet());
    I don't believe there is anything wrong with this portion of the code, I think it lies in the second portion:

    Java Code:
    //declare our private variables
        private int numPeople;
        private boolean paper, plastic, glass, cans;
        private double grossWaste, grossReduction, grossNet;
      
        /**
         * This constructor receives the input for the private variables declares above in its parameters. It 
         * then assigns the values that were in the parameters to the private variables.
         */
        CO2FromWaste(int numPPL, boolean paper, boolean plastic, boolean glass, boolean cans)
        {
            setNumPeople(numPPL);
            paper = paper;
            plastic = plastic;
            glass = glass;
            cans = cans;
        }
        
        /**
         * This method is void (returns nothing) and receives nothing in its parameters.
         * This method simply calculates the gross waste and assigns it to the corresponding variable 
         * at the beginning of our class
         */
        public void calcGrossWasteEmission()
        {
            setGrossWaste(getNumPeople() * 1018);
        }
        
        /**
         * This method is also void (returns nothing) and receives nothing in its parameters as well.
         * This method finds out through if statements, if the household described recycles any of the following.
         * If they do, then the program subtracts that amount from the total CO2 output.
         */
        public void calcWasteReduction()
        {
            if (isPaper() == true)
                    setGrossReduction(getGrossReduction() + (184 * getNumPeople()));  
            if (isPlastic() == true)
                    setGrossReduction(getGrossReduction() + (25.6 * getNumPeople())); 
            if (isGlass() == true)
                    setGrossReduction(getGrossReduction() + (46.6 * getNumPeople())); 
            if (isCans() == true)
                    setGrossReduction(getGrossReduction() + (165.8 * getNumPeople())); 
        }
        
        /**
         * This method is also void (returns nothing) and receives nothing in its parameters as well.
         * This method calculates the gross Net CO2 output by subtracting the total CO2 saved from recycling (method above)
         * from the total CO2 output which are based on the amount of people in a household.
         */
        public void calcNetWasteReduction()
        {
            setGrossNet(getGrossWaste() - getGrossReduction());
        }
    This is just the portion of the code I am having issues with, the rest consist of getter and setter methods, which I don't think are contributing to the problem. Whenever I run the program the output says that the boolean methods from the constructor are all false, thus making the public void calcWasteReduction() method do nothing. I think the issue lies in that method, as the variables it uses are private, thus allowing it no access. Can anyone help me?

    Thanks!

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Issue With Methods

    You want a hint?
    - Never, ever name local variables the same as attributes...

    Honestly if someone comes to you and brings you this:
    paper = paper;
    plastic = plastic;
    glass = glass;
    cans = cans;
    Wouldn't you think he is somehow... confused? ;))
    For a first try, the "this" keyword may help in clarifying what you mean!
    I like likes!

  3. #3
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Issue With Methods

    Wow...that fixed it. Thanks Sierra! I can't believe it was that simple. I'll never ever name two things the same again! Thanks again so much!

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Issue With Methods

    Don't be scared away from member variables and parameters with the same name; 'this.paper' and 'paper' (in your example) solve this little inconvenience and it doesn't affect the size of the code, nor does it increase the code speed ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Issue With Methods

    Quote Originally Posted by Sierra View Post
    You want a hint?
    - Never, ever name local variables the same as attributes...

    Honestly if someone comes to you and brings you this:
    paper = paper;
    plastic = plastic;
    glass = glass;
    cans = cans;
    Wouldn't you think he is somehow... confused? ;))
    For a first try, the "this" keyword may help in clarifying what you mean!
    It's a very common technique.
    I use it all the time so that I can easily map parameters to attributes.
    I know (using the above) that the 'paper' parameter will be mapped to the 'paper' attribute.

    Of course I also ensure I use 'this'...well, the IDE does anyway.
    :)
    DarrylBurke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Issue With Methods

    Well "common technique" does not need to mean it is "good to do"... I see all the time people walking over red traffic lights, that does not make it safe to do so, right? ;)
    It is a no go in my opinion exactly because of that problem, especially for beginners. Just make attributes have a prefix and you don't need neither "this" nor rely on your IDE to tell you.
    And you need not to bother with forgetting accidentially and doing many hours of debugging because of such simple things...

    Of course that's just my poor opinion. :)
    Last edited by Sierra; 12-11-2012 at 05:38 PM.
    I like likes!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Issue With Methods

    Prefixes are naff.
    Down that road leads Hungarian Notation...
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Issue With Methods

    If you don't like the Hungarians, just stay with the cool stuff. ;)
    I like likes!

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Issue With Methods

    Quote Originally Posted by Sierra View Post
    Just make attributes have a prefix and you don't need neither "this" nor rely on your IDE to tell you.
    And you need not to bother with forgetting accidentially and doing many hours of debugging because of such simple things...
    I'd rather rely on my IDE than on my own sloppy, inconsistent behaviour ...

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-23-2011, 12:37 PM

Tags for this Thread

Posting Permissions

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