0

I have a guide for this program and I am supposed to use an object CO2 somehow and add the values in. I am not sure where to implement the object CO2 that is declared in the beginning and i am getting a cannot find symbol error whenever i try to add to the list. I am using to classes to achieve this.

CO2 FROM ELECTRICITY TESTER CODE

/**
 * @purpose: Calculate yearly CO2 emissions from electricity 8.10
 *
 * @author:
 * @version:
 */
import java.util.ArrayList;

public class CO2FromElectricityTester {

    public static void main(String[] args) {
        CO2FromElectricity CO2 = new CO2FromElectricity();

        ArrayList<Double> monthlyBill = new ArrayList<Double>(3);

        ArrayList<Double> monthlyPrice = new ArrayList<Double>(3);

        // Values to add to the monthly bill or use your own:
        // 209.60, 249.68. 222.59
        monthylyBill.add(209.60);
        monthylyBill.add(249.68);
        monthylyBill.add(222.59);

        // Values to add to the monthly Price or use your own:
        // (209.70 / 2464), (249.68 / 2948), (222.59 / 2621)
        monthylyPrice.add(0.24);
        monthylyPrice.add(0.35);
        monthylyPrice.add(0.27);

        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);
    }
}

CO2 FROM ELECTRICITY CLASS

import java.util.ArrayList;

public class CO2FromElectricity

{
    CO2FromElectricity() {
        // default constructor should be used.
    }

    public double calcAverageBill(ArrayList<Double> monthlyBill) {
        double sum = 0;
        for (int i = 0; i < monthlyBill.size(); i++) {
            sum += monthlyBill.get(i);
        }
        return ((double) sum) / monthlyBill.size();
    }

    public double calcAveragePrice(ArrayList<Double> monthlyPrice) {
        double sum = 0;
        for (int i = 0; i < monthlyPrice.size(); i++) {
            sum += monthlyPrice.get(i);
        }
        return ((double) sum) / monthlyPrice.size();
    }

    public double calcElectricityCO2(double avgBill, double avgPrice) {
        return ((double) (avgBill / avgPrice) * 1.37 * 12);
    }
}
5
  • 1
    Can you post the exact exception message you are getting? Also the line # would be helpful. Commented Jan 17, 2016 at 16:41
  • I am getting a connot find symbol error on the first add statement in the first block of code. monthylyBill.add(209.60); Commented Jan 17, 2016 at 16:42
  • 1
    Because you have a typo, monthylyBill should be monthlyBill Commented Jan 17, 2016 at 16:44
  • 1
    Jesus christ... I need more sleep. Thanks that solved that, but where does the CO2 Object come in? The assigment instructions say i have to use it but I don't know where Commented Jan 17, 2016 at 16:44
  • CO2FromElectricity doesn't have any instance variable where you can add stuff, so you probably have to create one if you want to use an object CO2 somehow and add the values in Commented Jan 17, 2016 at 16:57

2 Answers 2

1

It's a typo. monthlyPrice is the variable name you declared but in your code you want to add on some variable called monthylyBill which doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is a simple typo:

monthylyPrice.add(0.24);
 monthylyPrice.add(0.35);
 monthylyPrice.add(0.27);

should be "monthlyPrice" not "monthylyPrice"

1 Comment

You also do it about 3 lines down. That's why you don't copy/paste your own code LOL. I've done the same thing: stackoverflow.com/questions/34536498/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.