Solution:
double max(double a[][][])
{
double max = a[0][0][0];
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++)
if (max < a[i][j][k])
max = a[i][j][k];
return max;
}
2. In this question, we represent a point in the 2-dimensional plane as a double array that has exactly 2 elements: an x-coordinate and a y-coordinate. Write a method that receives 2 arguments:
x' = x*cos(theta) + y*sin(theta)void rotate(double p[], int degrees)
y' = -x*sin(theta) + y*cos(theta)
Solution:
p[1] = -p[0]*Math.sin(radians) + p[1]*Math.cos(radians);
p[0] = temp;
return;
}
3. The applet below produces a "target": 3 concentric circles, centered on pixel coordinates 150, 150. The inner circle is red and has a radius of 50 pixels. The yellow circle has a radius of 100 pixels, and the blue circle has a radius of 150 pixels.
When the mouse is released in the red circle,
add 150 points to int variable sum. When the mouse is clicked in
the yellow ring (the yellow circle, but not the red circle), add 100 points
to sum. When the mouse is clicked in the blue ring (the blue circle,
but not the yellow circle), add 50 points to sum. (Hint: recall, for a
right triangle, that the sum of the squares of the sides equals the square
of the hypoteneuse.)
public class target extends Applet implements
MouseListener
{
int sum = 0, x, y;
public void init()
{
addMouseListener(this);
}
public void paint( Graphics
g)
{
g.setColor(Color.blue);
g.fillOval(50, 50, 300, 300);
g.setColor(Color.yellow);
g.fillOval(100, 100, 200, 200);
g.setColor(Color.red);
g.fillOval(150, 150, 100, 100);
}
public void mousePressed(MouseEvent
e) { }
public void mouseClicked(MouseEvent
e) { }
public void mouseReleased(MouseEvent
e)
{
x = e.getX();
y = e.getY();
repaint();
}
public void mouseEntered(MouseEvent
e) { }
public void mouseExited(MouseEvent
e) { }
}
Solution:I said during the test that you can imagine putting your code in the paint method. You also could have put the code in the mouseReleased method, as is done below.
The question says that the circles are centered at pixel coordinates 150, 150. The code below has the circles centered on 200, 200. Either center ( 150, 150 or 200, 200 ) is fine.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;public class target extends Applet implements MouseListener
{
int sum = 0, x, y;public void init()
{
addMouseListener(this);
}public void paint( Graphics g)
{
g.setColor(Color.blue);
g.fillOval(50, 50, 300, 300);
g.setColor(Color.yellow);
g.fillOval(100, 100, 200, 200);
g.setColor(Color.red);
g.fillOval(150, 150, 100, 100);
}public void mousePressed(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e)
{
int dist; // the square of the distance from the centerx = e.getX();
y = e.getY();
dist = (200 - x)*(200 - x) + (200 - y)*(200 - y);
if (dist < 50*50)
sum += 150;
else if (dist < 100*100)
sum += 100;
else if (dist < 150*150)
sum += 50;
repaint();
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
4. Characterize a square in the 2-dimensional plane by its lower left corner and its edge length. For example, the square whose 4 corners are (-1, -1), (1, -1), (-1, 1), (1, 1) would be represented by its lower left corner, (-1, -1), and its edge length of 2. For this question, represent the lower left corner with 2 doubles (xLL, yLL), and the edge length with another double:
double xLL = -1.0, yLL = -1.0, edge = 2.0;
This square in the 2-dimensional plane is "displayed"on the applet's graphic object as a 100 x 100 pixel square whose upper left corner has pixel coordinates (0,0).
The mouse release coordinates (x, y) are in applet int variables x and y.
int x,y; // mouse coordinates in pixels
We say that the mouse coordinates refer to the corresponding point in the 2-dimensional plane. In the example above, if mouse coordinates (x,y) were pixel coordinates (0,0) (i.e., the upper left corner of the square), then they would refer to 2-dimensional plane coordinates (-1, 1).
Give Java statements that compute the lower left corner of the square in the 2-dimensional plane (i.e., compute the values of xLL and yLL) whose edge length is the same as the current square, but whose center is the point in the 2-dimensional plane referred to by the mouse coordinates. In the example above, the new lower left point is (-2, 0) (i.e., xLL is -2.0 and yLL is 0.0). See Powerpoint slide. Assume that the mouse coordinates are within the applet square.