User Tools

Site Tools


lang:judo:english:tutorial-en-012

[ Intro | Begin | if/else | Loops | Arrays | Graphics | Animation | Mouse | Game | Real | Methods | Class | Class 2 | Applet | MouseClick | Thread | Button ]


Java Classes

A Java class is a reusable entity. It is self-contained and may be thought of as an object like a fish or a rock or book. In a Space Invaders game you might have an alien class, a defender class, a bullet class, a bomb class and a game class that controls the whole thing. If I wanted to write another game that had baddies, i might be able to reuse the aliens and bullet classes. classes can be thought of as modular. They are connected together through methods. Let's make an example to show you:

    public class Employee{
	private int empNum;
 
	public void setEmpNum(int _x) {
		bulletx = _x;
	}
	public int getEmpNum() {
		return empNum;
	}
    }

A class needs an access modifier, the word “class” to signal what it is, and a unique name. Our Employee class has only one variable - its employee number. It is private so that it cant be hacked into from outside. The only access to it is through the set and get methods. The set method(setEmpNum) allows other classes to access the Employee class and change the empNum variable. The get method(getEmpNum) allows access to what the value of it is.

Say you had an managing class called Accountant. It might be something like this:

    public class Accountant {
	public static void main(String[] args) {
	    // create an instance of class Employee called steve
	    Employee steve = new Employee();
 
	    steve.setEmpNum(234);
	    System.out.println("Steve: Employee Number:" + steve.getEmpNum());
	}
    }

But what is this line:

Employee steve = new Employee();

What is that? What we have done in this line is to INSTANTIATE the Employee class. We have made a clone of the Employee class and named it steve.

We use the keyword new to allocate memory for it and to make a new copy of that class.

Employee() is the CONSTRUCTOR. I allows us to create copies.

Later on we will write our own constructor methods, but for now we will use the default inbuilt constructor.

Lets write our own Alien class for our game. What do you think it will need?

A position (x and y), and a boolean flag to tell when it has been hit. Let us have a go at it:

    public class Baddie {
	// declare our variables
	private int xpos;
	private int ypos;
	private Boolean isHit;
 
	// declare our methods
	public void setXpos(_x) {
	    xpos = _x;
	}
	public void setYpos(int _y) {
	    ypos =_y;
	}
	public void setHit(boolean hitFlag) {
	    isHit = hitFlag;
	}
	public int getXpos() {
	    return xpos;
	}
	public int getYpos() {
	    return ypos;
	}
	public boolean getIsHit() {
	    return isHit;
	}
    }

So how are we going to use this class? Make a Game class that controls our baddies. Try something like so:

    public class Game {
	public static void main(String[] args) {
	    String aliveOrDead;
 
	    // instantiate a baddie
	    Baddie pugwort = new Baddie();
	    pugwort.setXpos(50);
	    pugwort.setYpos(100);
	    pugwort.setHit(true);
	    if (pugwort.getIsHit == true) {
		aliveOrDead = "Dead";
	    } else {
	        aliveOrDead = "Alive";
	    }
	    System.out.println("pugwort is at " + pugwort.getXpos() +
		": " + pugwort.getYpos() + " and he is " + aliveOrDead);
	}
    }

What did you get? Where was he? Was he dead or alive? What did we do in Game class?

  • Set up a variable to output whether the baddie was dead or not
  • Instantiated a new Baddie named pugwort
  • We called methods of Baddie through our created Baddie, pugwort
  • set the x:y position to 50:100
  • We killed the baddie by setting its isHit flag to true
  • Did an if check to see whether isHit was true or false and according to the answer, set our output variable, aliveOrDead, accordingly
  • Printed out the position of pugwort and whether it was dead or not

Java Class Constructors

A constructor is something like a method in that has Parentheses. It may be thought of as a method that constructs a class object. You can write your own constructor methods but if you don't Java writes one for you automatically. Called the default constructor. Any constructor you write must have the same name as the class and cannot have a return type.

It is usually called in the form of:

    Object anotherObject = new Object();
    // or
    ClassName instantiatedName = new ClassName();
    // e.g.
    Defender superDork = new Defender();

Lets make a java class constructor for our Employee class. It will have another variable which will be the same for all employees - the salary. Let's do it:

    public class Employee {
	private int empNum;
	private double salary;
 
	// overwrite our default constructor
	Employee() {
		salary = 300.00;
	}
 
	// access methods
	public void setEmpNum(int _x) {
		bulletx = _x;
	}
	public int getEmpNum() {
		return empNum;
	}
    }

Any employee instantiated will have a default salary of $300.00.

Exercise

Create a Circle class with attributes of radius, area and diameter. Use a constructor that sets the radius to 1.

Write methods such as setRadius, getRadius, computeArea and computeDiameter. Hints:

Diameter = 2 * radius
area = PI * Radius * Radius

Create another class called TestCircle and instantiate 3 Circles. Use the get and set methods for Circle and compute the diameter and area of each circle and display those statistics.

Next lesson we will get more into writing constructors and delving deeper into the murky depths of classes.

lang/judo/english/tutorial-en-012.txt · Last modified: 2007/02/08 09:21 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki