I'm doing a lesson that is supposed to demonstrate inheritance using the song Old MacDonald; there's an animal interface, a farm class that implements each animal object.. you get the idea. I've got two problems.
First,
The animal has a type and a sound, for example, a chick is

public class Chick implements Animal{
  private String myType;
  private String mySound;

  Chick(){
    myType = "chick";
    mySound = "chick";
  }

  public String getSound(){
    return mySound;
  }

  public String getType(){
    return myType;
  }
}

I'm supposed to create a second constructor with a flag that says whether a chick is childish and returns the sound "cheep" or is adult and returns "cluck" so that there's an equal change of getSound() returning either. I have no idea how to do this. I considered adding a boolean to the constructor, but that won't work because the farm class can only create a new Chick(); I realize you're not just going to hand me an answer, but I don't even know where to start.

The other issue is that I'm supposed to create a NamedCow class that, as you probably guessed, gives the cow a name. I'm supposed to be able to add a named cow to an arraylist by typing

myFarm.add(new Cow("Elsie"));

. I can't just add a getName method to the cow class, namedCow has to be a new class. I figure okay, simple,

public class NamedCow extends Cow{
	private String myName;
	NamedCow(String name){
		myName = name;
	}
	public String getName(){
		return myName;
	}
	
}

Obviously that doesn't work. I really don't know what to do, and any help is greatly appreciated.

Why can't the farm class create a new Chick object with the syntax Chick(true) or Chick(false) depending on if it is young or old? And you can also add a randomizer if you wanted to, and add isYoung() and isOld() methods. And why can't you add that method to the Cow class? Also, why is Animal an interface -- a Chick should extend Animal, because a Chick is-a Animal. But can you post your complete code? I think it would clarify your questions. Because several suggestions you point out you say you can't do either b/c the Farm class won't allow it (which you didn't show us) or that you can't add a name to a Cow but i don't see why not. If you post your full code it should help us to see exactly what's going wrong.

lab: http://tinyurl.com/2g24jk9
As you can see, I'm given a set farm class, a set animal interface, and I need to make cow and chick do the above actions. I can't add to cow because the lab requires that I create a NamedCow class so that i have to implement a class that extends another, and I can't use a boolean because I'm givena final farm class. Thank you for your patience.
My complete code:

public interface Animal{
  public String getSound();
  public String getType();
}

cow

public class Cow implements Animal{
  private String myType;
  private String mySound;

  Cow(){
    myType = "cow";
    mySound = "moo";
  }

  public String getSound(){
    return mySound;
  }

  public String getType(){
    return myType;
  }
}

chick

public class Chick implements Animal{
  private String myType;
  private String mySound;

  Chick(){
    myType = "chick";
    mySound = "chick";
  }
   public String getSound(){
    return mySound;
  }

  public String getType(){
    return myType;
  }
}

farm

import java.util.*;
public class Farm
{
   private ArrayList <Animal> myFarm;

   public Farm() {
      myFarm = new ArrayList <Animal>();
      myFarm.add(new Cow());
      myFarm.add(new Chick());
      myFarm.add(new Pig());
      myFarm.add(new Cow("Elsie"));
   }

   public void animalSounds(){
      Animal temp;
      for(int i = 0; i < myFarm.size(); i++){
         temp = myFarm.get(i);
         System.out.println(temp.getType() + " goes " + temp.getSound());
      }

      NamedCow named = (NamedCow)myFarm.get(4);
      System.out.println(named.getName());
   }
}

anyone?

anyone?

Where you the one who wrote the Farm class?
I need to know this because, I need to understand what was given to you initially.

Also you could do this:

myFarm.add(new NamedCow("Elsie"));

Where you the one who wrote the Farm class?
I need to know this because, I need to understand what was given to you initially.

Also you could do this:

myFarm.add(new NamedCow("Elsie"));

The farm class was premade, i did not write it.

Member Avatar for joankim

Hey. I guess I am a little late, but I had the same assignment and same problem, and I am sure people after me will too.
So, I couldn't figure out how to do the NamedCow part. But you dont have to. Just overload the constructor in Cow.java like this:

Cow(){
        myType = "Cow";
        mySound = "moo";
     }
     Cow(String name){
        myName = name;
        myType = "Cow";
        mySound = "moo";
     }

You will obviously have to change some other stuff, but the errors will be easy to fix from here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.