Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: how to create multiple objects

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question how to create multiple objects

    Hello,

    i need help on who to create multiple objects

    i provide a part of the code, what i need is to create, for example, 25 Cartons.

    How can i implement this faster (?) without writing 25 times :

    "
    Carton carton1 = new Carton(numBags);
    Carton carton2 = new Carton(numBags);
    Carton carton1 = new Carton(numBags);
    ....
    Carton carton25 = new Carton(numBags);
    "


    public class Programm{
        protected int numCartons; //number of Cartons. 25 cartons
        protected int numBags;     //number of Bags in Cartons. 2 bags per carton
     
        public Programm(int numCartons, int numBags){
            this.numCartons= numCartons;
            this.numBags = numBags;
        }
     
      public void createCartonsWithBags(int numBags){
           Carton carton1 = new Carton(numBags);
           Carton carton2 = new Carton(numBags);
           Carton carton3 = new Carton(numBags);
           //....
           Carton carton25 = new Carton(numBags);
      }
     
      public static void main(String[] argv ){
              int numCartons = 25;
    	  int numBags= 2;
     
    	  Myprogram m = new Myprogram(numCartons , numBags);
    	  m.createCartonsWithBags(numCartons);
      }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to create multiple objects

    use array's..
     Carton[] cartons = new Carton[25];

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to create multiple objects

    Quote Originally Posted by arieltal View Post
    use array's..
     Carton[] cartons = new Carton[25];
    This will only create an array variable filled with nulls. You will still need to use a for loop to fill the array with Carton objects.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Re: how to create multiple objects

    2 questions :

    1) I forgot to write that i want a vector. I mean that the number of Cartons objects should be determined dynamically.
    The question here is that i would like you to see the code and tell me if what i have done is correct with the vector.
    Is it ok or not ?


    2) I want to print the message, from method cartonPrint() in Carton class, in createCartonsWithBags() method.
    I need help here. Any idea ?

    I provide code below, what i have tried so far :

    import java.util.Vector;
     
    public class Programm{
        protected int numCartons;  //number of Cartons.
        protected int numBags;     //number of Bags in Cartons.
     
    	public Vector<Carton> carton; //is this correct ???
     
        public Programm(int numCartons, int numBags){
            this.numCartons= numCartons;
            this.numBags = numBags;
        }
     
      public void createCartonsWithBags(int numBags){
           carton = new Vector<Carton>(25); // is it correct ???
     
    	   /* i want to print the message from method cartonPrint() in Carton class.  */ 
    	   for(int i=0; i< carton.size(); i++){
    			carton[i].cartonPrint(); // i need Help here.
    	   }
      }
     
      public static void main(String[] argv ){
          int numCartons = 25;
    	  int numBags = 2;
     
    	  Programm m = new Programm(numCartons, numBags);
    	  m.createCartonsWithBags(numBags);
      }
    }

    import java.util.Vector;
     
    public class Carton{
      protected int numBags; 
     
      public Carton(int numBags){
          this.numBags = numBags;
      }  
     
      public void cartonPrint(){
    	System.out.println("Carton number "); 
      }
    }

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to create multiple objects

    Quote Originally Posted by dominco View Post
    2 questions :

    1) I forgot to write that i want a vector. I mean that the number of Cartons objects should be determined dynamically.
    The question here is that i would like you to see the code and tell me if what i have done is correct with the vector.
    Is it ok or not ?
    regarding
    public Vector<Carton> carton; //is this correct ???

    This looks good. Just a personal nitpic, but I'd name the variable cartons because it will hold references to more than one Carton object.

    regarding
      public void createCartonsWithBags(int numBags){
           carton = new Vector<Carton>(25); // is it correct ???

    This also looks good, although again I'd name the variable cartons. Note that you don't have to pass an int into the Vector constructor, but it does make it work more efficiently if you tell it on creation your best estimate of its likely size.

    regarding:
    	   /* i want to print the message from method cartonPrint() in Carton class.  */ 
    	   for(int i=0; i< carton.size(); i++){
    			carton[i].cartonPrint(); // i need Help here.
    	   }

    Here you've got several problems.

    First of all you're trying to treat a Vector as if it were an array. It isn't so you can't use array index notation to get items out of the Vector. Instead you must use methods that the Vector has such as its get(int i) method.

    But more importantly, you're trying to call a method on a Carton object before it's been created. Think of an array or ArrayList or Vector as being similar to an empty egg carton. You don't have any eggs in it until you first put them in there. So use a for loop and add Carton objects into your Vector using its add(...) method. Only then can you use Carton objects that the vector might hold.

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    dominco (November 5th, 2012)

  7. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Re: how to create multiple objects

    Hi again and first of all thanks for the replies curmudgeon.

    I still have problem with the cartons vector. I mean the adding method. I provide code below with comments and questions :

    import java.util.Vector;
     
    public class Programm{
        protected int numCartons;  //number of Cartons.
        protected int numBags;     //number of Bags in Cartons.
     
    	public Vector<Carton> cartons;
     
        public Programm(int numCartons, int numBags){
            this.numCartons= numCartons;
            this.numBags = numBags;
        }
     
      public void createCartonsWithBags(int numBags){
    	   Carton cart = new Carton();   //create 1 empty carton. correct ?
    	   int size = 2;
    	   cart.addBags(new Bag(size));  //add 1 bag to the carton. correct ?
     
     
    	   cartons = new Vector<Carton>(25); // create 25 empty cartons. ok?
     
    	   // how to add bags in the cartons ? i have problem here.
    	   for(int i=0; i< cartons.size(); i++){
    	       cartons(i).addBags(new Bag(size)); // how to call the addBags() method using Vector ? my target is to create 25 cartons with 1 bag each.
    	   }
      }
     
      public static void main(String[] argv ){
          int numCartons = 25;
    	  int numBags = 2;
     
    	  Programm m = new Programm(numCartons, numBags);
    	  m.createCartonsWithBags(numBags);
      }
    }

    import java.util.Vector;
     
    public class Carton{
      protected int numBags; 
     
      public Carton(){
      }
     
      public Carton(int numBags){
          this.numBags = numBags;
      }
     
      //addBag method. add 1 bag to the carton
      public void addBags(Bag newBag){
    	   System.out.println("Carton"); 
      }
     
    }

    public class Bag{
      protected int size;   // ball size
     
      public Bag(int size){
    	  this.size = size;
    	  System.out.println("Bag"); 
      }  
    }
    Last edited by jps; November 5th, 2012 at 08:00 PM. Reason: code=java

  8. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: how to create multiple objects

    Please Google for an ArrayList tutorial and check the API. You're still not adding any objects to the list, you're still guessing at how to work with Lists, and you're not using any of the methods that are available for the list. The API will tell you what methods are available for this class and you need to learn to use it.

  9. The Following User Says Thank You to curmudgeon For This Useful Post:

    dominco (November 6th, 2012)

  10. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: how to create multiple objects

    Quote Originally Posted by dominco View Post
    Is it ok or not ?
    The best way to find out is to compile and run your code. Why waste time waiting for us to reply?
    Improving the world one idiot at a time!

  11. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to create multiple objects

    OK, thanks i ll check the generics in java the List, ArrayList and if have any question i ll ask here, because i have no where else to ask.

Similar Threads

  1. Trying to create an array of stacks that contain objects
    By jadeclan in forum Collections and Generics
    Replies: 15
    Last Post: October 6th, 2012, 03:30 PM
  2. [SOLVED] Persisting multiple objects of the same type to a file.
    By mjr in forum Object Oriented Programming
    Replies: 6
    Last Post: August 15th, 2012, 07:25 PM
  3. Creating multiple objects with a for loop
    By JackCannon15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 20th, 2011, 07:26 PM
  4. Create Objects on Demand?
    By Programmierer in forum Object Oriented Programming
    Replies: 5
    Last Post: July 7th, 2010, 01:13 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM