OO Programming and Data Structures | CS 241

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:

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

Submission

Submit your program by logging into the CS Department Linux system, and running the submit command with your filename, for example:


submit check03b.py

You should then select the appropriate course (CS 241), instructor, and assignment (check03b) from the list.