|





| |
- Draw a diagram showing the name of your class. Think of what it
should know and what it should be able to do.
Examples:
| Paycheck |
Know:
 | Hours worked |
 | Pay rate |
 | Payee |
 | Date |
|
Do:
 | Calculate gross pay |
 | Print itself* |
|
| Monster |
Know:
 | x, y coordinates |
 | size |
 | eye color |
 | body color
|
|
Do:
 | run |
 | jump |
 | growl |
 | become angry |
 | display itself* |
|
*Objects are responsible for displaying or printing themselves.
Use Textpad to create your class
import java.awt.* // awt needed because monster must display self
Public Class Monster { //Monster is the Class name, taken from the
}
//top of your diagram.
Save ClassName.java
Create instance variables to hold what objects instantiated from
your Class will know. Think carefully about the data type for each
variable. These variables form Class' attributes. Attributes
must be protected from other programmers. Set the security to
private. Do not set the value of your
attributes when you declare them. Example:
private int xCoord;
private int yCoord;
private int size; //in pixels
private Color eyeColor; //data type of color variable is the Color class.
private Color bodyColor;
Create blank methods for what your Class will know. Think carefully
about whether the method should be void or will return a value.
public void run() {
}
public void jump() {
}
public void growl() {
}
public void becomeAngry() {
}
public void display() {
}
Create a special Constructor method that is used to set the initial values of
the attributes. Constructor methods don't specify the security or the
datatype and must have the same name as the class. Think carefully about
what values you will allow users of your class to specify. You'll need to
include these values as parameters. Example:
Monster(int desired XCoord, int desired YCoord,
int desiredSize) {
xCoord = desiredXCoord;
yCoord = desiredYCoord;
size = desiredSize;
eyeColor = Color.red; // I decided not to allow the
user to set the colors
bodyColor = Color.black;
}
Implement you class' methods by writing the code for them.
Where necessary, specify any parameters the user will need to pass to your
method for it to work. Wherever possible, use your class'
attributes. Example of implementing the display method:
public void display(Graphics g) {
// BODY
g.setColor(bodyColor);
int monsterWidth = size;
int monsterHeight = size * 2 / 3;
g.fillOval(x, y, monsterWidth, monsterHeight);
// EYES
g.setColor(eyeColor);
int eyeWidth = monsterWidth / 10;
int eyeHeight = eyeWidth * 2 / 3;
int eye1X = x + monsterWidth / 6;
int eye1Y = y + monsterHeight / 3;
g.fillOval( eye1X, eye1Y, eyeWidth, eyeHeight );
int eye2X = eye1X + monsterWidth / 2;
int eye2Y = eye1Y;
g.fillOval( eye2X, eye2Y, eyeWidth, eyeHeight );
}
Compile your Class. You CAN'T USE YOUR CLASS IN
ANOTHER PROGRAM UNLESS YOU COMPILE IT FIRST. If you make changes to
your class, you will need to compile it again.
Save your class and open a second copy of textpad. Create
an Applet program to instantiate an object from your class and make it do
things. Example:
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public Class MonsterController extends Applet{
Monster fred; //declare variable to
hold the object you will instantiate
public void init() {
/* new instantiates (creates)
the object from your class pattern.
At the time
your object is instantiated, java runs it's constructor
method,
passing it the parameters in parentheses. */
fred = new Monster(50, 50, 75);
//fred now exists and you can execute
his methods, making him do tricks
}
public void paint(Graphics g) {
fred.display(g); // displays fred
fred.growl(); // makes fred growl
}
}
Save your Applet in the same folder as your Class. Compile
your Applet. Run your Applet (NOT your class) and admire it. If it
doesn't work, you may need to make changes to either your Class or your Applet
or both.
|