Results 1 to 2 of 2
  1. #1
    nhlfan is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default help with oop code

    Hello guys,

    I am doing the following exercise:

    a) Create a base class named Rectangle that contains length and width data members. From this class, derive a class named Box having an additional data member named depth. The method members of the base Rectangle class should consist of a constructor and have an area( ) method. The derived Box class should have a constructor and an override method named area( ) that returns the surface area of the box and a volume( ) method.

    b) Include the classes constructed for Exercise 8a in a working Java program. Have your program call all of the member methods in each class and explain the result when the area method is called using two Box objects.

    I have 3 separate files : Rectangle.java, Box.java, and UsingShapes.java
    This is what I have so far:

    Rectangle.java

    Java Code:
    public class Rectangle
    {
     
    protected double length;
    protected double width;
     
    public Rectangle (double lh, double wh)
    {
     
    length = lh;
    width = wh;		
     
    }
     
    public double area()
    {
    	return(length * width);
    }
     
    }


    Box.java

    Java Code:
    public class Box {
    protected double depth;	
     
    public Box (double lh, double wh, double dh)
     
    {
    super(lh);
    super(wh);
    depth = dh;	
    }
     
    public double area()
     
    {
    	return ((2*super.area)+(2*(dh*super(lh))+ (2*(dh*super(wh))))); //surface area of box = 2(h*w) + 2(h*l) + 2(w*l)
    }
     
    public double volume()
    {
    	return (depth * super.area());
    }
     
    }


    UsingShapes.java (File which runs the other 2 files)

    Java Code:
    public class UseShapes {
    public static void main (String[] args) {
     
    Rectangle rect1 = new Rectangle(2,4);
    Box bv = new Box(5,5,5);
    	
    Box boxOne = new Box(1, 2, 3);
    Box boxTwo = new Box(4, 5, 6);
     
    System.out.println("The area of the rectangle is " + rect1.area);
    System.out.println("The volume of the box is " + bv.volume);
     
    System.out.println("The surface area of boxOne is " +boxOne.area);
    System.out.println("The surface area of boxTwo is " +boxTwo.area);	
    	
    	}
    }
    Any help? Thanks alot

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    From this class, derive a class named Box
    This means that Box extends (from) Rectangle.
    Java Code:
    public class UseShapesRx
    {
        public static void main (String[] args)
        {
            RectangleRx rect1 = new RectangleRx(2,4);
            BoxRx bv = new BoxRx(5,5,5);
            System.out.println("The area of the rectangle is " + rect1.area());
            System.out.println("The volume of the box is " + bv.volume());
    
            BoxRx boxOne = new BoxRx(1, 2, 3);
            BoxRx boxTwo = new BoxRx(4, 5, 6);
            System.out.println("The surface area of boxOne is " +boxOne.area());
            System.out.println("The surface area of boxTwo is " +boxTwo.area());	
        }
    }
    
    public class BoxRx extends RectangleRx
    {
        protected double depth;	
    
        public BoxRx(double lh, double wh, double dh)
        {
            super(lh, wh);
            depth = dh;	
        }
     
        public double area()
        {
            //surface area of box = 2(h*w) + 2(h*l) + 2(w*l)
            double area = 2*(length*width + width*depth + length*depth);
            return area;
        }
    
        public double volume()
        {
            return width * length * depth;
        }
    }
    No changes in Rectangle class but name changed to RectangleRx for this.

Similar Threads

  1. Need help with my code.
    By stormviper in forum New To Java
    Replies: 8
    Last Post: 11-18-2013, 04:58 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 03:02 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 05:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •