Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code in Java please. Here are the instructions This is the Test class public class Test { public static void main(String[] args) { // (1)

Code in Java please.

image text in transcribedimage text in transcribed

Here are the instructions

image text in transcribedimage text in transcribed

This is the Test class

public class Test { public static void main(String[] args) { // (1) System.out.println(" ------(1)------"); Cvector v1 = new Cvector(3); v1.printCvector();

// (2) System.out.println(" ------(2)------"); String s = "1.5 2.1 3.1 -1.5 9.1 4"; Cvector v2 = new Cvector(s); v2.printCvector();

// (3) System.out.println(" ------(3)------"); double[] re = {2.1, 3.5, -1.01}; double[] im = {5.0, -1, 2.3}; Cvector v3 = new Cvector(re, im); v3.printCvector();

// (4) System.out.println(" ------(4)------"); Cvector v4 = v3.getVector(); v4.printCvector();

// (5) System.out.println(" ------(5)------"); System.out.println(v4.length());

// (6) System.out.println(" ------(6)------"); Cvector v6 = v2.add(v3); v6.printCvector();

// (7) System.out.println(" ------(7)------"); Cvector v7 = v2.mutiply(v3); v7.printCvector();

// (8) System.out.println(" ------(8)------"); double p = v7.pow(); System.out.printf("%.4f ", p);

// (9) System.out.println(" ------(9)------"); Cvector v9 = v7.conj(); v9.printCvector();

// (11) System.out.println(" ------(11)------"); Cvector v12 = Cvector.randomVector(5); v12.printCvector(); } }

This is the output of the program

------(1)------ (0.000, 0.000) (0.000, 0.000) (0.000, 0.000) ------(2)------ (1.500, 2.100) (3.100, -1.500) (9.100, 4.000) ------(3)------ (2.100, 5.000) (3.500, -1.000) (-1.010, 2.300) ------(4)------ (2.100, 5.000) (3.500, -1.000) (-1.010, 2.300) ------(5)------ 3 ------(6)------ (3.600, 7.100) (6.600, -2.500) (8.090, 6.300) ------(7)------ (-7.350, 11.910) (9.350, -8.350) (-18.391, 16.890) ------(8)------ 976.5166

------(9)------ (-7.350, -11.910) (9.350, 8.350) (-18.391, -16.890) ------(11)------ (0.617, 0.480) (0.704, 0.147) (0.615, 0.579) (0.307, 0.610) (0.257, 0.144)

This is the code so far

public class Complex { // encapsulated instance data fields private double real; private double imag; public Complex() { } public Complex(double real, double imag) { this.real = real; this.imag = imag; } public double getReal() { return real; } public double getImag() { return imag; } // the returned angle is in Randians public double getAngle() { return Math.atan(imag / real); } // the returned angle is in degrees public double getAngle2() { return getAngle() * 180. / Math.PI; } public double pow() { return real * real + imag * imag; } public double abs(){ return Math.sqrt(pow()); } public Complex conjugate() { return new Complex(real, -1 * imag); } public double getRadius() { return Math.sqrt(pow()); } public void setReal(double real) { this.real = real; } public void setImag(double imag) { this.imag = imag; } public Complex add(Complex in) { return new Complex(this.real + in.real, this.imag + in.imag); } public Complex multiply(Complex in) { Complex out = new Complex(); out.real = this.real * in.real - this.imag * in.imag; out.imag = this.real * in.imag + this.imag * in.real; return out; } public Complex divide(Complex in) { double tmpD = in.pow(); Complex tmpC = this.multiply(in.conjugate()); tmpC.setReal(tmpC.getReal() / tmpD); tmpC.setImag(tmpC.getImag() / tmpD); return tmpC; } @Override public boolean equals(Object in) { boolean eq = false; if (in == null) eq = false; else if (this.getClass() != in.getClass()) eq = false; else { Complex c = (Complex) in; if ((this.real == c.real) && (this.imag == c.imag)) eq = true; } return eq; } @Override public String toString() { return real + " " + imag; } public void printComplex() { System.out.printf("(%.3f, %.3f) ", real, imag); } public static Complex nextComplex() { return new Complex(Math.random(), Math.random()); } }

Task 2] Create the class Cvector. The class properties, constructors, and methods are described below.. Cvector -vector: Complex[] +Cvector(size: int) +Cvector(String s) +Cvector(re: double[], im: double[]) +length(): int +getVector(): Cvector +add(in: Cvector): Cvector add (in: double[]): Cvector +multiply(in: Cvector): Cvector +divide(in: Cvector): Cvector +pow(): double +abs(): double +conj(): Cvector +printCvector(): void +randomVector(size: int): Cvector (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13) (14) The data field vector is an array of objects from the class Complex. For example, Cvector v = new Cvector(5); Please keep in mind that v is a reference variable that refers to an object from the class Cvector. An object from the class Cvector contains the reference variable vector which refers to an array of objects from the class Complex. In the following and for the sake brevity, I will refer to vector as an array of Complex objects. (1) A constructor that takes a parameter of type integer. The constructor creates an array of objects from the class Complex referenced by the data field vector. The length of the array is provided as the parameter of the constructor. (2) This constructor takes a String object (a reference variable to an object from the class String). The string contains the real part followed by the imaginary part of each element of the array of Complex objects to be created. Numbers are separated by the space character. The constructor creates an array of Complex objects of appropriate length and parses the double values from the taken string to the created array of Complex objects. The created array is referenced by the data field vector. (3) This constructor creates an array of Complex objects referenced by the data field vector. The real and imaginary parts of the created array are provided in the parameter arrays re and im, respectively. (4) This method returns the data field vector. I suggest you return a reference to a copy rather than to the original data. This prevents alternating the private data field outside the class Cvector. (5) This method returns the number of elements in the array referenced by vector. (6) This method adds this Cvector object to the taken Cvector object and returns the sum as a Cvector object. You can use any of the methods of the class Complex. The operations are performed elementwise. (7) This method multiplies this Cvector object by the taken Cvector object and returns the product as a Cvector object. The multiplication is performed elementwise. (8) This method divides this Cvector object by the taken Cvector object and returns the result of division as a Cvector object. The division is performed elementwise. (9) This method divides this Cvector by the taken array of type double[]. Because the taken array does not contain an imaginary part, the real part of the returned Cvector object is the result of dividing the real part of this Cvector by the parameter array; the imaginary part of the returned Cvector object is the result of dividing the imaginary part of this Cvector by the parameter array. The division is performed elementwise. (10) This method returns the total power of this Cvector object. The total power of a vector is the sum of the power of all its elements. Use the method pow() from the class Complex to compute the power of an object from the class Complex. (11) This method returns the total absolute value of this Cvector object. The total absolute value of a vector is the sum of the absolute values of all its elements. Use the method abs() from the class Complex to compute the power of an object from the class Complex. (12) This method returns the conjugate of this Cvector object as a Cvector object. Refer to the provided code of the Complex class for more details on the conjugate operator. (13) This method displays the data fields of this Cvector object in a format of your choice. (14) This static method creates an instance from the class Cvector. The created instance contains an array of objects from the class Complex of length size (the parameter of the method). The elements of the created array are assigned randomly. You can use any method from the class Complex to assign random values to an object from the class Complex

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

using signal flow graph

Answered: 1 week ago