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...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
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...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals...
................................................ ................................................ This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference. …………………………...…….. …………………………...……. Instructions LAB5 Instructions Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an   equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT