Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman

Thread: Adding Random Numbers to Collection

  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Adding Random Numbers to Collection

    I cannot figure out how to add random numbers to my collection using the add method. In the end I want to display a GUI application with 10 random numbers. I thought my code was pretty solid but it does not seem to store any values into the collection, but then again the whole collection thing confuses me.

    Java Code:
    import java.awt.Graphics;
    import java.util.Collections;
    import java.util.Random;
    
    
    public class Collection implements DataCollection
    {
    	private int size;
    	Item[] myCollection= new Item[10];
    	private Item element;
    	
    	public Collection()
    	{
    		size=10;
    	}
    	
    	public Collection(int someSize){
    		size= someSize;
    		
    	}
    		 
    	@Override
    	public void add(Item someItem)
    	{		
    		for (int i = 0; i < 10; i++) 
    		{
    			element.setValue( (int)(1 + 10*Math.random());
    			myCollection[i]= element;
    		
    			System.out.println( myCollection[i]);
    		}
    		
    	}
    
    	@Override
    	public void remove()
    	{
    
    	}
    
    	@Override
    	public void reset()
    	{
    		myCollection= null;
    	}
    
    	@Override
    	public void reset(Item someItem) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public boolean hasNext() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    
    	@Override
    	public Item next() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public void paint(Graphics pane) {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    Last edited by VettesRus; 09-17-2013 at 05:28 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Adding Random Numbers to Collection

    We can't run this code, as you haven't posted an SSCCE containing everything it requires.

    Also, it's a very bad idea to use names that Java already uses for class names, like Collection.
    quad64bit likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Adding Random Numbers to Collection

    Quote Originally Posted by KevinWorkman View Post
    We can't run this code, as you haven't posted an SSCCE containing everything it requires.

    Also, it's a very bad idea to use names that Java already uses for class names, like Collection.
    Sorry here is the rest, I havent even begun to figure out how to put the collection into my GUI app, right now I am just tyring to get it to sys.out.prnt my collection to see if its working, but I cannot get my main method to print it. Dont mind some of the comments, I am working off a previous program and have not fixed them yet. I will also change the Collection class name, I knew that seemed like a bad idea when I wrote it.

    Edit: Removed code to me post easier to read. New code at bottom.
    Last edited by VettesRus; 09-18-2013 at 03:48 AM.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Adding Random Numbers to Collection

    I highly recommend starting much smaller. If your current problem is generating random numbers and adding them to a collection, cut out all the extra junk and create a small SSCCE consisting of about a dozen lines dedicated to doing only that. Get that working, then move on from there.
    jim829 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,619
    Rep Power
    5

    Default Re: Adding Random Numbers to Collection

    I cannot figure out how to add random numbers to my collection using the add method. In the end I want to display a GUI application with 10 random numbers. I thought my code was pretty solid but it does not seem to store any values into the collection, but then again the whole collection thing confuses me.
    Well, for one thing, your collection does not lend itself to adding random numbers. The interface is specifically designed to add items of type Item.

    And although not that important at this time I find it incongruous to have a paint method in your DataCollection interface. IMO it would be better to have a separate interface (e.g PaintCollection) to handle the painting. And you might want to investigate using Generic types in implementing your interface as opposed to hard coding Item. Not required but worth looking at.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Adding Random Numbers to Collection

    I appreciate the helpful information. I have been literally working on this all day but I think its coming along. I feel in the DataCollection1 class my items are adding into the collection, but I cannot get them to display when I run the GUI app. There should be ten columns representing each value in the collection.

    Adding items to collection
    Java Code:
    	public void randomAction() {
    		myCollection.clear(); // We restart with nothing,
    							// then we add random items,
    		for (int i = 1; i <= COLLECTION_SIZE; i++) {
    			myCollection.add(new Item((int) (1 + MAXIMUM_ITEM_VALUE
    					* Math.random()), DEFAULT_COLOR));
    		}
    		selected = null; // We make sure nothing is selected
    	}
    Trying to paint the collection
    Java Code:
    public void paint(Graphics pane) {
    		if (myCollection != null) { // When instantiated,
    			Item e; // we display the items
    			int x = firstItemXCoord;
    			myCollection.reset();
    			while (myCollection.hasNext()) {
    				e = myCollection.next();
    					System.out.println("painting item " + e + " at x=" + x);
    				e.paint(pane);
    				x += Item.OVERALL_WIDTH;
    			}
    		}
    		if (randomButton != null)
    			randomButton.paint(pane); // and all of the buttons
    
    		if (maximumButton != null)
    			maximumButton.paint(pane);
    		if (minimumButton != null)
    			minimumButton.paint(pane);
    
    		if (removeButton != null)
    			removeButton.paint(pane);
    	}
    paint method from another class that paints the GUI app. Do I have to call the previous paint method to this one?
    Java Code:
    	public void paint(Graphics pane)
    	{
    		final int DELTA = 2;				//	To define the space between the
    											//		frame and the inside of a button
    
    		pane.setColor(Color.black);			//	Drawing the button frame
    		pane.drawRect(x, y, width, height);	//		in black,
    
    		pane.setColor(theColor);			//	and the inside of the button
    		pane.fill3DRect(x + DELTA, y + DELTA,	//	in the button color
    						width - (2*DELTA - 1),
    						height - (2*DELTA - 1),
    						up);
    
    		pane.setColor(Color.black);			//	Finally, we put the label in black
    											//		... and nicely centered
    		int labelWidth = pane.getFontMetrics().stringWidth(theLabel);
    		int labelHeight = pane.getFontMetrics().getAscent();
    		pane.drawString(theLabel,
    						x + (width - labelWidth)/2,
    						y + (height + labelHeight)/2);
    	}
    Last edited by VettesRus; 09-18-2013 at 04:01 AM.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Adding Random Numbers to Collection

    In the first reply by Kevin he asked you to supply us a SSCCE. He even gave you a link which you chose to ignore. Most people will not read elebenty gazillion lines of code and try and track down your problem for you. After all it is your job to fix the code not ours. We can help as long as you make it as easy for us as possible.

  8. #8
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Adding Random Numbers to Collection

    I understand, I read the link but when I tried to just put the code I was having trouble with I was told to upload the rest so it can be ran. Now that I see this there is probably a way to just upload the other files without spamming code huh? I just posted the SSCCE (I think lol), I apologize.
    Last edited by VettesRus; 09-18-2013 at 04:02 AM.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Adding Random Numbers to Collection

    You don't create an SSCCE by eliminating pieces of your existing program. You create an SSCCE by starting from scratch and only adding the bare minimum required to show the problem. You should try to do it all in one file, or even one main method if at all possible. For example, if your question only involves adding numbers to a collection, I'm not sure why you have any gui code or any reference to an Item class.

    At this point I'm not sure where you're stuck or what your current code looks like.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Adding random numbers questions
    By new12java in forum New To Java
    Replies: 1
    Last Post: 04-06-2012, 07:54 PM
  2. Two random numbers
    By kath09 in forum Java Applets
    Replies: 2
    Last Post: 11-13-2011, 06:11 PM
  3. Replies: 26
    Last Post: 10-30-2011, 06:01 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 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
  •