Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Problem Calculating emmisions

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Problem Calculating emmisions

    I get NaN for all the values when I run the code. Not sure why? Here is the code.
    import java.util.ArrayList;
    class CO2FromElectricity
     
    {
       CO2FromElectricity()
       {
       	//default constructor to be used
       }
     
       public double calcAverageBill(ArrayList<Double> monthlyBill)
       {
           // **** insert code for method here ****//
           monthlyBill = new ArrayList<Double>();
           double aveBill = 0;
           double bIS = 0;
           for(int i = 0; i < monthlyBill.size(); i++)
           bIS += monthlyBill.get(i);
           aveBill = bIS / monthlyBill.size();
           return aveBill;
       }
     
       public double calcAveragePrice (ArrayList<Double> monthlyPrice)
       {
            // **** insert code for method here ****//
            monthlyPrice = new ArrayList<Double>();
           double avePrice = 0;
           double pS = 0;
           for(int i = 0; i < monthlyPrice.size(); i++)
           pS += monthlyPrice.get(i);
           avePrice = pS / monthlyPrice.size();
           return avePrice;
       }
     
       public double calcElectricityCO2 (double avgBill, double avgPrice)
       {
            // **** insert code for method here ****//
             double emmisions = ((avgBill / avgPrice)*1.37*12);
             return emmisions;
       }
    }
    And for the tester.
    import java.util.ArrayList;
    public class CO2FromElectricityTester
    {
       public static void main(String[] args)
       {
           	CO2FromElectricity CO2 = new CO2FromElectricity();
     
           	ArrayList<Double> monthlyBill = new ArrayList<Double>();
           	ArrayList<Double> monthlyPrice = new ArrayList<Double>();
     
           	//Values to add to the monthly bill or use your own:
           	monthlyBill.add(209.60);
            monthlyBill.add(249.68); 
            monthlyBill.add(222.59);
            monthlyBill.add(209.60);
            monthlyBill.add(249.68); 
            monthlyBill.add(222.59);
            monthlyBill.add(209.60);
            monthlyBill.add(249.68); 
            monthlyBill.add(222.59);
            monthlyBill.add(209.60);
            monthlyBill.add(249.68); 
            monthlyBill.add(222.59);
     
           	// Values to add to the monthly Price or use your own:
    		// 
     
    		monthlyPrice.add(209.70 / 2464);
    		monthlyPrice.add(249.68 / 2948);
    		monthlyPrice.add(222.59 / 2621);
    		monthlyPrice.add(209.70 / 2464);
    		monthlyPrice.add(249.68 / 2948);
    		monthlyPrice.add(222.59 / 2621);
    		monthlyPrice.add(209.70 / 2464);
    		monthlyPrice.add(249.68 / 2948);
    		monthlyPrice.add(222.59 / 2621);
    		monthlyPrice.add(209.70 / 2464);
    		monthlyPrice.add(249.68 / 2948);
    		monthlyPrice.add(222.59 / 2621);
     
           double avgBill = CO2.calcAverageBill(monthlyBill);
           double avgPrice = CO2.calcAveragePrice(monthlyPrice);
     
           double emissions = CO2.calcElectricityCO2(avgBill, avgPrice);
     
           System.out.printf("Average Monthly Electricity Bill: %6.2f%n", avgBill);
           System.out.printf("Average Monthly Electricity Price: %4.2f%n", avgPrice);
           System.out.printf("Annual CO2 Emissions from Electricity Usage:   %7.1f pounds", emissions);
       }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Problem Calculating emmisions

    I recommend stepping through this with a debugger, or at least adding some print statements, to figure out what's going on with your code. Hint: make sure you use {} after if statements and for loops!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Problem Calculating emmisions

    You're not using the values passed to the CO2 methods, but creating empty/null values to use instead:
       public double calcAverageBill(ArrayList<Double> monthlyBill)
       {
           // **** insert code for method here ****//
           monthlyBill = new ArrayList<Double>();
           double aveBill = 0;
           double bIS = 0;
           for(int i = 0; i < monthlyBill.size(); i++)
           bIS += monthlyBill.get(i);
           aveBill = bIS / monthlyBill.size();
           return aveBill;
       }
    If you delete or comment out the line,

    monthlyBill = new ArrayList<Double>();

    all should be well. Do you understand why? If not, please ask.

  4. #4
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Problem Calculating emmisions

    Wait why?
    Also should I do the same thing for Monthly Price.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Problem Calculating emmisions

    If you understood why, you wouldn't have to ask, but try it and see what happens. Then figure out why. If you can't, ask.

    Oh, wait a minute. You did ask.

    In the method calcAverageBill(), the ArrayList<Double> of values is passed as an argument called 'monthlyBill.' That's what the method's signature means:

    public double calcAverageBill(ArrayList<Double> monthlyBill)

    Then, the following statement declares and initializes a new LOCAL variable ArrayList<Double> of null values:

    monthlyBill = new ArrayList<Double>();

    replacing the values passed to the method. From there, all calculations done by the method use the monthlyBill ArrayList that is essentially empty (though initialized with some number of null values.)

    Does that make sense? And, yes, you need to do the same with the calcAveragePrice() method.
    Last edited by GregBrannon; August 6th, 2013 at 10:38 AM. Reason: Fixed typos.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    llowe29 (August 6th, 2013)

  7. #6
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Problem Calculating emmisions

    I realized it worked but don't understand why the value would be null.

    --- Update ---

    Oh now I see it's because its already initialized in param, so when you reinitialize in the subcode itself it is null, I suppose , because Java is sequential, it takes the null value. Thanks.

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Problem Calculating emmisions

    Maybe you get it, but I can't tell from your choice of words. I'm not sure about the sequential part.

    I would say it differently: The values passed from the main() method to the CO2FromElectricity methods in the argument variables were replaced by new local variables of the same name (that's why they are replaced) that are initialized empty/null.

Similar Threads

  1. Calculating Pi to more digits?
    By sci4me in forum Java Theory & Questions
    Replies: 5
    Last Post: May 23rd, 2013, 01:41 PM
  2. [SOLVED] Calculating Commission with a GUI???
    By jbarcus81 in forum AWT / Java Swing
    Replies: 13
    Last Post: February 25th, 2012, 06:35 AM
  3. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 07:26 PM
  4. Calculating Strings?
    By SkyAphid in forum Java Theory & Questions
    Replies: 2
    Last Post: August 30th, 2011, 09:38 PM
  5. question on calculating
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 9th, 2010, 05:06 PM