7 Replies - 943 Views - Last Post: 18 September 2013 - 04:21 AM Rate Topic: -----

#1 Anitush   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 12:55 AM

Hey there:

I need help with the following:

1. Create an array of Animal and then loop through the array using an enhanced for(), calling the desccribe(), sound(),
sleep(), and move() methods, successively.

2. Change the test to use an ArrayList instead of an array. Indicate your for() loop had to be modified when changing from
an array to an Arraylist.

First example is the Arraylist (point 1 above) It compiled and works perfect. Not sure if this is what they asked though.

Second example is the array(point 1 above) it has a compiling error and again I am not sure if I am doing this correctly.

Can someone please see if 1) am I understanding the questions right by writing the right code and to assist with the compiling error in point 1.

Compiling error: F:\Assignments\Assignment 3\Chapter 10\2 - Using Abstract Classes and Methods\LoopTestArray.java:29: cannot find symbol
symbol : method getType()
location: class Animal

System.out.println("For the " + (i+1) + ": "+ma[i].getType());
^
1 error

Tool completed with exit code 1

Array code:
import java.util.*;

public class LoopTestArray
{
	public static void main(String[] args)
	{
		Animal[] ma = {new Cat("Kitty" , "Angora"), new Robin("Rockin"), new Dog("Pluto","Great Dame"), new Fish("Nemo","Clownfish")};

		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");
		Dog dog = new Dog("Pluto", "Great Dame");
		Fish fish = new Fish("Nemo", "Clownfish");


		//create new ArrayList

		for(int i=0;i<ma.length;++i)
		{
			System.out.println("For the " + (i+1) + ": "+ma[i].getType());

			if(ma.equals(cat))
			{
				System.out.print("This is:  "); cat.describe();
				System.out.print("Sound:    "); cat.sound();
				System.out.print("Sleeping: "); cat.sleep();
				System.out.print("Moving:   "); cat.move();
				System.out.print("\n");

			}
			else if (ma.equals(bird))
			{
				System.out.print("This is:  "); bird.describe();
				System.out.print("Sound:    "); bird.sound();
				System.out.print("Sleeping: "); bird.sleep();
				System.out.print("Moving:   "); bird.move();
				System.out.print("\n");

			}

			else if (ma.equals(dog))
			{
				System.out.print("This is:  "); dog.describe();
				System.out.print("Sound:    "); dog.sound();
				System.out.print("Sleeping: "); dog.sleep();
				System.out.print("Moving:   "); dog.move();
				System.out.print("\n");

			}
			else if (ma.equals(fish))
			{
				System.out.print("This is:  "); fish.describe();
				System.out.print("Sound:    "); fish.sound();
				System.out.print("Sleeping: "); fish.sleep();
				System.out.print("Moving:   "); fish.move();
				System.out.print("\n");
			}
		}

		System.out.println("End of program.");
	}
}


ArrayList code:
import java.util.*;

public class LoopTestArrayList
{
	public static void main(String[] args)
	{

		//create new ArrayList
		ArrayList<String> myAnimals = new ArrayList<String>();

		//create the objects
		Cat cat = new Cat("Kitty", "Angora");
		Robin bird = new Robin("Rockin");
		Dog dog = new Dog("Pluto", "Great Dame");
		Fish fish = new Fish("Nemo", "Clownfish");

		//add objects to ArrayList
		myAnimals.add("cat");
		myAnimals.add("robin");
		myAnimals.add("dog");
		myAnimals.add("fish");


		for(String ma : myAnimals)
		{
			System.out.println("For the " + ma + ": ");

			if(ma == "cat")
			{
				System.out.print("This is:  "); cat.describe();
				System.out.print("Sound:    "); cat.sound();
				System.out.print("Sleeping: "); cat.sleep();
				System.out.print("Moving:   "); cat.move();
				System.out.print("\n");

			}
			else if (ma == "robin")
			{
				System.out.print("This is:  "); bird.describe();
				System.out.print("Sound:    "); bird.sound();
				System.out.print("Sleeping: "); bird.sleep();
				System.out.print("Moving:   "); bird.move();
				System.out.print("\n");

			}

			else if (ma == "dog")
			{
				System.out.print("This is:  "); dog.describe();
				System.out.print("Sound:    "); dog.sound();
				System.out.print("Sleeping: "); dog.sleep();
				System.out.print("Moving:   "); dog.move();
				System.out.print("\n");

			}
			else if (ma == "fish")
			{
				System.out.print("This is:  "); fish.describe();
				System.out.print("Sound:    "); fish.sound();
				System.out.print("Sleeping: "); fish.sleep();
				System.out.print("Moving:   "); fish.move();
				System.out.print("\n");
			}
		}

		System.out.println("End of program.");
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array and ArrayList : diff for() loops if any?

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 01:06 AM

I think you're not understanding the idea of inheritance very well. You don't need those ifs at all. Go back and review that part.

Quote

Indicate your for() loop had to be modified when changing from
an array to an Arraylist.


This is probably not how the assigment was originally posed. There is at least one word missing, and the missing text is significant - we're not going to be able to answer the question without it.
Was This Post Helpful? 0
  • +
  • -

#3 Anitush   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 01:13 AM

It was called an enhanced for() loop.

My husband is a Oracle Programmer and Analysist and he works in sql, php and so on.

He said that I had to do it that way. Didn't feel to happy with the results, that is way I am asking on Java forums now.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 01:18 AM

I'm familiar with enhanced for loops. :)

No, what I'm saying is the assignment as you've reproduced it is clearly missing at least one word. As it stands, it's simply not an instruction in English. "Indicate your for loop had to be changed" - do you mean "whether your loop had to be changed" or "how your loop had to be changed" or do you mean something else?
Was This Post Helpful? 1
  • +
  • -

#5 Anitush   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 02:00 AM

OOO sorry man. I am so stressed out. Been busy with this for over a week. Moved on and then came back to it.

The assignment reads as follow:

1. Re-Write AbstractTest as LoopTest.java. Create an array of Animal and then loop through the array using an enhanced for(), calling the describe(), sound(), sleep(), and move() methods, successively.

2. Change the LoopTest.java to use an ArrayList instead of an array. print your source code and indicate whether or not your for() loop had to be modified when changing from an array to an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11095
  • View blog
  • Posts: 18,982
  • Joined: 19-March 11

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 02:02 AM

So your question should answer itself. Did your for loop have to be modified?
Was This Post Helpful? 0
  • +
  • -

#7 Anitush   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 02:07 AM

Yes is did, but not sure if the code I did above is even correct and the "Array" code has a error.
Was This Post Helpful? 0
  • +
  • -

#8 Anitush   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Array and ArrayList : diff for() loops if any?

Posted 18 September 2013 - 04:21 AM

Hello can someone please help me I really don't know what to do. Have to give in this assignment.

Thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1