In: Computer Science
03 Prepare : Checkpoint B
Objective
Demonstrate basic class methods (member functions) in
Python.
After completing (or while completing) the
preparation material for this week, complete the following
exercise.
Introduction
Recall from your mathematics classes that a complex
number is composed of two parts, a real part and an imaginary part.
We could write a complex number in the form "3 + 4i" where 3 is the
real part and 4 is the imaginary part. For this program, we will
create a new class for complex numbers, and write methods (member
functions) to prompt for them and display them.
Instructions
Write a Python 3 program according to the
following:
Create a class for a Complex number that has two
member variables, "real" and "imaginary".
Create an initializer function for this class that
sets each part to 0.
Create a prompt method (member function) that will
prompt the user for the two values and set them
appropriately.
Create a display method (member function) that
displays the complex number in the form "3 + 4i". For this
assignment, you need not worry about handling negative numbers, or
about simplifying the display if either value is 0.
Then, in your main function you should create two new
complex numbers. Display them (which should show 0 + 0i), prompt
the user for each one, and display them again.
To help you see how to work with these values, a main
function is provided for you. Your task is to create the Complex
class to make this work.
The file to start with is found at:
/home/cs241/check03b.py. You can copy it to your directory with the
following command:
cp /home/cs241/check03b.py .
Don't forget the "." at the end, it tells linux to
copy it to your current directory.
Sample Output
The following is an example of output for this
program:
The values are: 0 + 0i 0 + 0i Please enter the real part: 3 Please
enter the imaginary part: 4 Please enter the real part: 6 Please
enter the imaginary part: 10 The values are: 3 + 4i 6 + 10i
Automatic Grading Script (TestBed)
This assignment is pass/fail. In order to receive
credit, it must pass the auto-grading test script (TestBed). You
can run this script as many times as you like while you are working
on the assignment. The same test script will be run by the
instructor for credit, so you should not be surprised at the
result.
In addition, because the point of this exercise is to
help you practice the use of classes. You must use classes
to receive credit for this assignment.
To run the test script, log into the CS Department
Linux system and run the testBed command, providing the class and
test script to run as well as the Python program to use, for
example:
testBed cs241/check03b check03b.py
SOLUTION:
class Complex:
# Setting real and imaginary values to zero
initially
def __init__(self):
self.real = 0
self.imaginary = 0
# Prompting the user for the values of real and
imaginary
def prompt(self):
self.real =
int(input("Please enter the real part: "))
self.imaginary =
int(input("Please enter the imaginary part: "))
# Diplaying the complex number in the form x +
yi
def display(self):
print(str(self.real) + "
+ " + str(self.imaginary) + "i")
# Main Function
if __name__ == "__main__":
# Creating the two complex numbers as complex1
and complex2
complex1 = Complex()
complex2 = Complex()
#Printing their initial values
print("The values are: ")
complex1.display()
complex2.display()
# Prompting the user for new values for complex1
and complex2
complex1.prompt()
complex2.prompt()
# Printing the new values
print("The values are: ")
complex1.display()
complex2.display()
The above solution corresponds with the details mentioned in the question and the required output. It is recommended to use the above main function only for the final submission in order to avoid any conflicts in the output.