OO everything is an instance of something
public class Person {
// Declaration of instance variables for
// the Person class is shown below in green.
// They are also the attributes of the Person class.
public String name;
public int age;
public int height;
public String sex;
// Methods of Person are given below
public void drawPerson(Graphics g, int x, int y) {
/* Write code to draw the person at position (x, y) in the graphics element g.
*/
}
public double grow(double increment) {
/* Write code to grow the person
This should increase the height of the Person
*/
}
}
//filename: Person.java
import java.awt.*;
import java.applet.Applet;
public class Person {
// Attributes of Person as shown below
public String name;
public int age;
public int height = 80;
public String sex;
public void drawPerson (Graphics g, int x, int y) {
// draws the person, anchored at (x,y)
// draw head
g.drawOval(x, y, 20, 30);
g.drawArc(x+12, y+6, 6, 9, 200, 135);
g.drawArc(x+3, y+6, 6, 9, 200,135);
g.drawOval(x+7, y+20, 6, 5);
//draw body
g.drawLine(x+ 8, y + 30, x+ 8, y + height);
g.drawLine(x+12, y + 30, x+12, y + height);
//draw arms
g.drawLine(x + 8, y + 30, x, y + 40);
g.drawLine(x, y + 40, x + 8, y + 50);
g.drawLine(x + 12, y + 30, x + 20, y + 40);
g.drawLine(x + 20, y + 40, x + 12, y + 50);
//draw feet
g.fillRect(x, y + height, 10, 3);
g.fillRect(x+11, y + height, 10, 3);
}
}
//filename: PersonApplet.java
import java.awt.*;
import java.applet.Applet;
public class PersonApplet extends Applet {
public Person joe; // Joe is an instance variable.
public void init() {
/* init() is called only once.
This is where the applet is initialized.*/
joe = new Person(); // Create a new object instance for Joe Hamilton.
}
public void paint ( Graphics g ) {
joe.drawPerson(g, 20, 15); // Draw Joe on the applet.
}
}