In the writing of this program, I have happened upon a problem in the main method of the Tester class. In the Tester class, I am trying to print out the average monthly electric bill value. However, when I try to run it, I get an error stating: "calcAveragePrice(java.util.ArrayList<java.lang.Double>in CO2FromElectricitycannot be applied to()" I am wondering how to overcome this.

Both classes that make up this program are shown below:

/**
 * This program calculates my CO2 footprint based on the amount of electricity used in my home each year.
 * 
 * @author John D. Barry 
 * @version 03/18/2009
 */
import java.util.ArrayList; 
class CO2FromElectricity
{
    //declaration of private instance variables
    
    /**
     * Default constructor to create an object from the CO2FromElectricity class.
     */
    
    CO2FromElectricity()
    {
    }
    
    /**
     * A mutator method which calculates the average annual electricity bill.
     * @param monthlyBill an ArrayList containing the monthly bills for home electricity use.
     * @return the average monthly electricity bill.
     */
    public double calcAverageBill(ArrayList<Double> monthlyBill)
    {

        monthlyBill.add(279.41);
        monthlyBill.add(238.03);
        monthlyBill.add(248.64);
        monthlyBill.add(258.73);
        monthlyBill.add(395.48);
        monthlyBill.add(419.91);
        monthlyBill.add(431.15);
        monthlyBill.add(407.56);
        monthlyBill.add(417.14);
        monthlyBill.add(308.35);
        monthlyBill.add(337.91);
        monthlyBill.add(320.77);
        double sum = 0.0;
        double monthlyTotal = 0.0;
        
        for(int i=0; i < monthlyBill.size(); i++) 
        {
			sum += monthlyBill.get(i);
          }

          return monthlyTotal/monthlyBill.size(); 
}
        
     /**
     * A mutator method which calculates the average annual price of electricity.
     * @param monthlyPrice an ArrayList containing the monthly price of electricity per kilowatthour.
     * @return the average monthly price of electricity.
     */
    public double calcAveragePrice(ArrayList<Double> monthlyPrice)
    {
        monthlyPrice.add(0.1117);
        monthlyPrice.add(0.1107);
        monthlyPrice.add(0.1110);
        monthlyPrice.add(0.1113);
        monthlyPrice.add(0.1135);
        monthlyPrice.add(0.1138);
        monthlyPrice.add(0.1217);
        monthlyPrice.add(0.1215);
        monthlyPrice.add(0.1216);
        monthlyPrice.add(0.1228);
        monthlyPrice.add(0.1209);
        monthlyPrice.add(0.1192);
        double sum = 0.0;
        double monthlyTotal = 0.0;

       for(int i=0; i < monthlyPrice.size(); i++) 
        {
			sum += monthlyPrice.get(i);
          }

          return monthlyTotal/monthlyPrice.size();
    }
        
     /**
     * A mutator method which calculates the annual home CO2 emission from electricity.
     * @param avgBill the average monthly home electricity bill.
     * @param avgPrice the average montlyl price of home electricity.
     * @return the annual home CO2 emission from home electricity use.
     */
    public double calcElectricityCO2(double avgBill,
                                    double avgPrice)
    {
        return (avgBill/avgPrice) * 1.37 * 12;
    } 
    
 
}
mport java.util.ArrayList;
public class CO2FromElectricityTester

{

//main method
    public static void main(String[ ] args)
    {
        //initialization of variables
        double total = 0.0;
        
        //create object
        CO2FromElectricity CO2 = new CO2FromElectricity();
        
        
                             
        System.out.printf("Average Monthly Electricity Bill: %11f",CO2.calcAveragePrice());
                                                    
        }//end of main method
    
}

Recommended Answers

All 6 Replies

You created the method

calcAverageprice(ArrayList monthylPrice)

Then you call the method in your tester class without passing the ArrayList parameter

calcAveragePrive()

Also, as a side note, in your tester class you forgot the 'i' in import java.util.ArrayList;

Your cacAveragePrice method needs an ArrayList as a parameter, and when you call it you don't supply a parameter

Yes, thank you. I thought I should pass a parameter when I call the method back up in the main method; however, I don't know parameter to pass to it because I have already done the entire calculation in the method. :(

Well, you should have added all those values in the tester method.

import java.util.ArrayList;
public class CO2FromElectricityTester

{

//main method
    public static void main(String[ ] args)
    {
        //initialization of variables
        double total = 0.0;
        
        //create object
        CO2FromElectricity CO2 = new CO2FromElectricity();
        ArrayList<Double> monthlyPrice= new ArrayList<Double>();
        monthlyPrice.add(0.1117);
        monthlyPrice.add(0.1107);
        monthlyPrice.add(0.1110);
        monthlyPrice.add(0.1113);
        monthlyPrice.add(0.1135);
        monthlyPrice.add(0.1138);
        monthlyPrice.add(0.1217);
        monthlyPrice.add(0.1215);
        monthlyPrice.add(0.1216);
        monthlyPrice.add(0.1228);
        monthlyPrice.add(0.1209);
        monthlyPrice.add(0.1192);

        
        
                             
        System.out.printf("Average Monthly Electricity Bill: %11f",CO2.calcAveragePrice(monthlyPrice));
                                                    
        }//end of main method
    
}

That is what the tester class should look like. The tester class should provide the values to the class you are testing. This way you can reuse the CO2 class. The same goes for the calcAverageBill() method. The point of passing a parameter to a method is so it can use the value(s) the parameter holds.

Your calcAveragePrice() method should look like this

public double calcAveragePrice(ArrayList<Double> monthlyPrice)
    {
        double sum = 0.0;
        double monthlyTotal = 0.0;

       for(int i=0; i < monthlyPrice.size(); i++) 
       {
	      sum += monthlyPrice.get(i);
       }

          return monthlyTotal/monthlyPrice.size();
    }

Hi jasmip, I was just having that idea myself. However, something must still be wrong because based on my code below, I get output that is like this: [ CO2FromElectricityTester.main({ }) ]
Average Monthly Electricity Bill: 0.000000

import java.util.ArrayList;
public class CO2FromElectricityTester

{

//main method
    public static void main(String[ ] args)
    {
        //initialization of variables

        
        
        //create object
        CO2FromElectricity CO2 = new CO2FromElectricity();
        
        ArrayList<Double> monthlyBill = new ArrayList<Double>();
        
        monthlyBill.add(279.41);
        monthlyBill.add(238.03);
        monthlyBill.add(248.64);
        monthlyBill.add(258.73);
        monthlyBill.add(395.48);
        monthlyBill.add(419.91);
        monthlyBill.add(431.15);
        monthlyBill.add(407.56);
        monthlyBill.add(417.14);
        monthlyBill.add(308.35);
        monthlyBill.add(337.91);
        monthlyBill.add(320.77);
        
        
        
        
                             
        System.out.printf("Average Monthly Electricity Bill: %11f",CO2.calcAverageBill(monthlyBill));
                                                    
        }//end of main method
    
}
/**
 * This program calculates my CO2 footprint based on the amount of electricity used in my home each year.
 * 
 * @author John D. Barry 
 * @version 03/18/2009
 */
import java.util.ArrayList; 
class CO2FromElectricity
{
    //declaration of private instance variables
    
    /**
     * Default constructor to create an object from the CO2FromElectricity class.
     */
    
    CO2FromElectricity()
    {
    }
    
    /**
     * A mutator method which calculates the average annual electricity bill.
     * @param monthlyBill an ArrayList containing the monthly bills for home electricity use.
     * @return the average monthly electricity bill.
     */
    public double calcAverageBill(ArrayList<Double> monthlyBill)
    {

        
        double sum = 0.0;
        double monthlyTotal = 0.0;
        
        for(int i=0; i < monthlyBill.size(); i++) 
        {
			sum += monthlyBill.get(i);
	
          }

        return monthlyTotal/monthlyBill.size(); 
}
        
     /**
     * A mutator method which calculates the average annual price of electricity.
     * @param monthlyPrice an ArrayList containing the monthly price of electricity per kilowatthour.
     * @return the average monthly price of electricity.
     */
    public double calcAveragePrice(ArrayList<Double> monthlyPrice)
    {
        monthlyPrice.add(0.1117);
        monthlyPrice.add(0.1107);
        monthlyPrice.add(0.1110);
        monthlyPrice.add(0.1113);
        monthlyPrice.add(0.1135);
        monthlyPrice.add(0.1138);
        monthlyPrice.add(0.1217);
        monthlyPrice.add(0.1215);
        monthlyPrice.add(0.1216);
        monthlyPrice.add(0.1228);
        monthlyPrice.add(0.1209);
        monthlyPrice.add(0.1192);
        double sum = 0.0;
        double monthlyTotal = 0.0;

       for(int i=0; i < monthlyPrice.size(); i++) 
        {
			sum += monthlyPrice.get(i);
          }

          return monthlyTotal/monthlyPrice.size();
    }
        
     /**
     * A mutator method which calculates the annual home CO2 emission from electricity.
     * @param avgBill the average monthly home electricity bill.
     * @param avgPrice the average montlyl price of home electricity.
     * @return the annual home CO2 emission from home electricity use.
     */
    public double calcElectricityCO2(double avgBill,
                                    double avgPrice)
    {
        return (avgBill/avgPrice) * 1.37 * 12;
    } 
    
 
}

In your calcAverageBill() method you return monthlyTotal/monthlyBill.size(); When you initialize monthlyTotal you set it equal to zero, and then never change the value.
Did you mean to set monthlyTotal to sum after the for loop ran?

public double calcAverageBill(ArrayList<Double> monthlyBill)
    {

        
        double sum = 0.0;
        double monthlyTotal = 0.0;
        
        for(int i=0; i < monthlyBill.size(); i++) 
        {
			sum += monthlyBill.get(i);
	
          }
        //monthlyTotal still equals Zero here
        return monthlyTotal/monthlyBill.size(); 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.