![]() |
Write an applet that displays a picture of a
stoplight. The stoplight should be centered within the applet and
the size of each of its lights should be one-fourth that of the
applet's standard size 400x400. The top circle should be red, the
center circle should be yellow, and the bottom circle should be
green. Finally, a white rectangle should surround the three circles
exactly. |
public class Applet extends GP.Containers.Applet
{
public Applet ()
{
GP.Shapes.Rectangle box = new GP.Shapes.Rectangle(); // must be created first!
GP.Shapes.Oval red = new GP.Shapes.Oval();
GP.Shapes.Oval yellow = new GP.Shapes.Oval();
GP.Shapes.Oval green = new GP.Shapes.Oval();
GP.Attributes.Coordinate center = new GP.Attributes.Coordinate(200, 200);
box.SetPosition(center);
yellow.SetPosition(center);
center = new GP.Attributes.Coordinate(200, 100);
red.SetPosition(center);
center = new GP.Attributes.Coordinate(200, 300);
green.SetPosition(center);
GP.Attributes.Dimension size = new GP.Attributes.Dimension(100, 300);
box.SetSize(size);
size = new GP.Attributes.Dimension(100, 100);
red.SetSize(size);
yellow.SetSize(size);
green.SetSize(size);
box.SetColor(new GP.Attributes.Colors.White());
red.SetColor(new GP.Attributes.Colors.Red());
yellow.SetColor(new GP.Attributes.Colors.Yellow());
green.SetColor(new GP.Attributes.Colors.Green());
}
}
![]() |
Write an applet that displays a pie chart showing the percentages of
students of each class taking this course. The pie chart should be centered within the applet and
its size should be that of the
applet's standard size 400x400. The pie should be divided
according to the following percentages:
The individual wedges should be colored accordingly:
|
public class Applet extends GP.Containers.Applet { public Applet () { GP.Shapes.Pie firstyears = new GP.Shapes.Pie(); GP.Shapes.Pie sophomores = new GP.Shapes.Pie(); GP.Shapes.Pie juniors = new GP.Shapes.Pie(); GP.Shapes.Pie seniors = new GP.Shapes.Pie(); GP.Attributes.Coordinate center = new GP.Attributes.Coordinate(200, 200); firstyears.SetPosition(center); sophomores.SetPosition(center); juniors.SetPosition(center); seniors.SetPosition(center); GP.Attributes.Dimension size = new GP.Attributes.Dimension(400, 400); firstyears.SetSize(size); sophomores.SetSize(size); juniors.SetSize(size); seniors.SetSize(size); firstyears.SetColor(new GP.Attributes.Colors.Red()); sophomores.SetColor(new GP.Attributes.Colors.Blue()); juniors.SetColor(new GP.Attributes.Colors.Violet()); seniors.SetColor(new GP.Attributes.Colors.Green());// and now for the interesting part :) double start = 0; double end = 0.4 * 360; firstyears.SetStartAngle(new GP.Attributes.Angle(start)); firstyears.SetEndAngle(new GP.Attributes.Angle(end)); start = end; end = start + 0.1 * 360; sophomores.SetStartAngle(new GP.Attributes.Angle(start)); sophomores.SetEndAngle(new GP.Attributes.Angle(end)); start = end; end = start + 0.1 * 360; juniors.SetStartAngle(new GP.Attributes.Angle(start)); juniors.SetEndAngle(new GP.Attributes.Angle(end)); start = end; end = start + 0.4 * 359; seniors.SetStartAngle(new GP.Attributes.Angle(start)); seniors.SetEndAngle(new GP.Attributes.Angle(end)); } }
BMI = (weight in kilograms) / (height in meters)2
An index of 27.8 or greater for men or 27.3 or greater for non-pregnant women is considered obese. Write java code that, given height and weight in metric units, determines the BMI.
public Applet () { double weightKG = 100.0; double heightM = 2.1;// enter Java code here to calculate BMI double BMI = weightKG / (heightM * heightM); }
Now write your code such that the height and weight can be calculated given American units. Note that one meter is 39.37 inches, one inch is 2.54 centimeters, one kilogram is 2.2 pounds, and one pound is 454 grams.
public Applet () { double weightLB = 220.0; double heightF = 6.1;// enter Java code here to calculate BMI double weightKG = weightLB / 2.2; double heightM = (heightF * 12) / 39.37; double BMI = weightKG / (heightM * heightM); }