Question

In: Computer Science

Create an abstract class polygon that has a method getnoofsides() and getarea() and it has three...

Create an abstract class polygon that has a method getnoofsides() and getarea() and it has three subclasses triangle, rectangle and square each with its own two methods getnoofsides and getarea and their respective implementations accordingly.

python

Solutions

Expert Solution

from abc import ABC, abstractmethod

"""Python does not support abstract class but we have abc which stands for Abstract Base Classes for abstract class"""


class Polygon(ABC):  # parent class
    @abstractmethod
    def getnoofsides(self):
        pass

    def getarea(self, b, h):
        pass

    def getareasquare(self, length):  # square has all sides equal so only one parameter needed
        pass


class Triangle(Polygon):
    def getnoofsides(self):
        super().getnoofsides()  # super method to override the parent class method
        print("It's a triangle with 3 sides")

    def getarea(self, b, h):
        super().getarea(b, h)
        area = (1 / 2) * b * h
        print("Area of triangle is:", area)


class Rectangle(Polygon):
    def getnoofsides(self):
        super().getnoofsides()
        print("It's a Rectangle with 4 sides")

    def getarea(self, b, h):
        super().getarea(b, h)
        area = h * b
        print("Area of triangle is:", area)


class Square(Polygon):
    def getnoofsides(self):
        super().getnoofsides()
        print("It's a square with 4 sides")

    def getareasquare(self, length):
        super().getareasquare(length)
        areaofsquare = length * length
        print("Area of square is:", areaofsquare)


T = Triangle()
T.getnoofsides()
T.getarea(float(input("Enter the breadth")), float(input("Enter the height")))  # float is used to change string input
# to float value (type casting) 

R = Rectangle()
R.getnoofsides()
R.getarea(float(input("Enter the breadth")), float(input("Enter the length")))

S = Square()
S.getnoofsides()
S.getareasquare(float(input("Enter the length")))

Related Solutions

Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return...
Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Derive a class BulkDiscount from DiscountPolicy. It should have a constructor that has two parameters minimum and percent. It should define the method computeDiscount so that if the quantity purchased of an item is more then minimum, the discount is percent percent.
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class....
In Java, create an abstract vehicle class. Create a Truck Class that Inherits from Vehicle Class. The vehicle class should contain at least 2 variables that could pertain to ANY vehicle and two methods. The truck class should contain 2 variables that only apply to trucks and one method. Create a console program that will instantiate a truck with provided member information then call one method from the truck and one method contained from the inherited vehicle class. Have these...
i just need the Polygon class Shapes In this lab you will create an interface, and...
i just need the Polygon class Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double area none computes the area of the shape double perimeter none computes the perimeter of the shape Point2d center none computes the center of the shape You will then create 3 implementations of this interface: Rectangle, Circle, and...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
The abstract class SayHello and the static method process are defined as followed: class SayHello {...
The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(   )             {                         greeting = “Hello guys”;             } SayHello( String wts )             {                         greeting = wts;             }             void printGreeting( ); } static void process( SayHello obj ) {             obj.printGreeting(   ); } Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the...
Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the...
Create an interface MessageEncoder that has a single abstract method encode(plainText) where the plainText is the message to be encoded. The method will return the encoded version of the message. Then create a class SubstitutionCipher that implements this interface. The constructor should have on parameter called shift. Define the method encode so that each letter is shifted by the value in shift. For example if shift is 3 a will be replaced by d, b will be replaced by e....
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat...
Make Animal an abstract class. Create a Kennel Class Create 2-3 Dog objects Create 2-3 Cat objects Put your Dog and Cat objects in an Array of Animals Loop over your Animals and print the animal with Species, Age, and the status of the appropriate vaccines. Using the code below: public class Animal { //Declaring instance variables private int age; private boolean RabiesVaccinationStatus; private String name; private String ownerName; //Zero argumented constructor public Animal() { } //Parameterized constructor public Animal(int...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
Create a class that has a method that uses the ‘this’ keyword as the return statement....
Create a class that has a method that uses the ‘this’ keyword as the return statement. This method (called increment( ) in the class that you have just created ) increments the private integer field (private i ) in the class.   In the main ( ) method, if you increment (using the ‘this’ with increment() call) four times, you should see the following display: i is = 4 Thank You
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT