




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Polymorphism applied using Polygon and other shapes like rectangle and triangle.
Typology: Exercises
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Raphael P. Enciso BSCS 2- 1
package Polygon_Polymorphism; public class Main { public static void main(String[] args) { System.out.println("=====================================\nPOLYGO N:"); Polygon polygon = new Polygon(); polygon.sides(); polygon.perimeter(); polygon.area(); System.out.println("\n=====================================\nTRIA NGLE:"); Triangle triangle = new Triangle(); triangle.sides(); triangle.perimeter( 3 , 4 , 5 ); triangle.area( 7 , 7 ); System.out.println("\n=====================================\nRECT ANGLE:"); Rectangle rectangle = new Rectangle(); rectangle.sides(); rectangle.perimeter( 4 , 5 ); rectangle.area( 4 , 5 ); System.out.println("\n=====================================\nSQUA RE:"); Square square = new Square(); square.sides();
square.perimeter( 7 );; square.area( 7 ); System.out.println("\n=====================================\nHEXA GON:"); Hexagon hexagon = new Hexagon(); hexagon.sides(); hexagon.perimeter( 6 ); hexagon.area( 6 ); } }
package Polygon_Polymorphism; public class Polygon { void sides() { System.out.println("All polygons have sides."); } void perimeter() { System.out.println("Perimeter is the total measurement of all the edges of a shape."); } void area() { System.out.println("Area is the amount of space within the perimeter of a 2D shape."); } }
System.out.println("Perimeter: " + ans + " units."); } void perimeter(double side1, double side2, double side3) { double ans = side1 + side2 + side3; System.out.println("Perimeter: " + ans + " units."); } void area(int base, int height) { double ans = (0.5baseheight); System.out.println("Area: " + ans + " units."); } void area(int base, double height) { double ans = (0.5baseheight); System.out.println("Area: " + ans + " units."); } void area(double base, int height) { double ans = (0.5baseheight); System.out.println("Area: " + ans + " units."); } void area(double base, double height) { double ans = (0.5baseheight); System.out.println("Area: " + ans + " units."); } }
package Polygon_Polymorphism; public class Rectangle extends Polygon { void sides() { System.out.println("Sides: 4 sides."); } void perimeter(int length, int width) { int ans = (( 2 length) + ( 2 width)); System.out.println("Perimeter: " + ans + " units."); } void perimeter(int length, double width) { double ans = (( 2 length) + (2.00fwidth)); System.out.println("Perimeter: " + ans + " units."); } void perimeter(double length, int width) { double ans = (( 2 length) + (2.00fwidth)); System.out.println("Perimeter: " + ans + " units."); } void perimeter(double length, double width) { double ans = (( 2 length) + (2.00fwidth)); System.out.println("Perimeter: " + ans + " units."); } void area(int length, int width) { int ans = lengthwidth; System.out.println("Area: " + ans + " units."); } void area(int length, double width) { double ans = lengthwidth; System.out.println("Area: " + ans + " units."); } void area(double length, int width) { double ans = length*width;
package Polygon_Polymorphism; public class Hexagon extends Polygon { void sides() { System.out.println("Sides: 6 sides."); } void perimeter(int s) { int ans = 6 s; System.out.println("Perimeter: " + ans + " units."); } void perimeter(double s) { double ans = 6.00fs; System.out.println("Perimeter: " + ans + " units."); } void area(int s) { double ans = ( 6 (ss))/( 4 *Math.tan(Math.PI/ 6 )); System.out.println("Area: " + ans + " units."); } void area(double s) { double ans = ( 6 (ss))/( 4 *Math.tan(Math.PI/ 6 )); System.out.println("Area: " + ans + " units."); } }