Question

In: Computer Science

***JAVA PLEASE*** All correct and complete responses will receive a thumbs up and positive comment! Thank...

***JAVA PLEASE*** All correct and complete responses will receive a thumbs up and positive comment! Thank you!!

Create a simple class that emulates some mathematical functions in relation to complex numbers. A complex number is a number of the form a + bi, where a is a real number and bi is an imaginary number. Create a class Complex having two private data members real and imag of type double. The class has two constructors, one default (no parameters) and one whose parameters initialize the instance variables. It also need the following member functions, all of which are public:

  • getReal – returns the real number component
  • getImag – returns the imaginary number component
  • setReal – sets the value of the real number component
  • setImag – sets the value of the imaginary component
  • addComplex – has one parameter, another Complex object to add to this one. This method does not return a value (it updates the calling object’s variables).
  • subtractComplex - has one parameter, another Complex object to subtract to this one. This method does not return a value (it updates the calling object’s variables).
  • multiplyComplex - has one parameter, another Complex object to multiply to this one. This method does not return a value (it updates the calling object’s variables).
  • print – prints the data in the form a + bi

**** ALSO Write a driver program to test all of the above functions***

Hints: For complex numbers:

Addition: add the real and imaginary components separately:

(7 + 4i) + (23 – 12i) = 30 – 8i

Subtraction: subtract the real and imaginary components separately:

(7 + 4i) - (23 – 12i) = -16 + 16i

Multiplication: More complicated

Solutions

Expert Solution

Code

Complex.java


public class Complex
{
private double real,imag;

public Complex()
{
this.real=0;
this.imag=0;
}

public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}

public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public double getImag() {
return imag;
}

public void setImag(double imag) {
this.imag = imag;
}
public void addComplex(Complex z)
{
real=real+z.real;
imag=imag+z.imag;
}
public void subtractComplex (Complex z)
{
real=real-z.real;
imag=imag-z.imag;
}
public void multiplyComplex (Complex z)
{
real=(real*z.real)-(imag*z.imag);
imag=(real*z.imag)+(z.real*imag);
}
  
public void print ()
{
if (imag==0) {
System.out.print("( "+imag);
}
else if (imag>0)
{
System.out.print("("+real+"+"+imag+"i)");
}
else
System.out.print("("+real+" - "+(-imag)+"i)");
}
  
}

Driver file

TestComple.java


public class TestComolex
{
public static void main(String[] args)
{
Complex c1=new Complex(7,4);
Complex c2=new Complex(23,-12);
c1.print();
System.out.print(" + ");
c2.print();
c1.addComplex(c2);
System.out.print(" = ");
c1.print();
System.out.println("");
  
}
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

PLEASE ANSWER ALL PARTS FOR A THUMBS UP RATING, THANK YOU! 1) What are the main...
PLEASE ANSWER ALL PARTS FOR A THUMBS UP RATING, THANK YOU! 1) What are the main elements to take into consideration in preparing a financial forecast? 2) Once you develop an initial set of projected financial statements, what are some things to check to make sure the forecast makes sense? 3) In general, is it easier to plug a cash shortfall with debt or equity? Explain.
PLEASE ANSWER QUESTIONS WITH ALL CALCULATIONS! WILL GIVE THUMBS UP ! THANK YOU ! 6. Use...
PLEASE ANSWER QUESTIONS WITH ALL CALCULATIONS! WILL GIVE THUMBS UP ! THANK YOU ! 6. Use the information below and the Two-stage Growth Model to find Axis Capital’s intrinsic value.  Forecast its cash flows from t+1 to t+3.  If its current price is $37.54, is it over or under valued? t t+1 t+2 t+3 t+4 Beta 0.80 ROE 0.09 0.08 0.08 0.08 DPO 0.37 0.34 0.34 0.34 Div $1.64
Please answer all 3 questions CORRECTLY to receive a thumbs up rating. Not doing so, will...
Please answer all 3 questions CORRECTLY to receive a thumbs up rating. Not doing so, will result in a thumbs down rating. Thank you. 1. Era 2 sees the emergence of: a. severe global competition. b. hierarchy and bureaucracy. c. constant change. d. pre-industrial and pre-bureaucratic society. 2. Researchers found that CEOs in turnaround situations—where companies must improve results quickly: a. relationship skills were more valuable leader qualities. b. teamwork was the most valuable leader quality. c. task-focused characteristics were...
FORECASTING , THE COMPLETE ANSWERS TO THESE QUESTIONS WILL RECEIVE A THUMBS UP! Weekly demand figures...
FORECASTING , THE COMPLETE ANSWERS TO THESE QUESTIONS WILL RECEIVE A THUMBS UP! Weekly demand figures at Hot Pizza are as follows: Week Demand 1 108 2 116 3 118 4 124 5 96 6 119 7 96 8 102 9 112 10 102 11 92 12 91 Estimate demand for the next 4 weeks using a 4-week moving average as well as simple exponential smoothing with α = 0.1. Evaluate the MAD, MAPE, MSE, bias, and TS in each...
Please select ALL the correct answers for a thumbs up. Thanks :) 1. MMs’ Proposition II...
Please select ALL the correct answers for a thumbs up. Thanks :) 1. MMs’ Proposition II states that the value of the firm depends on all of the following except, Required rate of return on the firm’s assets Cost of debt of the firm Debt to equity ratio of the firm Cost of equity of the firm 2. which of the following types of risk is most difficult to manage? a. interest rate risk b. exchange rate risk c. price...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200 words or more, please explain why angel investments are important. *NOTICE* I do NOT need a definition of what angel investors are, I just need to know why angel investing is important
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200 words or more, list some examples of angel investment trends in the United States. *NOTICE* I do NOT need a definition of what angel investors are!
please leave alot of quality information and i will provide a thumbs up in return thank...
please leave alot of quality information and i will provide a thumbs up in return thank you 1. Joan is an Accountant who opposes the introduction of a new financial control system. For 15 years she has worked with a manual system. Now the firm is introducing a new computer-based system. How would you, as her manager, attempt to change Joan's attitude towards the new system? 2. Some people believe that perception is a more important explanation of behavior than...
Need this ASAP, I will thumbs up! Please show work, thank you! Jerry needs to plan...
Need this ASAP, I will thumbs up! Please show work, thank you! Jerry needs to plan for his weekend activity. He may or may not go to play tennis, depending on his friends’ availability. If both Andrew and Bob are going, then Jerry will go with them. Or, if Charlie will go, then Jerry will also go with him. Otherwise, Jerry will not go to play tennis. Please design a logic circuit that makes the decision for Jerry automatically based...
COMPLETE ALL PARTS AND ILL LEAVE YOU A THUMBS UP A retail company is reevaluating its...
COMPLETE ALL PARTS AND ILL LEAVE YOU A THUMBS UP A retail company is reevaluating its inventory policy for meeting a constant annual demand of 5,200 units for its top-selling product. The current inventory policy is to order 650 units each time. The company purchases the product from a manufacturer at a wholesale price of $20 per unit. The company also pays $80 as the ordering cost for each order regardless of the order quantity. The company’s inventory capital is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT