Solution:
public boolean in(int
a[][][][], int x)
{
for (int i = 0; i < a.length; i++)
for (int j = 0; j < a[i].length; j++)
for (int k = 0; k < a[i][j].length; k++)
for (int l = 0; l < a[i][j][k].length; l++)
if (a[i][j][k][l] == x)
return true;
return false;
}
2.
Solution:
Solution:
4. Remove all unnecessary uses of this from the Complex class below.
public class ComplexSolution:
{
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;
}
}
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)
{
real = r;
imag = i;
}
public Complex(Complex c)
{
real = c.real;
imag = c.imag;
}
public double getReal() { return real; }public double getImag() { return imag; }
public Complex add(Complex c)
{
real += c.real;
imag += c.imag;
return this;
}
public Complex multiply(Complex c)
{
double temp = real*c.real - imag*c.imag;imag = real*c.imag + imag*c.real;
real = temp;
return this;
}
public double sizeSquared()
{
return real*real + imag*imag;
}
}
Solution:
public boolean implies(boolean x, boolean y) { return !x || y; }
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.
Solution:
(int) (5*Math.random()) - 2
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
1bJrSolution:public void stringProduct(String a[], String b[], String c[])
{
for (int i = 0; i < a.length; i++)
for (int j = 0; j < b.length; j++)
for (int k = 0; k < c.length; k++)
System.out.println(a[i] + b[j] + c[k]);
}
Solution:
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;
}
}
Solution:
public class Box extends Bug
{
private Color color;
public Box(int fieldSize)
{
super(fieldSize);
int i = (int) (2*Math.random());
color = Color.red;
if (i == 1)
color = Color.blue;
}
public void draw(Graphics
g)
{
g.setColor(color);
g.fillRect(x, y, e, e);
}
}
import java.awt.*;
public class Circle extends Bug
{
private Color color;
public Circle(int fieldSize)
{
super(fieldSize);
int i = (int) (2*Math.random());
color = Color.red;
if (i == 1)
color = Color.blue;
}
public void draw(Graphics
g)
{
g.setColor(color);
g.fillOval(x, y, e, e);
}
}
Solution:
Complex dotproduct(Complex
a[], Complex b[])
{
int k = (int) Math.min(a.length, b.length);
Complex product, sum = new Complex(0,0);
for (int i = 0; i < k; i++)
{
product = new Complex(a[i]); // copy construct a[i]
product.multiply(b[i]); // product
= a[i]*b[i]
sum.add(product);
// sum += product
}
return sum;
}