Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Instantiating an array with object of another class

  1. #1
    AMCNovember is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Instantiating an array with object of another class

    I can't figure out why I'm not getting the String representation
    of "item" from my toString. I'm getting an incompatible type error
    Please point me in the right direction.
    Here's my code:
    Java Code:
     public class Shopping
    {
     private static Cart [] carts;
    public static void main ( String[] args)
    {    
        int numOfCarts = 2;
        carts = new Cart[ numOfCarts]; // create a Cart array of multiple carts
    
        // add logic to process each cart in the array here
    
        int numOfItems = 5;
        String typeSize = "Medium";
        String typeSeat = "Regular";
    
        carts[0] = new Cart(numOfItems, typeSize, typeSeat); // creates a cart object with values
    
        String itemName = "apple";    
        double itemPrice = .50;
        int itemQuantity = 5;
    
        Item item = new Item(itemName, itemPrice, itemQuantity); //instantiates item
    
        
        String s = "";
        s = item;
    
        carts[0].addItemToCustomerCart(item);
    
        System.out.println(carts);
    
    }// end method main
    @Override //return String representation of Item object
    public String toString()
    {
        String cartString = "";
        for ( Cart cart : carts )
        {
            cartString += cart + "/n";
        }
        return String.format( " %s", cartString );
        }
    
    }//end class Shopping
    
    
    import java.util.ArrayList;
    
    public class Cart
    {
        private String sizeType;
        private String seatType;
        private ArrayList <Item> items;
    
        //no argument constructor creates a cart with a default size (10)
        public Cart()
        {
    
        } // end no argument constructor
        public Cart( int n, String s, String t)
        {
            int initNbrOfItems = n > 0? n : 10; // validate
            
            items = new ArrayList< Item >( initNbrOfItems ); //create ArrayList
            setSizeType(s);
            setSeatType(t);
        } // end three parameter constructor
        public void addItemToCustomerCart( Item I)
        {
            items.add( I );
            for ( Item item : items )
            {
                System.out.println(item);
            }
        } //end method addItemToCustomerCart
        public void setSizeType(String Size)
        {
            sizeType = Size;
        } //end method setSizeType
        public void setSeatType(String Seat)
        {
            seatType = Seat;
        } // end method setSeatType
        public String getSizeType()
        {
            return sizeType;
        } //end method getSizeType
        public String getSeatType()
        {
            return seatType;
        } //end method getSeatType
    @Override //return String representation of each element in Item ArrayList
    public String toString()
    {
        String itemString = "";
        for ( Item item : items )
        {
            itemString += item + "/n";
        }
        return String.format( " %s", itemString );
    
    }//end method toString
    
    } //end class Cart
    
    
    public class Item
    {
        private String iName;
        private double iPrice;
        private int iQuantity;
    
        // no argument constructor
        public Item()
        {}// no argument constructor
        public Item(String n, double p, int q)
        {
            setIName(n);
            setIPrice(p);
            setIQuantity(q);
        }// end three parameter constructor
        public void setIName(String name)
        {
            iName = name;
        } //end method setIName
        public void setIPrice(double price)
        {
            iPrice = price;
        } // end method setIPrice
        public void setIQuantity(int quantity)
        {
            iQuantity = quantity;
        } // end method setIQuantity
        public String getIName()
        {
            return iName;
        } //end method getIName
        public double getIPrice()
        {
            return iPrice;
        } //end method getIPrice
        public int getIQuantity()
        {
            return iQuantity;
        } //end method getIQuantity
        // return String representation of the Item object
        @Override
        public String toString()
        {
            return String.format( "%s %d %d\n",
                    iName, iPrice, iQuantity );
        }// end method toString for the item object
    }//end class Item

  2. #2
    Vampiricx3 is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Instantiating an array with object of another class

    ArrayList<Carts> leArray = new ArrayList<Carts>();
    If I have helped you, + rep

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

    Default Re: Instantiating an array with object of another class

    Can you tell us more, specifically, what line is causing the error and what is the exact error message?

  4. #4
    AMCNovember is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instantiating an array with object of another class

    The compiler error is at line 24 and 25 above
    incompatible type
    Required: java.lang.String
    found: Item

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Instantiating an array with object of another class

    These are lines 24 and 25:
    Java Code:
        String s = "";
        s = item;
    Why do you think you can assign an Item reference to a String reference?

  6. #6
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Re: Instantiating an array with object of another class

    In the line 25
    string can not be referened to a object directly. If give reference then it will throw the exception like incompatible type.
    if you want Item object reference to a string then you need do Item.toString().
    Then exception will not occur.

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

    Default Re: Instantiating an array with object of another class

    Do you need to call the Item class's toString method?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Instantiating an array with object of another class

    Quote Originally Posted by dinesh.guntha View Post
    In the line 25
    string can not be referened to a object directly. If give reference then it will throw the exception like incompatible type.
    if you want Item object reference to a string then you need do Item.toString().
    Then exception will not occur.
    Which is only a solution if the OP actually wants the result of the toString referenced by 's'.
    Since 's' is not used in the above code they may intend something else entirely, and your 'fix' is simply a way to get things to compile.
    DarrylBurke likes this.

  9. #9
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default Re: Instantiating an array with object of another class

    It converts the item class object reference to string.
    what is requirement we dont know, but he is laking at that place. so that i tryed to explain that error & how to overcome from that error.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: Instantiating an array with object of another class

    Just fixing the cosmetic error may not fix the underlying problem, that's all I'm saying.
    I see it too often in bug fixes...

  11. #11
    AMCNovember is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Instantiating an array with object of another class

    Thanks for all the help. To get the program to compile I changed line 24 to read
    s += item; That forces Item item into a string.

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

    Default Re: Instantiating an array with object of another class

    The simple way would be to call the Item class's toString() method:
    String s = item.toString();

Similar Threads

  1. Replies: 6
    Last Post: 11-02-2011, 03:38 AM
  2. Replies: 0
    Last Post: 10-04-2011, 03:38 AM
  3. Replies: 2
    Last Post: 10-28-2010, 02:33 PM
  4. Can we generate an array for class object?
    By gaurav2211 in forum Advanced Java
    Replies: 4
    Last Post: 03-21-2009, 04:20 AM
  5. Replies: 2
    Last Post: 05-05-2008, 10:55 AM

Posting Permissions

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