I need some Java help

DilLw33D

Veteran X
RIght now im taking this class on programming languages where they teach you about what goes into making a language. Every so often they give programming assignments on in different languages but never teach you anything about the language. Im having a lot of trouble with this Java.

Code:
public class Pet
{
        protected String pet_name;
        protected String owner_name;
        protected String color;
        protected int sex;
        public static final int MALE=0, FEMALE=1, SPAYED=2, NEUTERED=3;
        //make an array for hte strings for MALE, FEMALE, etc...
        String[] sexarray = {"MALE", "FEMALE", "SPAYED", "NEUTERED"};

        public Pet(String name, String ownerName, String c)
        {
                pet_name = name;
                owner_name = ownerName;
                color = c;
        }

        public String getPetName()
        {
                return pet_name;
        }

        public String getOwnerName()
        {
                return owner_name;
        }

        public String getColor()
        {
                return color;
        }

        public void setSex(int sexid)
        {
                sex = sexid;
        }

        public String getSex()
        {
                return sexarray[sex];
        }

       public String toString()
        {
                return (pet_name + " owned by " + owner_name + "\nColor: " + color + "\nSex: " + getSex());
        }
}

this works fine but when i have to write my class Cat that extends Pet is when stuff doenst work

Code:
public class Cat extends Pet
{
  private String hairLength;
  //Pet temp;

  public Cat(String name, String OwnerName, String c, String HairLength)
  {
   pet_name = name;
    owner_name = OwnerName;
    color = c; 
    //Pet temp = new Pet(name, OwnerName, c);
    hairLength = HairLength;
  }

  public String getHairLength()
  {
    return hairLength;
  }

  public String toString()
        {
                return ("CAT: \n" + getPetName() + "is owned by " + getOwnerName() + "\nColor: " + getColor()
                        + "\nSex: " + getSex() + "\nHair: " + getHairLength());
        }

}

i get this strange error and i have no clue how to fix it... someone please help me!
 
it says:
Cat.jav9: cannot resolve symbol
symbol: constructor Pet()
location: class Pet
{
^

1 error



yes when i compile pet.java it gives me no errors and works fine
 
And just kind of java code convention thing, instead of setting the variables for pet in your cat class call the constructor for pet using super, i.e.

super(name, 0wnername, c);

Yeah, but hard to tell you how to fix it without the error.
 
hmm

it has to do with what Einsteinium said... Java automatically calls super() (the default constructor for the base class) when it constructs the derived class, but there's no default constructor (there's no pet() function, there's just pet(String, String, String))... you have to call super yourself, with the arguments required.

*edit*
I think ;)
 
Last edited:
eM.Einsteinium said:
And just kind of java code convention thing, instead of setting the variables for pet in your cat class call the constructor for pet using super, i.e.

super(name, 0wnername, c);

Yeah, but hard to tell you how to fix it without the error.
yeah this is exactly what i needed to do
thanks fellas
 
Back
Top