Results 1 to 13 of 13
- 04-19-2012, 04:56 AM #1
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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
This is Animal and I have to create an array of AnimalJava 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..."); } }
// 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.
- 04-19-2012, 06:56 AM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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?
- 04-19-2012, 07:21 AM #3
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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!!
- 04-19-2012, 07:57 AM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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
- 04-19-2012, 03:03 PM #5
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-19-2012, 04:54 PM #6
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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!
- 04-19-2012, 04:55 PM #7
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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!!
- 04-20-2012, 05:51 AM #8
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
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..."); } } }
- 04-20-2012, 12:26 PM #9
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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..."); } } }
- 04-20-2012, 12:33 PM #10
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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
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?Java Code:Animal[i].describe();
- 04-20-2012, 12:43 PM #11
Member
- Join Date
- Mar 2012
- Location
- Palestine Texas
- Posts
- 18
- Rep Power
- 0
Re: Change a program to a LoopTest, and change a LoopTest to an Array List???
Ok...thank you!!
- 04-20-2012, 04:54 PM #12
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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:".
- 04-20-2012, 05:43 PM #13
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Change a program to a LoopTest, and change a LoopTest to an Array List???
Similar Threads
-
Please help : drop down list value not change after submit
By kalyana in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-01-2011, 01:27 PM -
I need a java program for change denominations
By TheSweetHelloKitty in forum New To JavaReplies: 6Last Post: 02-16-2011, 12:24 AM -
Need to change an ArrayList to an Array
By trojansc82 in forum New To JavaReplies: 9Last Post: 07-25-2010, 08:26 PM -
why dosent the 2d array change?
By TheBreadCat in forum New To JavaReplies: 11Last Post: 05-15-2010, 01:20 PM -
Change the color in my program
By carl in forum New To JavaReplies: 5Last Post: 04-03-2009, 12:20 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks