Inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javatech007
    New Member
    • Nov 2007
    • 51

    Inheritance

    I am finding trouble using inheritance in my solution to the following question:

    "Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of the shape in the following format: [a, b].
    Next write a class called orange that is a child class of shapes. This time the constructor accepts in a, b (centre of the orange), and a radius. Override the toPrinter() method in the orange class to include outputting the radius, as well as a and b. Write a method to get the area of the orange. Include any relevant classes to run and test the application."

    I can the first part of the question but not the second paragraph. I am finding it difficult using inheritance.

    Below is my solution so far.Can anyone spot where I am going wrong with the inheritance?

    [CODE=java]public class Shapes_5
    {
    public static void main(String[] args)
    {

    Shapes_Values c = new Shapes_Values() ;

    c.setValueA(5);
    c.setValueB(14) ;

    c.goToPrinter() ;

    }
    }



    class Shapes_Values
    {

    int ValueA;
    int ValueB;


    void setValueA(int iValueA) //setter method
    {

    ValueA = iValueA;

    }


    void setValueB(int iValueB) //setter method
    {

    ValueB = iValueB;

    }

    int getValueA() //getter method
    {

    return ValueA;

    }

    int getValueB() //getter method
    {

    return ValueB;

    }

    void goToPrinter() //Outputs the coordinates of the shape
    {

    System.out.prin tln("[" + ValueA + "," + ValueB + "]");

    }

    }


    class Orange extends Shapes_5
    {
    Shapes_Values c = new Shapes_Values() ;

    c.setValueRadiu s(2);
    }

    class Shapes_Values
    {

    int ValueRadius;

    void setValueRadius( int iValueRadius) //setter method
    {

    ValueRadius = iValueRadius;

    }

    int getValueRadius( ) //getter method
    {

    return ValueRadius;

    }

    }[/CODE]


    Thanks in advance!!
    Last edited by Ganon11; Dec 2 '07, 10:39 PM. Reason: Please look up the correct way to use [CODE] tags.
  • heat84
    New Member
    • Nov 2007
    • 118

    #2
    You have not created a constructor for your shape class . Either way , a and b can be instance variables of your class so you need to declare them in your shape class and create public methods that allow for accessing and changing data in the shape class (accessor and mutator methods).
    Last edited by r035198x; Dec 3 '07, 08:44 AM. Reason: code tags bug had struck again

    Comment

    • javatech007
      New Member
      • Nov 2007
      • 51

      #3
      [QUOTE=javatech0 07]I am finding trouble using inheritance in my solution to the following question:

      "Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of the shape in the following format: [a, b].
      Next write a class called orange that is a child class of shapes. This time the constructor accepts in a, b (centre of the orange), and a radius. Override the toPrinter() method in the orange class to include outputting the radius, as well as a and b. Write a method to get the area of the orange. Include any relevant classes to run and test the application."

      I can the first part of the question but not the second paragraph. I am finding it difficult using inheritance.

      Below is my solution so far.Can anyone spot where I am going wrong with the inheritance?

      [CODE=java]public class Shapes_5
      {
      public static void main(String[] args)
      {

      Shapes_Values c = new Shapes_Values() ;

      c.setValueA(5);
      c.setValueB(14) ;

      c.goToPrinter() ;

      }
      }



      class Shapes_Values
      {

      int ValueA;
      int ValueB;


      void setValueA(int iValueA) //setter method
      {

      ValueA = iValueA;

      }


      void setValueB(int iValueB) //setter method
      {

      ValueB = iValueB;

      }

      int getValueA() //getter method
      {

      return ValueA;

      }

      int getValueB() //getter method
      {

      return ValueB;

      }

      void goToPrinter() //Outputs the coordinates of the shape
      {

      System.out.prin tln("[" + ValueA + "," + ValueB + "]");

      }

      }


      class Orange extends Shapes_5
      {
      Shapes_Values c = new Shapes_Values() ;

      c.setValueRadiu s(2);
      }

      class Shapes_Values
      {

      int ValueRadius;

      void setValueRadius( int iValueRadius) //setter method
      {

      ValueRadius = iValueRadius;

      }

      int getValueRadius( ) //getter method
      {

      return ValueRadius;

      }

      }[/CODE]



      When I go to compile the code, one error shows up in line 68 saying an identifer is expected!! Any ideas what i did wrong dere!!??

      Comment

      • javatech007
        New Member
        • Nov 2007
        • 51

        #4
        When I go to compile the code, one error shows up in line 68 saying an identifer is expected!! Any ideas what i did wrong dere!!??

        Comment

        Working...