Results 1 to 2 of 2
  1. #1
    2rr2nv is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default JAVA is kickin my butt - Arraylist issue

    Hi ya all.
    I've been attending programming courses for a few months, but these Arraylists are kicking my butt... i have numerous problems and i don't know where to start...

    1st, i have a driver, a superclass, and 2 subclasses. (i totally hate this type of programming. gimme one program to do it all and i'd be happy.)

    2nd, i have to use an Arraylist to track some mice (to add more mice, to get rid of mice, etc (i hate mice - LOL)

    3rd i don't understand how it the Arraylist are supposed to work between 3 of the programs (driver, super and subclass)

    please bear with me, i have a lot of stuff commented as i work issues....here is my subclass:


    Java Code:
    /******************************************
    *	 Cat.java
    *	 scot zarnoth
    *
    * this class program called Cat, takes care of eating mice and growing the cat
    ******************************************/
    
    public class Cat extends Mammal
    {
    	private int age;
    
        //*************************************************************************
    
    	public Cat()
    	{}
    
    	public Cat(int age, double weight)
    	{
    		super(weight);
    		this.age = age;
    	}
    
    	//*************************************************************************
    
    	public void grow()
    	{
    		this.age = age + 1;
    	}
    
    	//**************************************************************************
    
    	//**************************************************************************
    
    	public int getAge()
    	{
    		return this.age;
    	}
    
    	//**************************************************************************
    
    
    	public void display()
    	{
    		System.out.println(getAge());
    	}
    
    	//**************************************************************************
    	public void eat()
    	{
    
    		double hungryCat = (Math.random());
    		if (hungryCat <= .7)
    		{
    			mice.remove((int) (Math.random() * mice.size()));
    		}
    
    	}
    }// end Cat

    HERE IS MY Driver...I KNOW I'M MISSING THINGS, but i don't know how to fix it....or properly

    Java Code:
    /********************************************************
    *  ZarnothScotWeek5CatMouse.java
    *  Scot Zarnoth
    *
    *  this is a driver program that uses 2 classes and 1 superclass to produce results of a fight between a
    *		non-producing cat and reproducing mice and runs the fight 10 times
    **********************************************************************/
    
    import java.util.ArrayList;
    
    public class ZarnothScotWeek5CatMouse
    {
    	public static void main(String [] args)
    	{
    		Cat sylvester = new Cat();
            ArrayList<Mouse> mice = new ArrayList<Mouse>();
            mice.add(new Mouse());
            mice.add(new Mouse());
            mice.add(new Mouse());
    //        mice.get(0).setSex(true);
    //        mice.get(1).setSex(false);
    //        mice.get(2).setSex(false);
            for (int i=1; i < 11; i++)
    		{
    			mice.add(new Mouse());
    			while (mice.size() >1 && mice.size() < 10)
                {
           			for (int n=1; n < 11; n++)
    				{
    					for (Mouse m:mice)
    	              	     m.grow();
                  		sylvester.grow();
    					mice.add(new Mouse());
    					//Mouse.mate(mice);
    					//sylvester.eat(mice);
    				    System.out.println("Mice RULE, Cats drool Mice population: " + mice.size());
    				    System.out.print("Cats RULE, Mice drool Cat Weight (in mice): ");
    				}
    			}
    		}
            System.out.println("Mice RULE, Cats drool Mice population: " + mice.size());
    
            System.out.println("Cats RULE, Mice drool Cat Weight (in mice): ");
    
         }
    
    
    }//end ZarnothScotWeek5CatMouse

    i'm trying to figure out how to get the "eat" method to work. Please forgive me. i thought i was "getting" it, and did real good on the last program, but this one is the pits. here's the error i get when i compile..

    Java Code:
    .\Cat.java:54: error: cannot find symbol
    			mice.remove((int) (Math.random() * mice.size()));
    			                                   ^
      symbol:   variable mice
      location: class Cat
    .\Cat.java:54: error: cannot find symbol
    			mice.remove((int) (Math.random() * mice.size()));
    			^
      symbol:   variable mice
      location: class Cat
    2 errors
    
    Tool completed with exit code 1

    i realize the mice is a variable, but how did i get it to work here when it's started in the Driver program.....
    Last edited by Fubarable; 11-20-2011 at 05:09 PM. Reason: code tags added

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

    Default Re: JAVA is kickin my butt - Arraylist issue

    Quote Originally Posted by 2rr2nv View Post
    Hi ya all.
    I've been attending programming courses for a few months, but these Arraylists are kicking my butt... i have numerous problems and i don't know where to start...

    1st, i have a driver, a superclass, and 2 subclasses. (i totally hate this type of programming. gimme one program to do it all and i'd be happy.)

    2nd, i have to use an Arraylist to track some mice (to add more mice, to get rid of mice, etc (i hate mice - LOL)

    3rd i don't understand how it the Arraylist are supposed to work between 3 of the programs (driver, super and subclass)

    please bear with me, i have a lot of stuff commented as i work issues....here is my subclass:


    Java Code:
    /******************************************
    *	 Cat.java
    *	 scot zarnoth
    *
    * this class program called Cat, takes care of eating mice and growing the cat
    ******************************************/
    
    public class Cat extends Mammal
    {
    	private int age;
    
        //*************************************************************************
    
    	public Cat()
    	{}
    
    	public Cat(int age, double weight)
    	{
    		super(weight);
    		this.age = age;
    	}
    
    	//*************************************************************************
    
    	public void grow()
    	{
    		this.age = age + 1;
    	}
    
    	//**************************************************************************
    
    	//**************************************************************************
    
    	public int getAge()
    	{
    		return this.age;
    	}
    
    	//**************************************************************************
    
    
    	public void display()
    	{
    		System.out.println(getAge());
    	}
    
    	//**************************************************************************
    	public void eat()
    	{
    
    		double hungryCat = (Math.random());
    		if (hungryCat <= .7)
    		{
    			mice.remove((int) (Math.random() * mice.size()));
    		}
    
    	}
    }// end Cat

    HERE IS MY Driver...I KNOW I'M MISSING THINGS, but i don't know how to fix it....or properly

    Java Code:
    /********************************************************
    *  ZarnothScotWeek5CatMouse.java
    *  Scot Zarnoth
    *
    *  this is a driver program that uses 2 classes and 1 superclass to produce results of a fight between a
    *		non-producing cat and reproducing mice and runs the fight 10 times
    **********************************************************************/
    
    import java.util.ArrayList;
    
    public class ZarnothScotWeek5CatMouse
    {
    	public static void main(String [] args)
    	{
    		Cat sylvester = new Cat();
            ArrayList<Mouse> mice = new ArrayList<Mouse>();
            mice.add(new Mouse());
            mice.add(new Mouse());
            mice.add(new Mouse());
    //        mice.get(0).setSex(true);
    //        mice.get(1).setSex(false);
    //        mice.get(2).setSex(false);
            for (int i=1; i < 11; i++)
    		{
    			mice.add(new Mouse());
    			while (mice.size() >1 && mice.size() < 10)
                {
           			for (int n=1; n < 11; n++)
    				{
    					for (Mouse m:mice)
    	              	     m.grow();
                  		sylvester.grow();
    					mice.add(new Mouse());
    					//Mouse.mate(mice);
    					//sylvester.eat(mice);
    				    System.out.println("Mice RULE, Cats drool Mice population: " + mice.size());
    				    System.out.print("Cats RULE, Mice drool Cat Weight (in mice): ");
    				}
    			}
    		}
            System.out.println("Mice RULE, Cats drool Mice population: " + mice.size());
    
            System.out.println("Cats RULE, Mice drool Cat Weight (in mice): ");
    
         }
    
    
    }//end ZarnothScotWeek5CatMouse

    i'm trying to figure out how to get the "eat" method to work. Please forgive me. i thought i was "getting" it, and did real good on the last program, but this one is the pits. here's the error i get when i compile..

    Java Code:
    .\Cat.java:54: error: cannot find symbol
    			mice.remove((int) (Math.random() * mice.size()));
    			                                   ^
      symbol:   variable mice
      location: class Cat
    .\Cat.java:54: error: cannot find symbol
    			mice.remove((int) (Math.random() * mice.size()));
    			^
      symbol:   variable mice
      location: class Cat
    2 errors
    
    Tool completed with exit code 1

    i realize the mice is a variable, but how did i get it to work here when it's started in the Driver program.....
    I've added code tags to your post above.

    It looks as if part of your problem is a "reference" issue. You're having Cat call methods on a variable called "mice" but the compiler is telling you that the mice variable does not exist in cat, and the compiler is in fact correct (as always).

    We may need to see more, including your full instructions.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. ArrayList issue
    By drogba123 in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 11:02 AM
  3. Replies: 19
    Last Post: 08-10-2010, 09:36 AM
  4. ArrayList issue
    By tomrobpowell in forum Eclipse
    Replies: 2
    Last Post: 05-08-2010, 08:04 AM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 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
  •