Question

Must be in Python 3.6 (please have a screen shot on the code)

12.1 (The Triangle class) Design a class named Triangle that extends the Geometricobject class. The Triangle class contains Three float data fields named sidel, side2, and side3 to denote the three sides of the triangle. A constructor that creates a triangle with the specified sidel, side2, and side3 with default values 1.0 The accessor methods for all three data fields. A method named getArea O that returns the area of this triangle A method named getPerimeterO that returns the perimeter of this triangle. A method named-str-O that returns a string description for the triangle.

ex 2.14 :

# Enter three points for a triangle
x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: "))

# Compute the length of the three sides
side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5
side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)) ** 0.5
side3 = ((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)) ** 0.5

s = (side1 + side2 + side3) / 2;
area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5

print("The area of the triangle is", area)

GeometricObject class:

0 0
Add a comment Improve this question Transcribed image text
Answer #1



# please refer to screenshot and take care if indentations

class Geometricobject: def init (self, color=green , filled= True): self. colorColor self. filled-filled def getcolor (self38 39 40 41 def setSide2 (self, side1) self . side2 = side2 def getSide3 (self): return side3 def setSide3 (self, sidel): sel

python code

class GeometricObject:

def __init__(self, color = "green", filled = True):

self.__color = color

self.__filled = filled

def getColor(self):

return self.__color

def setColor(self, color):

self.__color = color

def isFilled(self):

return self.__filled

def setFilled(self, filled):

self.__filled = filled

  

def __str__(self):

return "color: " + self.__color + \

" and filled: " + str(self.__filled)

class Triangle(GeometricObject):

def __init__(self, color, filled, side1 = 1.0, side2 = 1.0, side3 = 1.0):

super(Triangle, self).__init__(color, filled)

self.side1 = side1

self.side2 = side2

self.side3 = side3

def getSide1(self):

return side1

def setSide1(self, side1):

self.side1 = side1

def getSide2(self):

return side2

def setSide2(self, side1):

self.side2 = side2

def getSide3(self):

return side3

def setSide3(self, side1):

self.side3 = side3

def getArea(self):

from math import sqrt

s = (side1+side2+side3)/2

a = sqrt(s*(s-side1)*(s-side2)*(s-side3))

return a

def getPerimeter(self):

return side1+side2+side3

def __str__(self):

return "Triangle: side1 = "+str(side1)+" side2 = "+str(side2)+" side3 = "+str(side3)

# Test Code

side1 = eval(input("Enter side1 of triangle: "))

side2 = eval(input("Enter side1 of triangle: "))

side3 = eval(input("Enter side1 of triangle: "))

filled = int(input("Enter triangle is filled or not (1 or 0) : "))

filled = True if filled == 1 else False

color = input("Enter color of triangle : ")

triangle = Triangle(color, filled, side1, side2, side3)

print()

print("Area of triangle is : ",triangle.getArea())

print("Perimeter of triangle is : ",triangle.getPerimeter())

print("Color of triangle is : ",triangle.getColor())

print("Triangle is filled : ",triangle.isFilled())

SAMPLE OUTPUT:

CAWindows System321cmd.exe C: \Users \SAM\Desktop\cg\Python>py code.py Enter side1 of triangle: 5 Enter side1 of triangle: 4

Add a comment
Know the answer?
Add Answer to:
Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 :...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3,...

    Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the distance of two points (x1, y1) and (x2, y2) is d = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); or d = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5); The formula for computing the...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Create a UML diagram with 3 lines per class/interface including all constructors. public class Point {...

    Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides();    public double getPerimeter();    public double getArea();    } public abstract class Simple_polygon implements Polygon{ public Point...

  • (Need to complete the methods and pass a Junit test i can email them to you.)...

    (Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable {    /**    * Stores the number of objects instantiated from the {@code Triangle} class    */    private static int numTriangles;    /**    * The shortest side of the triangle    */    private final double a;    /**    * The side of medium length of the triangle    */    private final double b;...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT