Question

In: Computer Science

in Python, Define the class called Line which represents a line with equation ?=??+? with input...

in Python,

Define the class called Line which represents a line with equation ?=??+? with input slope ? and intercept ? to initialize the instances. It should include:

attributes named ? and ? to represent slope and intercept.
method named intersect to return the list, containing coordinates of the intersection point of two lines.
support for + operator to compute the addition of two equations. The sum of two Line objects ?=?1?+?1 and ?=?2?+?2 is defined as the line ?=(?1+?2)?+?1+?2 .
printable representation for the equation of line, which we have already defined in __repr__ speical method below.

Solutions

Expert Solution

class Line:

    # initializing constructor with slope and y-intercept
    def __init__(self, k, b):
        self.k = k
        self.b = b

    # return intersection point of two lines
    def intersect(self, line):
        # formula of point of intersection (x,y)
        x_ = (line.b - self.b)/(self.k - line.k)
        y_ = self.k * x_ + self.b
        # returns intersection point
        return [x_, y_]

    # adds two lines and returns the new line
    def __add__(self, line):
        k_ = self.k + line.k
        b_ = self.b + line.b
        return Line(k_, b_)

    # prints the line
    def __repr__(self):
        return "y = {}x + {}".format(self.k, self.b)


line1 = Line(2,3)
line2 = Line(3,4)

# prints the intersection of two lines
print(line1.intersect(line2))

# adds two lines
print(line1 + line2)

Code

Output


Related Solutions

Define and implement a class named Coach, which represents a special kind of person. It is...
Define and implement a class named Coach, which represents a special kind of person. It is to be defined by inheriting from the Person class. The Coach class has the following constructor: Coach(string n, int sl) // creates a person with name n, whose occupation // is “coach” and service length is sl The class must have a private static attribute static int nextID ; which is the unique personID of the next Coach object to be created. This is...
In python can you fix the error in the line where it says message = input("Input...
In python can you fix the error in the line where it says message = input("Input a lowercase sentence: ") this is the error message I get after running the program and inputing a lowercase sentence Input a lowercase sentence:hello hi MM§MTraceback (most recent call last): MM§M File "client.py", line 14, in <module> MM§M message = input("Input a lowercase sentence:") MM§M File "<string>", line 1 MM§M hello hi MM§M ^ MM§MSyntaxError: unexpected EOF while parsing from socket import * #...
a. Write the equation of the line that represents the linear approximation to the following function...
a. Write the equation of the line that represents the linear approximation to the following function at the given point a. b. Use the linear approximation to estimate the given quantity. c. compute the percent error in the approximation, 100* (|approximation-exact|)/|exact|, where the exact value is given by a calculator. f(x)=15-3x^2 at a=2; f(1.9)
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
You are to create a class called ManageDB. The ManageDB class will have a 2 input...
You are to create a class called ManageDB. The ManageDB class will have a 2 input constructor that has the following definition: ManageDB(int number, String fileName) The constructor will create an array of EmployeeDB objects of length "number". The constructor will read the file located at "fileName" and extract the information from that file to populate a database of EmployeeDB objects. The file contains information on the EmployeeDB objects. This information is contained in records. The format of each record...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
PYTHON A Class for a Deck of Cards We will create a class called Card whose...
PYTHON A Class for a Deck of Cards We will create a class called Card whose objects we will imagine to be representations of playing cards. Each object of the class will be a particular card such as the '3' of clubs or 'A' of spades. For the private data of the class we will need a card value ('A', '2', '3', ... 'Q', 'K') and a suit (spades, hearts, diamond, clubs). Before we design this class, let's see a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT