Guest User

Untitled

a guest
Oct 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class Main { // example for non-virtual function implementation public double sum(Shape2D[] shapes) { double total = 0; for (int i = 0; i < shapes.length; i++) { if (shapes[i] instanceof Rectangle) { total += ((Rectangle)shapes[i]).area(); } else if (shapes[i] instanceof Circle) { total += ((Circle)shapes[i]).area(); } else if (shapes[i] instanceof Parallelogram) { total += ((Parallelogram)shapes[i]).area(); } // modify source code here for new sub classes } return total; } public static void main(String[] argv) { Shape2D[] data; // array of reference to Shape2D data = new Shape2D[5]; // create array object data[0] = new Rectangle(2.4, 3.8); // Polymorphism data[1] = new Circle(3.9); data[2] = new Parallelogram(3.5, 6.7, 10.2); data[3] = new Rectangle(5.3, 7.2); data[4] = new Circle(4.6); System.out.println("Sum of all Shape2D is "+sum(data)); }}
Add Comment
Please, Sign In to add comment