We are supposed to create several classes including rectangle,square, and right triangle. Each class must have a uniform way of calling methods, hence abstract methods. What I don't understand is that my instructor wants us to take the rectangle class, and derive a square and right triangle class from it which makes sense in a way, but when I create objects I can't understand how to setup the constructors so that when I create a right triangle object, I don't have to also enter a length and width of a rectangle. Here is my code.
import java.util.Scanner; public class ProgramDriver{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); Shapes[] container = new Shapes[13]; Rectangle rec1 = new Rectangle(10, 12); Rectangle rec2 = new Rectangle(6, 8); container[0] = rec1; //putting rectangle objects in container container[1] = rec2; Square sq1 = new Square(0,0,6); Square sq2 = new Square(0,0,8); container[2] = sq1; container[3] = sq2; //putting square objects in container RightTriangle rt1 = new RightTriangle(0,0,5,10); RightTriangle rt2 = new RightTriangle(0,0,6,12); container[4] = rt1; container[5] = rt2; //putting right triangle objects in container Circle c1 = new Circle(8); Circle c2 = new Circle(10); container[6] = c1; container[7] = c2; //putting circle objects in container Sphere s1 = new Sphere(15); Sphere s2 = new Sphere(10); container[8] = s1; container[9] = s2; //putting sphere objects in container Prism p1 = new Prism(5,3,2); Prism p2 = new Prism(6,3,4); container[10] = p1; //putting rectangular prism objects in container container[11] = p2; System.out.println("Rectangle 1" + "\n\nArea: " + container[0].getArea() + "\nPerimeter: " + container[0].getPerimeter() + "\nVolume: " + container[0].getVolume()); System.out.println("\nRectangle 2" + "\n\nArea: " + container[1].getArea() + "\nPerimeter: " + container[1].getPerimeter() + "\nVolume: " + container[1].getVolume()); System.out.println("\nSquare 1" + "\n\nArea: " + container[2].getArea() + "\nPerimeter: " + container[2].getPerimeter() + "\nVolume: " + container[2].getVolume()); System.out.println("\nSquare 2" + "\n\nArea: " + container[3].getArea() + "\nPerimeter: " + container[3].getPerimeter() + "\nVolume: " + container[3].getVolume()); System.out.println("\nRight Triangle 1" + "\n\nArea: " + container[4].getArea() + "\nPerimeter: " + container[4].getPerimeter() + "\nVolume: " + container[4].getVolume()); System.out.println("\nRight Triangle 2" + "\n\nArea: " + container[5].getArea() + "\nPerimeter: " + container[5].getPerimeter() + "\nVolume: " + container[5].getVolume()); System.out.println("\nCircle 1" + "\n\nArea: " + container[6].getArea() + "\nPerimeter: " + container[6].getPerimeter() + "\nVolume: " + container[6].getVolume()); System.out.println("\nCircle 2" + "\n\nArea: " + container[7].getArea() + "\nPerimeter: " + container[7].getPerimeter() + "\nVolume: " + container[7].getVolume()); System.out.println("\nSphere 1" + "\n\nArea: " + container[8].getArea() + "\nPerimeter: " + container[8].getPerimeter() + "\nVolume: " + container[8].getVolume()); System.out.println("\nSphere 2" + "\n\nArea: " + container[9].getArea() + "\nPerimeter: " + container[9].getPerimeter() + "\nVolume: " + container[9].getVolume()); System.out.println("\nRectangular Prism 1" + "\n\nArea: " + container[10].getArea() + "\nPerimeter: " + container[10].getPerimeter() + "\nVolume: " + container[10].getVolume()); System.out.println("\nRectangular Prism 2" + "\n\nArea: " + container[11].getArea() + "\nPerimeter: " + container[11].getPerimeter() + "\nVolume: " + container[11].getVolume()); } }public abstract class Shapes{ public Shapes(){ } public abstract double getArea(); public abstract double getPerimeter(); public abstract double getVolume(); }
public class Rectangle extends Shapes{ private double length; private double width; public Rectangle(double lr, double wr){ length = lr; width = wr; } public double getArea(){ double area = length * width; return area; } public double getPerimeter(){ double perimeter = (width*2)+(length*2); return perimeter; } public double getVolume(){ //2-d shapes cannot have volume return 0; } }
public class Square extends Rectangle{ private double side; public Square(double lr, double wr, double s){ super(lr,wr); side = s; } public double getArea(){ double area = side * side ; return area; } public double getPerimeter(){ double perimeter = side * 4; return perimeter; } public double getVolume(){ //2-d shapes cannot have volume return 0; } }public class RightTriangle extends Rectangle{ private double base; //this is an equilateral triangle private double height; public RightTriangle(double lr, double wr, double b, double h){ super(lr,wr); base = b; height = h; } public double getArea(){ double area = .5 * (base * height); return area; } public double getPerimeter(){ double perimeter = base * 3; return perimeter; } public double getVolume(){ return 0;//2-d shapes cannot have volume } }
If you look at the programdriver class, when I create several of the objects, I don't understand how to setup the right triangle/square class so that I don't need to enter data for a non-existant rectangle. Any tips?