Object oriented design for more than one class . Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AbrahamNkomo
    New Member
    • Aug 2010
    • 4

    Object oriented design for more than one class . Java

    Hi .l am real new to programming . l have a problem with my code .l have tried to translate the following programming problem into java language but the problem l have is that l cannot create the subclass that is Cube class.Here is the programming problem:Design a class called Square with a value for the side of the square and validate that value and calculate the area of the square .The child class Cube will use the existing methods of the parent class to validate and receive the side of the cube and create a new method to calculate and display the volume of the cube.Below are the code for two relational classes.Hoping for assistance.


    Code:
    /**
     *
     * @author Abraham Nkomo
     */
    public class Square
    {
      public double sideLength;
    
        public Square(double inSideLength )
    {
        sideLength  = inSideLength;
    }
        double Area;
         public void calculateSquare()
         {
             if (validInput)
          
        Area = sideLength * sideLength;
    
        else
             System.out.println("Invalid input");
    
         }
         boolean validInput = true;
         {
    
             if (sideLength < 0)
             validInput =  false;
        
             else
                 validInput = true;
        }
        public double displayArea()
    {
        return Area;
    }
    }

    Below is the code for the child class

    Code:
    /**
     *
     * @author Abraham Nkomo
     */
    
       public class Cube extends Square
    
      {
           
           
         
     
        double cubeVolume ;
        public  void  calculateCubeVolume()
        {
           validInput = true;
            if(validInput)
                cubeVolume =  sideLength * sideLength * sideLength;
    
            else
                System.out.println("Invalid input");
    
         }
        public double getCubeVolume()
    
        {
                return cubeVolume;
    
        }
    
    
    }

    Please help me to fix my code.
    Thanks in advance

    KKHULUZA NKOMO
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    I am not clear with your question.

    But following may be the OOP thing you are expecting.
    No need to calculate the volume again. directly return the volume like this.

    Code:
    Square parentobj = new Square(sideLength);
    return (parentobj.calculateSquare())*sideLength;
    Regards
    Dheeraj Joshi

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi!

      It's been a while since I did anything like that, but I think you'll need a constructor of the form
      Code:
      public Cube (double inSideLength )
      {
        super(inSideLength);
        // Do other stuff, that is different from what you did in the Square class
      }
      Greetings,
      Nepomuk

      Comment

      Working...