Results 1 to 11 of 11
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default Calling a method when using an arraylist?

    I can't quite figure this out. I have 2 files, one of them is the tester and one of them isn't. The non-tester (the one with all of the methods), works fine. The tester is the one where I am trying to call the methods and cannot seem to do so. The part that is giving me trouble is bolded. I think I just wrote the code wrong but please let me know. Thank you!



    This is the one that works fine!
    Java Code:
    /**
     * This program calculates the reduction of CO2 from recycling using ArrayLists.
     *
     *
     * Jamison Hyman 
     * 1/20/10
     */
     
    import java.util.ArrayList;
    public class CO2FromWaste {
    
    	private double totalEmission;
    	private double reduction;
    	private double netEmission;
    	private double waste = 1018.0;
    	private int myNumPeople;
    	private boolean myPaper;
    	private boolean myPlastic;
    	private boolean myGlass;
    	private boolean myCans;
    
    	CO2FromWaste(int numPeople, boolean paper, boolean plastic, boolean glass, boolean cans)
    	{
    		myNumPeople = numPeople;
    		myPaper = paper;
    		myPlastic = plastic;
    		myGlass = glass;
    		myCans = cans;	
    	}
    	
    	 public void calcGrossWasteEmission()
    	 {
    	 	totalEmission += myNumPeople * 1018.0;
    	 	System.out.println("Total Emission: " + totalEmission);
    	 }
    	 
    	 
    	public void calcWasteReduction()
    	{
    		
    	 	if (myPaper == true)
    	 	{
    	 		reduction += 184 * myNumPeople; 
    	 		System.out.println("Reduction: " + reduction);
    	 	}
    	 	if (myPlastic == true)
    	 	{
    	 		reduction += 25.6 * myNumPeople;
    	 		System.out.println("Reduction: " + reduction);
    	 	}
    	 	if (myGlass == true)
    	 	{
    	 		reduction += 46.6 * myNumPeople;
    	 		System.out.println("Reduction: " + reduction);
    	 	}
    	 	if(myCans == true)
    	 	{
    	 		reduction += 165.8 * myNumPeople;
    	 		System.out.println("Reduction: " + reduction);
    	 	}		
    			
    	}
    
    
    	public void calcNetWasteReduction()
    		{	
    			
    		netEmission = totalEmission - reduction;
    		System.out.println(netEmission);
    		}
    		
    }


    Here is what is giving me the issue
    [code]
    /**
    * This program calculates the reduction of CO2 from recycling using ArrayLists.
    *
    *
    * Jamison Hyman
    * 1/20/10
    */

    import java.util.ArrayList;
    public class CO2FromWasteTester {

    public static void main(String[] args) {

    ArrayList<CO2FromWaste> CO2 = new ArrayList<CO2FromWaste>();
    CO2.add(new CO2FromWaste(1, true, true, true, true));
    CO2.add(new CO2FromWaste(2, true, false, true, true));
    CO2.add(new CO2FromWaste(3, true, true, false, true));
    CO2.add(new CO2FromWaste(4, false, true, true, true));
    CO2.add(new CO2FromWaste(5, true, true, true, false));
    CO2.add(new CO2FromWaste(6, false, false, true, true));

    int index = 0;

    for (int i =0; i < CO2.size(); i++)
    {
    CO2[index].calcGrossWasteEmission();
    CO2[index].calcWasteReduction();
    CO2[index].calcNetWasteReduction();

    index++;
    }


    /*
    for(int i = 0; i < 6; i++)
    {
    System.out.print(index + " " + myNumPeople.CO2 + " " + myPaper.CO2 + " " + myPlastic.CO2FromWaste + " " +
    myGlass.CO2 + " " + myCans.CO2FromWaste + " " + totalEmission.CO2FromWaste + " " + reduction.CO2FromWaste
    + " " + netEmission.CO2FromWaste);
    }
    */

    }
    }

  2. #2
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Well, this won't solve your problem, but it will save time/code in the future.

    for (int i =0; i < CO2.size(); i++)
    {
    CO2[index].calcGrossWasteEmission();
    CO2[index].calcWasteReduction();
    CO2[index].calcNetWasteReduction();
    index++;
    }

    instead of using index, just use 'i'. Thats what its there for.

  3. #3
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Also, could you tell us the error/output that you are getting that is wrong??

  4. #4
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    The error is:

    array required, but java.util.ArrayList<CO2FromWaste> found
    CO2[index].calcGrossWasteEmission();



    It's the same for all of them.

  5. #5
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Oh, I should have noticed that.

    You are using an ArrayList, but saying CO2[0] is trying to use an Array. There are key differences.

    The concept of what you are trying to do is correct, but the syntax is off. For array lists you need to call a method to retrieve the object at the index.

    So you should be doing
    CO2.get(index).calcGrossWasteEmission();

  6. #6
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    THANK YOU SO MUCH! :)

    That's exactly what I was looking for!

  7. #7
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    You have no idea how many times I did the same thing while learning.

  8. #8
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    One last question:

    Java Code:
      ArrayList<CO2FromWaste> CO2 = new ArrayList<CO2FromWaste>();
            CO2.add(new CO2FromWaste(1, [B]true[/B], true, true, true));
            CO2.add(new CO2FromWaste(2, [B]true[/B], false, true, true));
            CO2.add(new CO2FromWaste(3, [B]true[/B], true, false, true));
            CO2.add(new CO2FromWaste(4, [B]false[/B], true, true, true));
            CO2.add(new CO2FromWaste(5, [B]true[/B], true, true, false));
            CO2.add(new CO2FromWaste(6, [B]false[/B], false, true, true));
    If that's my arraylist, how would I print pieces of it? For instance if I only wanted to print the bold values in a for loop.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Give the CO2 from waste class an isPaper method, and then loop through the arraylist calling this method on each item in the list.

  10. #10
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    So the bold value you have is the 'paper' boolean. Your CO2FromWaste class has this value stored in it, as you know. But, you can not access it by calling CO2FromWasteObject.myPaper because it is private (which is probably a good thing). So, this is where we use Getters and Setters. Your constructor is by default Setting the value of "myPaper" to the boolean value that you pass in.

    new CO2FromWaste(1, true, true, true, true) is setting it to true for example. You can also make a simple method called a setter to do this, so you can modify the value of "myPaper" for that object. It is a simple method that would be like:
    Java Code:
    public void setMyPaper(boolean bool){
    this.myPaper = bool;
    }
    Thats it. Now in order to retrieve this value you just create a 'Getter'.
    Java Code:
    public boolean getMyPaper(){
    return myPaper;
    }
    Notice that the return value of the 'getter' has to be specified as the type of what myPaper is, which is a boolean in this case. From there, you can probably figure it out (hint iterate/loop will help).

    Edit: isPaper makes more sense than getMyPaper, since its a boolean.
    Last edited by senorbum; 01-23-2010 at 07:55 PM.

  11. #11
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    no wonder scientists say we have global warming

    :)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. help w/ method calling
    By blueduiker in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 07:55 AM
  2. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  3. Need help calling from a different method
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:27 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Help with Calling a method
    By Albert in forum New To Java
    Replies: 3
    Last Post: 07-10-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •