2.
public class Complex5. Write a method, implies, that takes 2 boolean arguments, and returns true if either the first argument is false or the second argument is true, and returns false otherwise.
{
private double real, // the real part
imag; // the imaginarypublic Complex(double r, double i)
{
this.real = r;
this.imag = i;
}
public Complex(Complex c)
{
this.real = c.real;
this.imag = c.imag;
}
public double getReal() { return this.real; }public double getImag() { return this.imag; }
public Complex add(Complex c)
{
this.real += c.real;
this.imag += c.imag;
return this;
}
public Complex multiply(Complex c)
{
double temp = this.real*c.real - this.imag*c.imag;this.imag = this.real*c.imag + this.imag*c.real;
this.real = temp;
return this;
}
public double sizeSquared()
{
return this.real*this.real + this.imag*this.imag;
}
}
6. Write a Java expression whose value is a member of the following set, randomly selected each time it is executed: {-2, -1, 0, 1, 2}. That is, the first time the expression is evaluated, it might evaluate to 0; the second time, it might evaluate to -2, etc.
7. Write a method:
void stringProduct(String a[], String b[], String c[])that uses System.out.println to produce n output lines, where
n = the number of elements in a X the number of elements in b X the number of elements in c.For example, if a = { "0", "1" }, b = { "a", "b" }, and c = { "Lysander", "Spooner", "Jr" }, then the method would output the following 12 strings:
0aLysander8.
0aSpooner
0aJr
0bLysander
0bSpooner
0bJr
1aLysander
1aSpooner
1aJr
1bLysander
1bSpooner
1bJr
import java.awt.*;public class Bug {
final static int MINSIZE = 20,
MAXSIZE = 40,
MOVEMAX = 10;
// instance variables
protected int x, y, // initial upper left corner
e, // edge length
dx, dy; // motion vectorpublic Bug(int fieldSize) {
x = (int) (Math.random()*(fieldSize - MAXSIZE));
y = (int) (Math.random()*(fieldSize - MAXSIZE));
e = (int) (Math.random()*(MAXSIZE - MINSIZE + 1)) + MINSIZE;
dx = (int) (Math.random()*(2*MOVEMAX + 1)) - MOVEMAX - 1;
dy = (int) (Math.random()*(2*MOVEMAX + 1)) - MOVEMAX - 1;
}
public void setDx(int D) { dx = D; }public void setDy(int D) { dy = D; }
public void draw(Graphics g) { }
public void move() {
x += dx;
y += dy;
}
}
10. Define the dot product of 2 vectors, a = (a0, a1, a2, a3, ..., an) and b = (b0, b1, b2, b3, ..., bm) as a0*b0 + a1*b1 + a2*b2 + ... + aK*bK, where K is equal to the smaller of n and m. Using the Complex class defined in question 4, define a method, dotproduct, that takes 2 arrays of Complex as an arguments, and returns their dot product without altering either argument array.