Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By jlczuk

Thread: Change a program to a LoopTest, and change a LoopTest to an Array List???

  1. #1
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Change a program to a LoopTest, and change a LoopTest to an Array List???

    I have this program that I have written that I need to change it to a LoopTest.java and create an array of Animal and then loop through the array using an enhanced for(), calling the describe(), sound(), sleep(), and move() methods, successively.

    Then I need to change the LoopTest.java to an ArrayList instead of an array. I have no idea what they are talking about...can someone please help!!!

    This is what I need to rewrite to LoopTest.java

    Java Code:
    public class AbstractTest
    {
    	public static void main(String[] args)
    	{
    Cat cat = new Cat("Kitty", "Angora");
    Robin bird = new Robin("Rockin");
    
    System.out.println("For the cat:");
    System.out.print("This is: "+cat.describe()); 
    System.out.print("\nSound: "+cat.sound()); 
    System.out.print("\nSleeping: "+cat.sleep()); 
    System.out.print("\nMoving: "+cat.move()); 
    System.out.println("\n");
    
    System.out.println("For the robin:");
    System.out.print("This is: "+bird.describe()); 
    System.out.print("\nSound: "+bird.sound()); 
    System.out.print("\nSleeping: "+bird.sleep()); 
    System.out.print("\nMoving: "+bird.move()); 
    System.out.println("\n");
    
    System.out.println("End of program.");
    System.out.println("Press any key to continue...");
    	}
    }
    This is Animal and I have to create an array of Animal

    // Animal.java
    Java Code:
    public abstract class Animal
    {
    String type;
    
    	public Animal(String type)
    	{
    	this.type = type;
    	}
    	public abstract String describe();
    	public abstract String sound();
    	public abstract String sleep();
    	public abstract String move();
    }
    Last edited by Valerie1067; 04-19-2012 at 06:39 AM.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Have you created your Cat and Robin class? Those two classes will extend the Animal class, i.e. public class Cat extend Animal{...}.

    Also, you'll need to create an array of Animal, e.g. Animal anAnimal[] = new Animal[4]; then assign your animals to it's elements, i.e. cat, robin and etc.

    The enhanced for is described here The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics).

    Do you need to use both arrays and ArrayList?

  3. #3
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Hi Shall,

    yes I have created the Cat and Robin class, as well as Bird, Dog and Fish and they all extend Animal as you say. I am very new to Java, and not sure I understand what you mean by create an array of Animal and assign animals to its elements. like this Animals[0] = new Cat("Kitty", "Angora");??

    Thank you for send me the for Statement tutorial, I will definately check it out. And yes, the instructions say to rewrite it as LoopTest and create an array of Animal then loop it trhought the array and then change the LoopTest to use an ArrayList instead of an array. I don't understand that at all.

    Thanks for your help!!

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    No, its anAnimal[0] = new Cat("Kitty", "Angora");

    see this for converting from arrays to arraylist: How to create ArrayList (ArrayList<T>) from array (T[]) in Java - Stack Overflow

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Shall, If I read the OP's posts correctly, I thought they needed to rewrite the original code to use an ArrayList instead of an array. That's a different requirement from converting an array to an ArrayList within the existing code.

    Valerie, You may want to read the List Implementations Tutorial and How to use ArrayList in Java. This one may be helpful as well, but gets quite a bit more involved: ArrayList « Collections « Java Tutorial.
    DarrylBurke likes this.

  6. #6
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Thank you Shall for all your help! I appreciate it!

  7. #7
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Thank you jlczuk! I will check it out..I am so confused on this, and just kinda making it up as I go along...thanks for any and all help!!

  8. #8
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Hi guys, ok so here is what I have so far..not sure if this is EVEN close! Can you tell me what is wrong please?


    Java Code:
    public class LoopTest
    {
    	public static void main(String[] args)
    	{
    anAnimal[0] = new Cat("Kitty", "Angora");
    anAnimal[1] = new Robin("Rockin");
    
    for (i = 0; i <anAnimal.length; i++)
    {
    
    System.out.println("For the cat:");
    System.out.print("This is: "); Animal[i].describe(); 
    System.out.print("\nSound: "); Animal[i].sound(); 
    System.out.print("\nSleeping: ");Animal[i].sleep(); 
    System.out.print("\nMoving: ");Animal[i].move(); 
    System.out.println("\n");
    
    System.out.println("For the robin:");
    System.out.print("This is: ");Animal[i].describe();
    System.out.print("\nSound: ");Animal[i].sound(); 
    System.out.print("\nSleeping: ");Animal[i].sleep(); 
    System.out.print("\nMoving: ");Animal[i].move(); 
    System.out.println("\n");
    
    System.out.println("End of program.");
    System.out.println("Press any key to continue...");
    		}
    	}
    }

  9. #9
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    I only have time to reply to correct your indentation, which makes for very difficult reading:

    Java Code:
    public class LoopTest  {  // Personally, I prefer the opening brace on the same line as the declaration
    
        public static void main(String[] args)  {
            anAnimal[0] = new Cat("Kitty", "Angora");
            anAnimal[1] = new Robin("Rockin");
     
            for (i = 0; i <anAnimal.length; i++)  { 
     
                System.out.println("For the cat:");
                System.out.print("This is: "); Animal[i].describe();
                System.out.print("\nSound: "); Animal[i].sound();
                System.out.print("\nSleeping: ");Animal[i].sleep();
                System.out.print("\nMoving: ");Animal[i].move();
                System.out.println("\n");
     
                System.out.println("For the robin:");
                System.out.print("This is: ");Animal[i].describe();
                System.out.print("\nSound: ");Animal[i].sound();
                System.out.print("\nSleeping: ");Animal[i].sleep();
                System.out.print("\nMoving: ");Animal[i].move();
                System.out.println("\n");
     
                System.out.println("End of program.");
                System.out.println("Press any key to continue...");
            }
        }
    }

  10. #10
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Ok, quickly.

    You are initializing and array that you have not declared...you have to declare anAnimal, so you also need to know it's type.

    Look at your loop and at what you are trying to do inside of it. You are looping through to do something with each element of your anAnimal array (which you have not declared anywhere yet). Is each element going to be both a robin and a cat? Why would you expect

    Java Code:
    Animal[i].describe();
    to be both a cat and a robin? Which one is it really and how do you tell? Or do you even really need to tell them apart in your loop?

  11. #11
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Ok...thank you!!

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    To add to jlczuk's comments, you only need one the following group of statements in the loop:

    Java Code:
                System.out.print("This is: "); Animal[i].describe();
                System.out.print("\nSound: "); Animal[i].sound();
                System.out.print("\nSleeping: ");Animal[i].sleep();
                System.out.print("\nMoving: ");Animal[i].move();
                System.out.println("\n");

    The animal's describe() is already describing the animal, so remove the "For the cat:" and "For the robin:".

  13. #13
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Change a program to a LoopTest, and change a LoopTest to an Array List???

    Quote Originally Posted by shall View Post
    To add to jlczuk's comments, you only need one the following group of statements in the loop:

    Java Code:
                System.out.print("This is: "); Animal[i].describe();
                System.out.print("\nSound: "); Animal[i].sound();
                System.out.print("\nSleeping: ");Animal[i].sleep();
                System.out.print("\nMoving: ");Animal[i].move();
                System.out.println("\n");

    The animal's describe() is already describing the animal, so remove the "For the cat:" and "For the robin:".
    Just so Valerie doesn't think I'm totally incompetent, I could have quite easily just given her the answer. I was intentionally trying to make her think about what she's doing and what's wrong and why, otherwise she won't get as much out of the exercise.

Similar Threads

  1. Please help : drop down list value not change after submit
    By kalyana in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-01-2011, 01:27 PM
  2. I need a java program for change denominations
    By TheSweetHelloKitty in forum New To Java
    Replies: 6
    Last Post: 02-16-2011, 12:24 AM
  3. Need to change an ArrayList to an Array
    By trojansc82 in forum New To Java
    Replies: 9
    Last Post: 07-25-2010, 08:26 PM
  4. why dosent the 2d array change?
    By TheBreadCat in forum New To Java
    Replies: 11
    Last Post: 05-15-2010, 01:20 PM
  5. Change the color in my program
    By carl in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 12:20 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
  •