Ask your own question, for FREE!
Computer Science 22 Online
OpenStudy (anonymous):

can any one show me how to write a class for quadratic function in python can any one show me how to write a class for quadratic function in python @Computer Science

OpenStudy (anonymous):

What methods and attributes must the class possess?

OpenStudy (anonymous):

I'm thinking it must have an a, b, and c attributes. Might be cool to have a solver method.

OpenStudy (anonymous):

the methods er x and attributes must be a ,b ,c

OpenStudy (anonymous):

if you can also write out a table of x and f values

OpenStudy (anonymous):

class QuadraticFunction(object): """ I don't know how to write good docstrings. Sorry! """ def __init__(self, a, b, c): """ I don't know how to write good docstrings. Sorry! """ self.a = a self.b = b self.c = c def solve(): import cmath sol1 = (-self.b + cmath.sqrt(self.b ** 2 - 4 * self.a * self.c)) / 2 sol2 = (-self.b - cmath.sqrt(self.b ** 2 - 4 * self.a * self.c)) / 2 return sol1, sol2 def table(xtable): """ Given a table of x values, returns a list of tuples """ table1 = [] for number in xtable: x = float(number) tuple = x, self.a * (x**2) + self.b * x + self.c table1.append(tuple) return table1

OpenStudy (anonymous):

would be cool to have a __repr__ or __str__ method so you can print out the quadratic function.

OpenStudy (anonymous):

here you go. http://ideone.com/71MJ0 try it out on http://repl.it

OpenStudy (anonymous):

will try,,,,thanks,,,

OpenStudy (anonymous):

Is it alright?

OpenStudy (anonymous):

er you there

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

just wana ask you,,,if you can write out a table of x and f values for x in interval [2, 10]

OpenStudy (anonymous):

i mean in quadratic function that i gave to yo

OpenStudy (anonymous):

thanks

OpenStudy (anonymous):

what quadratic function did you give me?

OpenStudy (anonymous):

i mean the class quadratic function,,,,

OpenStudy (anonymous):

that's easy. using the code I've written, you would call the table() method with the argument range(2,11), making sure to assign the return value to some name so you create a table with (x, f(x)) tuples, and then you print out all those tuples using a for loop. But if you like to pretty print it, I think the better way would be to write another method.

OpenStudy (anonymous):

have to tried the class quadratic function code,,,

OpenStudy (anonymous):

er you there

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!