Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my homework: http://courses.missouristate.edu/JamilSaquer/CSC131/HOMEWOR

ID: 3666179 • Letter: H

Question

Here is my homework: http://courses.missouristate.edu/JamilSaquer/CSC131/HOMEWORK/hw2_Circle2D.html

Here is my code:

import math

class Circle2D(object):
    def _init_(self, x=0, y=0, r=0):
        self._x = x
        self._y = y
        self._r = r

    def _str_(self):
        return("Circle with center" + "(" + "%0.0f" % (self._x) + ", "
               + "%0.0f" % (self._y) + ")" + "and radius " + "%0.0f" % (self._r))
   
    def getX(self):
        return self._x

    def getY(self):
        return self._y

    def getRadius(self):
        return self._r

    def setx(self, x):
        self._x = x

    def sety(self, y):
        self._y = y

    def setRadius(self, r):
        self._r = r

    def gerArea(self):
        return math.pi * self._r ** 2

    def getPerimeter(self):
        return math.pi * self._r * 2
   
    def containsPoint(self, x, y):
        d = distance(x, y, self._x, self._y)
        return d <= self._r
       
    def contains(self, Ciecle2D):
        d = distance(self._x, self._y, circle._x, circle._y)
        return d + circle._radius <= self._r

    def overlaps(self, Circle2D):
        return distance(self._x, self._y, circle._x, circle._y) <= self._r + circle._r

    def _contains_(self, anotherCircle):
        return self.contains(anotherCircle)

    def _lt_(self, secondCircle):
        return self._cmp_(secondCircle) < 0

    def _le_(self, secondCircle):
        return self._cmp_(secondCirce) <= 0

    def _gt_(self, secondCircle):
        return self._cmp_(secondCircle) > 0

    def _ge_(self, secondCircle):
        return self._cmp_(secondCircle) >= 0

    def _cmp_(self, secondCircle):
        if self.r > secondCircle._r:
            return 1
        elif self.r < secondCircle._r:
            return -1
        else:
            return 0


def main():
    x = float(input("Enter x coordinate for the center of circle 1: "))
    y = float(input("Enter y coordinate for the center of circle 1: "))
    r = float(input("Enter the radius of circle 1: "))
    c1 = Circle2D(x, y, r)
    print(c1)

    x = float(input(" Enter x coordinate for the center of circle 2: "))
    y = float(input("Ennter y coordinate for the center of circle 2: "))
    r = float(input("Enter the radius of circle 2: "))
    print(c2)

    #Test the getArea() and getPerimeter() methods
    print(" Area of a %s is %0.2f" % (c1, c1.getArea()))
    print("Perimeter of a %s is %0.2f" % (c1, c1.getPerimeter()))

    print(" Area of a %s is %0.2f" % (c2, c2.getArea()))
    print("Perimeter of a %s is %0.2f" % (c2, c2.getPerimeter()))
    #-------------------

    #Test containsPoint() method
    print(" Result of c1.containsPoint(c2.getX( ), c2.getY( )) is",
          c1.containsPoint(c2.getX( ), c2.getY( )))

    #Test contains() method
    if c1.contains(c2):
        print(" %s contains %s" % (c1, c2))
    else:
        print(" %s does not contain %s" % (c1, c2))
                                         
    print(" Result of c2.contains(c1) is",
           c2.contains(c1))
    #----------------

    #Test overlap() method
    if c1.overlaps(c2):
        print(" %s overlaps with %s" % (c1,c2))
    else:
        print(" %s does not overlap with %s" % (c1,c2))
    #--------------

    #Test overloaded in operator                                    
    print(" Result of c2 in c1 is", c2 in c1)                    

    #Testing overloaded comparison and equality operators
    #Something similar to this
    print(" Testing overloaded comparison operators...")
    print("c1 == c2?", c1 == c2)
    print("c1 != c2?", c1 != c2)
    print("c1 < c2?", c1 < c2)
    print("c1 <= c2?", c1 <= c2)
    print("c1 > c2?", c1 > c2)
    print("c1 >= c2?", c1 >= c2)
    print('c1 == "Hello"?', c1 == "Hello")
    print('c1 != "Hello"?', c1 != "Hello")
   
main()

My code did not run, but I do not know what I fix this code. SO i need help.

thank you.

Explanation / Answer

There were lot of spelling mistakes, and _init_, _cmp should be __init__ and __cmp__. I have changed overridden methods too. Last two statements will fail because "Hello" is not of type circle.

import math

def distance(x1,y1,x2,y2):
return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
class Circle2D(object):
def __init__(self, x=0, y=0, r=0):
self._x = x
self._y = y
self._r = r
def __str__(self):
return("Circle with center" + "(" + "%0.0f" % (self._x) + ", "
+ "%0.0f" % (self._y) + ")" + "and radius " + "%0.0f" % (self._r))

def getX(self):
return self._x
def getY(self):
return self._y
def getRadius(self):
return self._r
def setx(self, x):
self._x = x
def sety(self, y):
self._y = y
def setRadius(self, r):
self._r = r
def getArea(self):
return math.pi * self._r ** 2
def getPerimeter(self):
return math.pi * self._r * 2
      
  

def containsPoint(self, x, y):
d = distance(x, y, self._x, self._y)
return d <= self._r

def contains(self, circle):
d = distance(self._x, self._y, circle._x, circle._y)
return d + circle._r <= self._r
def overlaps(self, circle):
return distance(self._x, self._y, circle._x, circle._y) <= self._r + circle._r
def __contains__(self, anotherCircle):
return self.contains(anotherCircle)
def __lt__(self, secondCircle):
return self.__cmp__(secondCircle) < 0
def __le__(self, secondCircle):
return self.__cmp__(secondCircle) <= 0
def __gt__(self, secondCircle):
return self.__cmp__(secondCircle) > 0
def __ge__(self, secondCircle):
return self.__cmp__(secondCircle) >= 0
def __cmp__(self, secondCircle):
if self._r > secondCircle._r:
return 1
elif self._r < secondCircle._r:
return -1
else:
return 0

def main():
x = float(input("Enter x coordinate for the center of circle 1: "))
y = float(input("Enter y coordinate for the center of circle 1: "))
r = float(input("Enter the radius of circle 1: "))
c1 = Circle2D(x, y, r)
print(c1)
x = float(input(" Enter x coordinate for the center of circle 2: "))
y = float(input("Ennter y coordinate for the center of circle 2: "))
r = float(input("Enter the radius of circle 2: "))
c2 = Circle2D(x,y,r)
print(c2)
#Test the getArea() and getPerimeter() methods
print(" Area of a %s is %0.2f" % (c1, c1.getArea()))
print("Perimeter of a %s is %0.2f" % (c1, c1.getPerimeter()))
print(" Area of a %s is %0.2f" % (c2, c2.getArea()))
print("Perimeter of a %s is %0.2f" % (c2, c2.getPerimeter()))
#-------------------
#Test containsPoint() method
print(" Result of c1.containsPoint(c2.getX( ), c2.getY( )) is",
c1.containsPoint(c2.getX( ), c2.getY( )))
#Test contains() method
if c1.contains(c2):
print(" %s contains %s" % (c1, c2))
else:
print(" %s does not contain %s" % (c1, c2))

print(" Result of c2.contains(c1) is",
c2.contains(c1))
#----------------
#Test overlap() method
if c1.overlaps(c2):
print(" %s overlaps with %s" % (c1,c2))
else:
print(" %s does not overlap with %s" % (c1,c2))
#--------------
#Test overloaded in operator
print(" Result of c2 in c1 is", c2 in c1)
#Testing overloaded comparison and equality operators
#Something similar to this
print(" Testing overloaded comparison operators...")
print("c1 == c2?", c1 == c2)
print("c1 != c2?", c1 != c2)
print("c1 < c2?", c1 < c2)
print("c1 <= c2?", c1 <= c2)
print("c1 > c2?", c1 > c2)
print("c1 >= c2?", c1 >= c2)
print('c1 == "Hello"?', c1 == "Hello")
print('c1 != "Hello"?', c1 != "Hello")

main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote