1 2 Previous Next 21 Replies Latest reply: Apr 29, 2009 11:18 PM by 843789 RSS

    inheritance problem first part compiles, but can't get the rest to compile.

    843789
      Here is the assignment :Lab #8, Objects, Inheritance and Polymorphism


      Purpose of Lab: Be able to:

      Write a Java program that defines, loads, and uses an array of objects.
      Create objects of classes.
      Understand the notion of polymorphism.
      Write superclasses and subclasses.
      Create objects of superclasses and subclasses.

      Instructions:

      1. Write an Abstract Data Type called Animal. Include two instance variables in this class: a String for name and an integer for age. Write a constructor method that takes two arguments and a second constructor that takes no arguments. Write get and set methods for each of the two instance variables. Write a toString method which displays the values of the instance variables for any object of type Animal. Write a speak method that takes no arguments, returns nothing and displays ?Arg! Arg!? on the monitor.

      2. Write a second class called Duck, a subclass of class Animal. Add an additional instance variable to those inherited from Animal: an integer called feathers (this variable is used to store the number of feathers that a particular Duck object has). Write a constructor that takes three arguments and another constructor that takes no arguments. Provide set and get methods for the feathers instance variable. Override the toString method to display the values of all instance variables belonging to a Duck type of object. Override the speak method to display a value of ?Quack! Quack!?.

      3. Write a third class called Cow, also a subclass of class Animal. Add an additional instance variable to those inherited from Animal: an integer called spots (this variable is used to store the number of spots feathers that a particular Cow object has). Write a constructor that takes three arguments and another constructor that takes no arguments. Override the toString method to display the values of all instance variables belonging to a Cow type of object. Override the speak method to display a value of ?Mooo! Mooo!?.

      4. Finally, enter, compile and execute the Farm class shown below. Make sure that all of your classes (Animal, Duck, Cow and Farm) are in the same folder. Compile all of the classes. Run the Farm program. Answer the questions below. Here is my code so far:
      public class Animals{
          private String name;
          private int age;
      
          public Animals()
          {
          }
          public Animals( String nVal, int ageVal)
          {
              name =nVal;
              age = ageVal;
          }
      
      
      
          public void setN(String nVal)
          {
              name = nVal;
          }
          public String getN()
      {
      return name;
      }
      public void setA( int ageVal)
      {
      age = ageVal;
      }
      public int getA()
      {
          return age;
      }
      public String toString()
      {
          return "\nThe name is" + name +", and the age is" + age + ".";
      }
      }
      public class Duck extends Animals
      
      {
      private int feathers;
      
      public Duck()
      {
          super();
          }
          public Duck(String nVal, int ageVal)
          {
          super(nVal,ageVal);
          feathers = feathersVal;
          }
          public void setFeathers(int feathersVal)
          {
          feathers= feathersVal;
          }
          public int getFeathers()
          {
              return feathers;
          }
          public String toString()
          {
          return  "\nThe name is" + super.getN() + ", and the age is" + super.getA()
          + "\n The Duck's feathers are" +feathers+ ".";
      
          }
      }
      public class Cow extends Animals
      {
          private int spots;
      
          public Cow(int spots)
          {
              super();
              }
              public Cow(String nVal, int spotsVal)
          {
              super(spots);
                  spotsColor = spots;
                  }
                  public void setSpots(int spots)
          {
              spots= spotsColor;
                  }
                  public int getSpots();
              }
          public String toString()
      
          {
              return  "\nThe name is" +super.getN()+ ", and the age is" + super.getA()
              + "\n The Duck's feathers are" +feathers+ "\nThe Cow's spots are" +spots+".";
      import javax.swing.JOptionPane.JOption;
      import javax.swing.*;
      
      public class Farm
      {
      
          public static void main( String[] args );
      
          Animals[]farmAnimals = new Animals[4];
                  farmAnimals[0] = new Animals("Animals", 3);
                  farmAnimals[1] = new Duck("Duck","green,grey feathers");
                  farmAnimals[2] = new Cow("Cow", 3, "brown white spots");
      any help greatly appreciated last assignment of the semester!
        • 1. Re: inheritance problem first part compiles, but can't get the rest to compile.
          699554
          The subject line is misleading, apparently this is a "i give up post, can you do my homework" thread. Where as your subject line implies your code contains compile errors yet you don't even post one compile error or the code related. And to end on an even lower note you can't even link code using CODE TAGS.

          Mel - come back with compiler errors and the corresponding code

          Ta
          • 2. Re: inheritance problem first part compiles, but can't get the rest to comp
            843789
            You didn't use code tags correctly. Simply copy/paste code from your source (not first post), highlight it and click CODE button (right above where you type your message).
            • 3. Re: inheritance problem first part compiles, but can't get the rest to comp
              843789
              public Animals()
              {  // start of class
              }  // end of class
              public Animals( String nVal, int ageVal) // not in class
              • 4. Re: inheritance problem first part compiles, but can't get the rest to comp
                699554
                flounder wrote:
                public Animals()
                {  // start of class
                }  // end of class
                public Animals( String nVal, int ageVal) // not in class
                Meh if we are going to play that game then

                Using multiple public modifiers for class in one file

                Mel
                • 5. Re: inheritance problem first part compiles, but can't get the rest to comp
                  699554
                  And another

                  Wrong
                  return "\nThe name is" super.getN() ", and the age is" + super.getA()
                  + "\n The Duck's feathers are" feathers "\nThe Cow's spots are" spots".";
                  Right
                  return "string1" + variable1 + "string2" + variable2:
                  Mel
                  • 6. Re: inheritance problem first part compiles, but can't get the rest to comp
                    843789
                    GAH!

                    I just realised that was a constructor. Geez I'm tired and trying to read unformatted code is not easy.
                    • 7. Re: inheritance problem first part compiles, but can't get the rest to comp
                      699554
                      And another
                      public static void main( String[] args );
                      
                      Animals[]farmAnimals = new Animals[4];
                      farmAnimals[0] = new Animals("Animals", 3);
                      farmAnimals[1] = new Duck("Duck","green,grey feathers");
                      farmAnimals[2] = new Cow("Cow", 3, "brown white spots");
                      Do you mean
                      public static void main( String[] args ){
                        Animals[]farmAnimals = new Animals[4];
                        farmAnimals[0] = new Animals("Animals", 3);
                        farmAnimals[1] = new Duck("Duck","green,grey feathers");
                        farmAnimals[2] = new Cow("Cow", 3, "brown white spots");
                      }
                      Mel
                      • 8. Re: inheritance problem first part compiles, but can't get the rest to comp
                        699554
                        Hey OP I see you, you can come out from behind that rock now. Remember this is your thread and it would be good to hear about your progress.

                        Mel
                        • 9. Re: inheritance problem first part compiles, but can't get the rest to compile.
                          843789
                          Here is the assignment :Lab #8, Objects, Inheritance and Polymorphism


                          Purpose of Lab: Be able to:

                          Write a Java program that defines, loads, and uses an array of objects.
                          Create objects of classes.
                          Understand the notion of polymorphism.
                          Write superclasses and subclasses.
                          Create objects of superclasses and subclasses.

                          Instructions:

                          1. Write an Abstract Data Type called Animal. Include two instance variables in this class: a String for name and an integer for age. Write a constructor method that takes two arguments and a second constructor that takes no arguments. Write get and set methods for each of the two instance variables. Write a toString method which displays the values of the instance variables for any object of type Animal. Write a speak method that takes no arguments, returns nothing and displays ?Arg! Arg!? on the monitor.

                          2. Write a second class called Duck, a subclass of class Animal. Add an additional instance variable to those inherited from Animal: an integer called feathers (this variable is used to store the number of feathers that a particular Duck object has). Write a constructor that takes three arguments and another constructor that takes no arguments. Provide set and get methods for the feathers instance variable. Override the toString method to display the values of all instance variables belonging to a Duck type of object. Override the speak method to display a value of ?Quack! Quack!?.

                          3. Write a third class called Cow, also a subclass of class Animal. Add an additional instance variable to those inherited from Animal: an integer called spots (this variable is used to store the number of spots feathers that a particular Cow object has). Write a constructor that takes three arguments and another constructor that takes no arguments. Override the toString method to display the values of all instance variables belonging to a Cow type of object. Override the speak method to display a value of ?Mooo! Mooo!?.

                          4. Finally, enter, compile and execute the Farm class shown below. Make sure that all of your classes (Animal, Duck, Cow and Farm) are in the same folder. Compile all of the classes. Run the Farm program. Answer the questions below. Here is my code so far:
                          public class Animals{
                          private String name;
                          private int age;

                          public Animals()
                          {

                          public Animals( String nVal, int ageVal) }
                          {
                          name =nVal;
                          age = ageVal;
                          }



                          public void setN(String nVal)
                          {
                          name = nVal;
                          }
                          public String getN()
                          {
                          return name;
                          }
                          public void setA( int ageVal)
                          {
                          age = ageVal;
                          }
                          public int getA()
                          {
                          return age;
                          }
                          public String toString()
                          {
                          return "\nThe name is"+name+ , and the age is" + age + ".";
                          }
                          }
                          {code}{code}
                          {code}{code}
                          public class Duck extends Animals

                          {
                          private int feathers;

                          public Duck()
                          {
                          super();
                          }
                          public Duck(String nVal, int ageVal)
                          {
                          super(nVal,ageVal);
                          feathers = feathersVal;
                          }
                          public void setFeathers(int feathersVal)
                          {
                          feathers= feathersVal;
                          }
                          public int getFeathers()
                          {
                          return feathers;
                          }
                          public String toString()
                          {
                          return "\nThe name is" + super.getN() + ", and the age is" + super.getA()
                          + "\n The Duck's feathers are" +feathers+ ".";

                          }
                          }
                          {code}{code}
                          {code}{code}
                          public class Cow extends Animals
                          {
                          private int spots;

                          public Cow(int spots)
                          {
                          super();
                          }
                          public Cow(String nVal, int spotsVal)
                          {
                          super(spots);
                          spotsColor = spots;
                          }
                          public void setSpots(int spots)
                          {
                          spots= spotsColor;
                          }
                          public int getSpots();
                          }
                          public String toString()

                          {
                          return "string1" + variable1 + "string2" + variable2;
                          {code}{code}
                          {code}{code}
                          import javax.swing.JOptionPane.JOption;
                          import javax.swing.*;

                          public class Farm
                          {

                          public static void main( String[] args ){

                          Animals[]farmAnimals = new Animals[4];
                          farmAnimals[0] = new Animals("Animals", 3);
                          farmAnimals[1] = new Duck("Duck","green,grey feathers");
                          farmAnimals[2] = new Cow("Cow", 3, "brown white spots");

                          }

                          }
                          {code}{code}
                          In Duck The compile errors are as follows:java1: cannot find symbol symbol class Duck extends Animals with the caret under A in Animals
                          java 13 same as above but for feathers = feathersVal; with the caret under f in feathersVal
                          java 25 same error caret under s in both supers

                          Cow has 1 error in java 20 class,interface,or enum expected caret under S in String
                          Farm a parsing error at last brace but when I add another ending brace i get
                          java1 cannot find symbol class JOption caret under period between JOptionPane.JOption
                          java 9 cannot find symbol caret under A in Animals[]farm etc also under A in new Animals
                          java 10 cannot find symbol caret under D in Duck
                          java 12 cannot find symbol caret under C in new Cow
                          • 10. Re: inheritance problem first part compiles, but can't get the rest to compile.
                            843789
                            I would strongly advise you to re-read the posts here and take to heart what they say. Why not come back after you've made some changes based on the recommendations. Also, please use code tags when posting code here (as has already been mentioned above).
                            • 12. Re: inheritance problem first part compiles, but can't get the rest to compile.
                              843789
                              public class Animals{
                              private String name;
                              private int age;

                              public Animals()
                              {
                              }
                              public Animals( String nVal, int ageVal)
                              {
                              name =nVal;
                              age = ageVal;
                              }



                              public void setN(String nVal)
                              {
                              name = nVal;
                              }
                              public String getN()
                              {
                              return name;
                              }
                              public void setA( int ageVal)
                              {
                              age = ageVal;
                              }
                              public int getA()
                              {
                              return age;
                              }
                              public String toString()
                              {
                              return "\nThe name is" + name + "and the age is" + age + ".";
                              }
                              }
                              public class Duck extends Animals

                              {
                              private int feathers;

                              public Duck()
                              {
                              super();
                              }
                              public Duck(String nVal, int ageVal)
                              {
                              super(nVal,ageVal);
                              feathers = feathersVal;
                              }
                              public void setFeathers(int feathersVal)
                              {
                              feathers= feathersVal;
                              }
                              public int getFeathers()
                              {
                              return feathers;
                              }
                              public String toString()
                              {
                              return "\nThe name is" + super.getN() + ", and the age is" + super.getA()
                              + "\n The Duck's feathers are" feathers ".";

                              }
                              }
                              public class Cow extends Animals
                              {
                              private int spots;

                              public Cow(int spots)
                              {
                              super();
                              }
                              public Cow(String nVal, int spotsVal)
                              {
                              super(spots);
                              spotsColor = spots;
                              }
                              public void setSpots(int spots)
                              {
                              spots= spotsColor;
                              }
                              public int getSpots();
                              }
                              public String toString()

                              {
                              return "string1" + variable1 + "string2" + variable2;
                              }
                              }
                              import javax.swing.JOptionPane.JOption;
                              import javax.swing.*;

                              public class Farm
                              {

                              public static void main( String[] args ){

                              Animals[]farmAnimals = new Animals[4];
                              farmAnimals[0] = new Animals("Animals", 3);
                              farmAnimals[1] = new Duck("Duck","green,grey feathers");
                              farmAnimals[2] = new Cow("Cow", 3, "brown white spots");

                              }
                              }
                              the compile errors are in my last code post at the bottom
                              • 13. Re: inheritance problem first part compiles, but can't get the rest to compile.
                                EJP
                                You didn't use the code tags, and this code still contains errors that were pointed out previously, such as here.

                                If you're not going to take any notice of what you're told here what's the point of posting exactly?
                                • 14. Re: inheritance problem first part compiles, but can't get the rest to compile.
                                  843789
                                  Maybe because I supposed people wanted to see the original as it was written with the code tags instead!
                                  1 2 Previous Next