I get NaN for all the values when I run the code. Not sure why? Here is the code.
And for the tester.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; } }
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); } }