Results 1 to 5 of 5
Thread: Trouble with ArrayList
- 03-18-2009, 05:42 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
Trouble with ArrayList
I am pretty new to ArrayLists so I am having a little trouble. 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.Do uble>in CO2FromElectricitycannot be applied to()" I am wondering how to overcome this. If anybody has any ideas, I'd appreciate;)
Both classes that make up this program are shown below:
Java Code: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 }Java Code:/** * 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; } }
- 03-18-2009, 06:09 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello,
calcAverageBill takes in an ArrayList, you forgot to pass the method the ArrayList<double>..
:)Java Code:public double calcAverageBill(ArrayList<Double> monthlyBill)
- 03-18-2009, 06:51 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
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. :(
- 03-18-2009, 07:15 PM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Then you should modify it not to take in anything :)
- 03-18-2009, 07:43 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
Thanks. I thought about it for a-while and have come up with something I think will work. However, based on what I have, I get this output: [ CO2FromElectricityTester.main({ }) ]
Average Monthly Electricity Bill: 0.000000
...so I guess something is still hosed...
Java Code: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 }Java Code:/** * 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; } }
Similar Threads
-
hi there got alittle trouble
By justime8 in forum New To JavaReplies: 7Last Post: 12-24-2008, 12:22 AM -
Trouble with method
By BlueJ2008 in forum New To JavaReplies: 2Last Post: 10-19-2008, 09:05 PM -
get Trouble with SetDate
By hungleon88 in forum JDBCReplies: 0Last Post: 09-17-2008, 09:02 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks