#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Help on Random class use/totalling objects in array


    Hello,

    I've been trying to figure this out for days now and I'm not sure what I'm missing on here.

    2. Create 4 additional cats by generating a random number of legs for each of these cats. Generate a random number between 1 and 6, and this will be the number of legs the cat has. We will have no cats with 0 legs, or more legs than 6. See the class Random to help. This should be in a loop.

    ^This is what I am supposed to do, now I understand the creating 4 Cats part but I don't know how to incorporate the random number generating for them.

    Code:
    import java.util.ArrayList;
    import java.util.Random;
    import java.lang.Math;
    
    
    public class CatTester
    {
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
    		ArrayList<Cat> catList = new ArrayList<Cat>();
    		
    		/*Random randLegs = new Random();
    		for (int i = 0; i < 7; i++)
    		{
    			int rm = randLegs.nextInt(6) + 1;
    			System.out.println(randLegs);
    		}
    		*/
    		Cat ozzy = new Cat(Litter.SCOOPABLE);
    		ozzy.setName("Ozzy");
    		ozzy.setBreed("Manx");
    		ozzy.setColor("Black");
    		ozzy.setWeight(13.0);
    		ozzy.setNumOfLegs(4);
    	    
    		Cat minnie = new Cat(Litter.SCOOPABLE);	
    		minnie.setName("Minnie");
    		minnie.setBreed("Tabby");
    		minnie.setColor("Orange");
    		minnie.setWeight(11.5);
    		minnie.setNumOfLegs(4);
    		
    		Cat bub = new Cat(Litter.NONE);
    		bub.setName("Bub");
    		bub.setBreed("Munchkin");
    		bub.setColor("Grey");
    		bub.setWeight(10.0);
    		bub.setNumOfLegs(4);
    		
    		Cat finster = new Cat(Litter.CRYSTALS);
    		finster.setName("Finster");
    		finster.setBreed("Bengal");
    		finster.setColor("Black");
    		finster.setWeight(12.6);
    		finster.setNumOfLegs(2);
    
    		Cat tucker = new Cat(Litter.SCOOPABLE);
    		tucker.setName("Tucker");
    		tucker.setBreed("Maine Coon");
    		tucker.setColor("Light Brown");
    		tucker.setWeight(9.5);
    		tucker.setNumOfLegs(3);
    
    		Cat fuddles = new Cat(Litter.CRYSTALS);
    		fuddles.setName("Fuddles");
    		fuddles.setBreed("California Spangled");
    		fuddles.setColor("Grey");
    		fuddles.setWeight(12.3);
    		fuddles.setNumOfLegs(5);
    
    		Cat snickerdoodles = new Cat(Litter.CRYSTALS);
    		snickerdoodles.setName("Snickerdoodles");
    		snickerdoodles.setBreed("York Chocolate");
    		snickerdoodles.setColor("Dark Brown");
    		snickerdoodles.setWeight(11.0);
    		snickerdoodles.setNumOfLegs(4);
    
    		Cat butters = new Cat(Litter.NONE);
    		butters.setName("Butters");
    		butters.setBreed("Turkish Angora");
    		butters.setColor("White");
    		butters.setWeight(13.5);
    		butters.setNumOfLegs(3);
    
    		Cat sneaker = new Cat(Litter.SCOOPABLE);
    		sneaker.setName("Sneaker");
    		sneaker.setBreed("Ragdoll");
    		sneaker.setColor("Brown");
    		sneaker.setWeight(15.5);
    		sneaker.setNumOfLegs(4);
    
    		Cat misty = new Cat(Litter.NONE);
    		misty.setName("Misty");
    		misty.setBreed("Scottish Fold");
    		misty.setColor("Tri-Colored");
    		misty.setWeight(20.5);
    		misty.setNumOfLegs(4);
    		
    		/*Cat Figs = new Cat(Litter.CRYSTALS);
    		misty.setName("Figs");
    		misty.setBreed("Calico");
    		misty.setColor("Tri-Colored");
    		misty.setWeight(15.2);
    		//misty.numOfLegs();
    		
    		Cat PicoDeGato = new Cat(Litter.NONE);
    		misty.setName("Pico de Gato");
    		misty.setBreed("Burmese");
    		misty.setColor("Brown");
    		misty.setWeight(20.5);
    		//misty.setNumOfLegs(4);
    		
    		Cat Schnickelfritz = new Cat(Litter.NONE);
    		misty.setName("Schnickelfritz");
    		misty.setBreed("Norwegian Forest Cat");
    		misty.setColor("White");
    		misty.setWeight(20.5);
    		//misty.setNumOfLegs(4);
    		
    		Cat CaptainSkittlehook = new Cat(Litter.NONE);
    		misty.setName("Captain Skittlehook");
    		misty.setBreed("Siberian");
    		misty.setColor("Tri-Colored");
    		misty.setWeight(20.5);
    		//misty.setNumOfLegs(4);*/
    
            catList.add(ozzy);
            catList.add(minnie);
            catList.add(bub);
            catList.add(finster);
            catList.add(tucker);
            catList.add(fuddles);
            catList.add(snickerdoodles);
            catList.add(butters);
            catList.add(sneaker);
            catList.add(misty);
           
            
            for (Cat cats : catList)
     	    {
     		   System.out.println(cats);
     	    }
     	   
            
     	   int[] addedCat = new int[4];
     	   {
     		   addedCat[0] = (int) ((6 * Math.random()) + 1);
     		   addedCat[1] = (int) ((6 * Math.random()) + 1);
     		   addedCat[2] = (int) ((6 * Math.random()) + 1);
     		   addedCat[3] = (int) ((6 * Math.random()) + 1);
     	   }
     	   System.out.print("Random legged cats :");
     	   for (int i = 0; i <= 6; i++)
     	    {
     	     System.out.print(addedCat[i] + " " );
     	    }
     	
     	   
     	   System.out.println("Total number of cat's legs: " + totalLegs());
    
    	}
    
    }

    I have the random numbers generated but I don't really understand how to put them into the additional cats array.

    ------
    Also having issues with
    System.out.println("Total number of cat's legs: " + totalLegs());

    Error is: The method totalLegs() is undefined for the type cat tester.

    I have this in the main program the Cat.java

    Code:
    public int numOfLegs(ArrayList<Cat> catList)
    	{
    		int totalLegs = 0;
    		for (int i = 0; i < catList.size(); i++)
    		{
    			totalLegs += catList.get(i).getNumOfLegs();
    			//totalLegs = catList.get(i).getNumOfLegs() + totalLegs;
    		}
    	    return totalLegs;
    	
    	}

    Thank you
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,020
    Rep Power
    1285
    When you generate the random number of legs, that information becomes part of a Cat.

    You add the Cat to the list.

    The error means there is no totalLegs() method.

    The variable totalLegs is local to the method numOfLegs.

IMN logo majestic logo threadwatch logo seochat tools logo