Results 1 to 2 of 2
  1. #1
    huupderitis is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Using a method from another class on an object of another class.

    Java Code:
    import java.util.ArrayList;
    
    public class Purse
    {
      private int quarters;
      private int dimes;
      private int nickels;
      private int pennies;
      public static final double QUARTER_VALUE = 0.25;
      public static final double DIME_VALUE = 0.10;
      public static final double NICKEL_VALUE = 0.05;
      public static final double PENNY_VALUE = 0.01;
      
      private ArrayList<String> coins;
      
      /**
       Constructs a default purse with no change in it.
      */
       public Purse()
    	{
    	  coins = new ArrayList<String>();
    	}
    	
    	
    	public void addCoin(String coinName)
    	{
    	   coins.add(coinName);
    	}
    	
    	public void writeToString()
    	{
    	 System.out.println(" Purse contains " + coins);
    	}
    	
    	public ArrayList reverse()
    	 { 
    	   ArrayList<String> reversed = new ArrayList<String>();
    	   for(int i = coins.size() - 1; i >= 0; i--)
    	   {
    	   String newCoin = coins.get(i);
    		reversed.add(newCoin);
    	   }
    		return reversed;
    	 }
    	 
    	 public ArrayList toArrayList()
    	 {
    	   return coins;
    	 }
    	 
    	 
    	 public void transfer(Purse other)
    	 { 
    	   ArrayList another = other.toArrayList();
    		
    		for(int i = 0; i < another.size(); i++)
    		{ 
    		 String z = another.get(i);
    		 coins.add(z);
          }
    	 }
    }
    Here is my problem.... When trying to create the method public void transfer, I am using a seperate instance of purse as my parameter. Purse contains a new ArrayList<String> for each instance of purse. However it does not recognize the .get() or .size() methods for the coins array in my "other" purse.

    Is there an easier way to transfer the contents of one arraylist to another? Also how in the world do I use arraylist methods on this object!

    The object contains a new arraylist.... it just won't use the arraylist methods on it... i can only use them on coins.

    Any help?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Using a method from another class on an object of another class.

    Is there an easier way to transfer the contents of one arraylist to another?
    Look at using the new arraylist's constructor.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 02-04-2012, 11:11 AM
  2. Replies: 1
    Last Post: 11-29-2011, 01:30 PM
  3. Unable to access getter method in an object class I made
    By EnSlavingBlair in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 10:52 AM
  4. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  5. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-19-2008, 11:58 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
  •