Question

In: Computer Science

a)Adoubledata field(private)named realfor real part of a complex number. b)Adoubledata field(private)named imgfor imaginarypart of a complex...

a)Adoubledata field(private)named realfor real part of a complex number.

b)Adoubledata field(private)named imgfor imaginarypart of a complex number.

c)A no-arg constructor that creates a default complex number with real 0and img 0.

d)Auser-defined constructorthat creates a complex number with given 2 numbers.

e)The accessor and mutator functions for realand img.

f)A constant function named addition(Complex&comp1, Complex&comp2) that returns the sum of two givencomplex numbers.

g)Aconstantfunction named subtraction(Complex&comp1, Complex&comp2) that returns the subtractionof two givencomplex numbers.

h)A constant function named multiplication(Complex&comp1, Complex&comp2) that returns the multiplicationof two givencomplex numbers.

i)Write a test program that creates aComplexobject with no-arg constructor.Then, set -4.2 and 3.1to real and imaginary parts of the complex number, respectively.

j)Create another Complexobject using the user-defined constructor.

k)Testaddition, subtractionand multiplication functions using thesetwoComplex objects.

Solutions

Expert Solution

The code for given problem statement:

#include<iostream>
using namespace std;
class Complex
{
    //  access: private
    private:
        double real,img;    //  real and imaginary part of complex number
    //  access: public  
    public:
        Complex()   //  Default constructor
        {
            //  Initializes values to 0
            real=0;
            img=0;
        }
        Complex(double r, double i)
        {
            //  Parameterized constructor
            //  Initializes value at the the time of object creation 
            real=r;
            img=i;
        }
        double R_accessor()     //  For accessing real part  
        {
            return real;
        }
        double I_accessor()     //  For accessing imaginary part 
        {
            return img;
        }
        void R_mutator(double r)    //  For updating value of real part
        {
            real=r;
        }
        void I_mutator(double i)    //  For updating value of imaginary part
        {
            img=i;
        }
        Complex addition(Complex &comp1,Complex &comp2) const
        {
            Complex add;    //  Object of Complex to store addition 
            double r=comp1.R_accessor()+comp2.R_accessor();     //  Addition of real parts
            double i=comp1.I_accessor()+comp2.I_accessor();     //  Addition of imaginary parts
            add.R_mutator(r);
            add.I_mutator(i);
            return add;
        }
        Complex subtraction(Complex &comp1,Complex &comp2) const
        {
            Complex sub;    //  Object of Complex to store subtraction
            double r=comp1.R_accessor()-comp2.R_accessor();     //  Subtraction of real parts
            double i=comp1.I_accessor()-comp2.I_accessor();     //  Subtraction of imaginary parts
            sub.R_mutator(r);
            sub.I_mutator(i);
            return sub;
        }
        Complex multiplication(Complex &comp1,Complex &comp2) const
        {
            Complex mul;    //  Object of Complex to store multiplication
            double r=comp1.R_accessor()*comp2.R_accessor()-comp1.I_accessor()*comp2.I_accessor();   //  To calculate real part
            double i=comp1.R_accessor()*comp2.I_accessor()+comp2.R_accessor()*comp1.I_accessor();   //  To calculate imaginary part
            mul.R_mutator(r);
            mul.I_mutator(i);
            return mul;
        }
};
int main()
{
    Complex a;  //  First object of Complex to initialize to 0+0i
    //  Assigning values to real and imaginary part of 'a'
    a.R_mutator(-4.2);  
    a.I_mutator(3.1);
    Complex b(1.2,2.2);     //  Using user defined parameterized constructor to create and initialize 2nd complex number 
    cout<<"1st complex number is: ";
    cout<<a.R_accessor()<<" + ("<<a.I_accessor()<<"i)"<<"\n";
    cout<<"2nd complex number is: ";
    cout<<b.R_accessor()<<" + ("<<b.I_accessor()<<"i)"<<"\n";
    Complex add=add.addition(a,b);  //  Adding 2 complex numbers and storing into 'add' 
    cout<<"Addition of given complex number is: ";
    cout<<add.R_accessor()<<" + ("<<add.I_accessor()<<"i)"<<"\n";
    Complex sub=sub.subtraction(a,b);  //  subtracting 2 complex numbers and storing into 'subtract' 
    cout<<"Subtraction of given complex number is: ";
    cout<<sub.R_accessor()<<" + ("<<sub.I_accessor()<<"i)"<<"\n";
    Complex mul=mul.multiplication(a,b);  //  multiplying 2 complex numbers and storing into 'add' 
    cout<<"Multiplication of given complex number is: ";
    cout<<mul.R_accessor()<<" + ("<<mul.I_accessor()<<"i)"<<"\n";
    return 0;
}

Screenshot of the code:

Screenshot of output:

Logic of the code:

A class is defned to store a complex number.

Comlex number is written in the form a+bi where a is real part and b is imaginary part and i=sqrt(-1)

There are 2 private data members: One for storing real part and other for storing imaginary part.

There are 2 constructors. One is default constructor to initialize both data members to 0. Second is parameterized constructor to initialize data members to given values when creating the object.

There are 2 mutators. 1 for real part and 2nd for imaginary part. Mutators are functions used to update the data members.

There are 2 accessors. 1 for real part and 2nd for imaginary part. Accessors are functions used to access the data members.

There are 3 constant functions.

1) Addition: Adds real parts and imaginary parts indivisually for provided 2 comlex objects

2) Subtraction: Subtracts real parts and imaginary parts indivisually for provided 2 comlex objects

3) Multiplication: Multiplication of 2 complex numbers is done

The example taken to test code is: -4.2+3.1i and 1.2+2.2i

Addition=(-4.2+1.2)+(3.1+2.2)i= -3 +5.3i

Subtraction=(-4.2-1.2)+(3.1-2.2)i= -5.4+0.9i

multiplication=((-4.2*1.2)-(3.1*2.2))+((-4.2*2.2)+(3.1*1.2))i = -11.86-5.52i


Related Solutions

A complex number is a number with two components, namely, the real part (rel) and the...
A complex number is a number with two components, namely, the real part (rel) and the imaginary part (img). For example the complex number (a+bi) has a as the real part and b as the imaginary part. In developing such a class, assume that both of the real and imaginary parts to be floats. The symbol i represent . A declaration for the Complex class is presented next. class Complex { public:    Complex (float r = 0.0, float g=...
Private int data field named id for the account A private double data field named balance...
Private int data field named id for the account A private double data field named balance for the account Private double data field named annualInterestRate that stores the current interest rate Assume all accounts have the same rate Private date data field named date Created that stores the date when the account was created No arg constructor that creates an account with the specified id and initial balance The get and set methods for id balance and annual Interest Rate...
1. Find the real part, the imaginary part, and the modulus of the complex number 1...
1. Find the real part, the imaginary part, and the modulus of the complex number 1 + 8i 2 + 3i , showing your work. 2. Find all three solutions of the equation 2z 3 + 4z 2 −z −5 = 0. (Hint: First try a few “simple” values of z.) You must show all working.
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
Design a class named Account that contains: ■ A private int data field named id for...
Design a class named Account that contains: ■ A private int data field named id for the account (default 0). ■ A private double data field named balance for the account (default 0). ■ A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. ■ A private Date data field named dateCreated that stores the date when the account was created. ■ A no-arg constructor that creates...
You have a class named AirConditioner that has a private boolean field named on. It has...
You have a class named AirConditioner that has a private boolean field named on. It has method named turnOn that sets the field to true and one named turnOff that set it to false. It also has a method named isOn that returns the value of the on field. Fill in the blank to complete the turnOff method. public void turnOff() { ______________________ }
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final...
Implement a class named Complex that represents immutable complex numbers. Your class needs two private final fields of type double to store the real and imaginary parts. Try to use constructor chaining to implement 2 of the 3 required constructors. If you cannot complete one or more of the methods, at least make sure that it returns some value of the correct type; this will allow the tester to run, and it will make it much easier to evaluate your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT