View Single Post
  #2 (permalink)  
Old 11-27-2007, 09:21 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
From this class, derive a class named Box
This means that Box extends (from) Rectangle.
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.
Reply With Quote