Question

In: Computer Science

03 Prepare : Checkpoint B Objective Demonstrate basic class methods (member functions) in Python. After completing...

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

Solutions

Expert Solution

  • The question indicates that the main function has already been provided and only the Complex class has to be written.
  • Since the main function is not visible in the question and in order to avoid any naming conflicts, the below solution is accompanied with a main function that carries out the operations as required.

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.


Related Solutions

Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing)...
Objective Demonstrate composition in Python (a class that contains another). Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class to model books and the author that wrote it. Note the relationship that a book has an author. At this point, you should put all of your classes and other code in the same file. Don't worry about separating it out into separate modules. Instructions Write...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods...
05 Prepare : Checkpoint A Objective This assignment will give you practice writing event handling methods to advance a ship in two dimensional space. Overview After completing (or while completing) the preparation material for this week, complete the following exercise. For this exercise, you will create a class that models a ship flying around in a 2-D plain (containing an x and y coordinate). A game class is provided to you that will help you call methods to advance and...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could...
Create a message encoder/decoder. PLEASE USE BASIC PYTHON METHODS/FUNCTIONS. The user enters a message that could only include alphabetic letters and space. There are 26 alphabetic letters. Consider space the 27th letter. The user then enters a shift code that should be an integer between -26 and 26. The application will show the encoded/decoded message based on the shift code entered. If you encode a message, each letter in the message will be moved forward through the alphabet according to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT