Polygon Polymorphism, Exercises of Object Oriented Programming

Polymorphism applied using Polygon and other shapes like rectangle and triangle.

Typology: Exercises

2020/2021

Uploaded on 12/06/2021

Shineljin
Shineljin 🇵🇭

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Raphael P. Enciso
BSCS 2-1
Main Class
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();
pf3
pf4
pf5
pf8

Partial preview of the text

Download Polygon Polymorphism and more Exercises Object Oriented Programming in PDF only on Docsity!

Raphael P. Enciso BSCS 2- 1

Main Class

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 ); } }

Polygon Class

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."); } }

Rectangle Class

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;

Hexagon Class

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."); } }

OUTPUT: