Question

In: Computer Science

Your first lab is to create a simple class that emulates some mathematical functions in relation...

Your first lab is to 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

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 (will be discussed in lab).

You MUST write a driver program to test all of the above functions. I also want a UML diagram for the Complex class.

The lab must be turned in on Blackboard. I need to see your Complex class, driver program (call it ComplexRunner.java), and UML diagram. MAKE SURE TO COMMENT YOUR CODE CORRECTLY!

Solutions

Expert Solution

​/*Source code*/ :


class ComplexRunner {
/* driver program */
public static void main(String[] args) {
Complex a = new Complex(5.0, 6.0);
Complex b = new Complex(-3.0, 4.0);

System.out.println("a = " + a.print());
System.out.println("b = " + b.print());

System.out.println("Real part of a = " + a.getReal());
System.out.println("Imaginary part of a = " + a.getImaginary());

System.out.println("Real part of b = " + b.getReal());
System.out.println("Imaginary part of b = " + b.getImaginary());

Complex c = new Complex();

c.setReal(4.0);
c.setImaginary(-8.0);

System.out.println("c = " + c.print());

/* add two complex numbers */
a.addComplex(b);
System.out.println("a + b = " + a.print());

/* subtract two complex numbers */
b.subtractComplex(a);
System.out.println("a - b = " + b.print());

/* multiply two complex numbers */
a.multiplyComplex(b);
System.out.println("a * b = " + a.print());
}

}
public class Complex {

/* complex number's real and imaginary part */
private double real;
private double imaginary;

/* default constructor */
public Complex() {
this.real = 0;
this.imaginary = 0;
}

/* create a new object with the given real and imaginary parts */
public Complex(double real_part, double imaginary_part) {
this.real = real_part;
this.imaginary = imaginary_part;
}
/* getter for real part */
public double getReal() {
return real;
}

/* setter for real part */
public void setReal(double real) {
this.real = real;
}

/* getter for imaginary part */
public double getImaginary() {
return imaginary;
}

/* setter for imaginary part */
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}

/* return a string representation of the invoking Complex object */
public String print() {
if (imaginary == 0) return real + "";
if (real == 0) return imaginary + "i";

if (imaginary < 0) {
return real + " - " + (-imaginary) + "i";
} else {
return real + " + " + imaginary + "i";
}
}

/* add two complex numbers */
public void addComplex(Complex b) {
this.real += b.real;
this.imaginary += b.imaginary;
}

/* subtract two complex numbers */
public void subtractComplex(Complex b) {
this.real += b.real;
this.imaginary += b.imaginary;
}

/* multiply two complex numbers */
public void multiplyComplex(Complex b) {

this.real = this.real * b.real - this.imaginary * b.imaginary;
this.imaginary = this.real * b.imaginary + this.imaginary * b.real;
}


}

/*ComplexRunner.java*/

Screenshot :

Complex.java

Screenshot of the output:

---------------Could you please leave a THUMBS Up for my work---------


Related Solutions

An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS...
write a C++ program to CREATE A CLASS EMPLOYEE WITH YOUR CHOICE OF ATTRIBUTES AND FUNCTIONS COVERING THE FOLLOWING POINTS: 1) COUNTING NUMBER OF OBJECTS CREATED ( NO OBJECT ARRAY TO BE USED) USING ROLE OF STATIC MEMBER 2) SHOWING THE VALID INVALID STATEMENTS IN CASE OF STATIC MEMBER WITH NON STATIC MEMBER FUNCTION, NON STATIC MEMBERS OF CLASS WITH STATIC MEMBER FUNCTION, BOTH STATIC. SHOW THE ERRORS WHERE STATEMENTS ARE INVALID. 3) CALL OF STATIC MEMBER FUNCTION, DECLARATION OF...
For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already...
For the first part of this lab, copy your working ArrayStringList code into the GenericArrayList class.(already in the code) Then, modify the class so that it can store any type someone asks for, instead of only Strings. You shouldn't have to change any of the actual logic in your class to accomplish this, only type declarations (i.e. the types of parameters, return types, etc.) Note: In doing so, you may end up needing to write something like this (where T...
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an...
Having some trouble with this Java Lab (Array.java) Objective: This lab is designed to create an array of variable length and insert unique numbers into it. The tasks in this lab include: Create and Initialize an integer array Create an add method to insert a unique number into the list Use the break command to exit a loop Create a toString method to display the elements of the array Task 1: Create a class called Array, which contains an integer...
For each of the following mathematical functions (or equations): (i) Take the first derivative dy/dx, (ii)...
For each of the following mathematical functions (or equations): (i) Take the first derivative dy/dx, (ii) Set dy/dx = 0, then solve for x . (iii) Take the second derivative d(dy/dx)/dx. Is the second derivative positive or negative at x*? Is this a relative minimum point or a relative maximum point? Or neither? 1) Y= 1500 X – (41,000,000 + 500 X + .0005 X2) 2) Y= 12,100,000 + 800X + .004 X2 X 3) Y=(1800-.006X)X 4) Y=1800X-.006X -(12,100,000+800X+.004X )...
Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
i just need the Polygon class Shapes In this lab you will create an interface, and...
i just need the Polygon class Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double area none computes the area of the shape double perimeter none computes the perimeter of the shape Point2d center none computes the center of the shape You will then create 3 implementations of this interface: Rectangle, Circle, and...
Physics Post Lab: Simple Machines 1) Which class of lever does the following belong to? a....
Physics Post Lab: Simple Machines 1) Which class of lever does the following belong to? a. Pliers       b. Bottle opener       c. Scissors       d. Fishing Rod       e. Wheel Barrow       f. Nail clipper     g. Stapler 2) Does the single pulley magnify force, multiply speed, or simply change the direction of the applied force? 3) Archimedes said, “Give me a place to stand, and I will move the world”. Discuss what you think he meant by that!...
Covered chapters: 1 ~ 3 In our first lab of the semester, we will create a...
Covered chapters: 1 ~ 3 In our first lab of the semester, we will create a Java CLI application that enables a store owner to calculate and out put the cost of a product. As the owner you determine the store name, product, cost, and hours it takes to make the product. Your output should display all pertinent information in a readable format. Lab Parameters Using Exercise 10 on page 165 of the textbook as a starting point, create an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT