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

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...
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 )...
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...
Put the ideas into practice by extending the original Room class (from lab 5) to create...
Put the ideas into practice by extending the original Room class (from lab 5) to create an AcademicRoom.java class. Room.java: import java.util.*; public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; }    public int compareTo(final Room o){ return Integer.compare(this.capacity, capacity); } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room...
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...
In this lab, you will explore some basics of Solidwork. 1. Create the numbers 0 –...
In this lab, you will explore some basics of Solidwork. 1. Create the numbers 0 – 9 with lines and arcs 2. Create a polygon with 5 sides and with 9 sides 3. Create two lines with the interior angle between the lines of 145º 4. Create a centerline inside a 4-sided polygon 5. Create a circle of 5 inches in diameter and extrude it for 12 inches in height. Type your name on the surface and extrude cut it...
Make quicksort functions to sort this class, first by ID then by athleteEvaluation athleteID: an unique...
Make quicksort functions to sort this class, first by ID then by athleteEvaluation athleteID: an unique identifier for the athlete athletePosition: an abbreviation of the expected player’s position on the team. We will use an enumerated data type to store the position within our BUAthlete class. A definition of the enumerated data type is provided below. Don't forget that enumerated data types are actually stored as integers. enum Position {OL, QB, RB, WR, TE, DL, DE, LB, CB, S, K};...
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT