public class TankTest
{
    public static void main(String[] args)
    {
        Tank sherman = new Tank(15, 50, 50);
System.out.println("Initial Property Values:"); System.out.println("ammo = " + sherman.getAmmo()); System.out.println("x = " + sherman.getX()); System.out.println("y = " + sherman.getY()); sherman.setAmmo(20); sherman.setX(60); sherman.setY(70); sherman.shoot(); sherman.moveTo(20,30);
Tank.displayType(); System.out.println("Final Property Values:"); System.out.println("ammo = " + sherman.getAmmo()); System.out.println("x = " + sherman.getX()); System.out.println("y = " + sherman.getY()); } }
// Tank class

class Tank { // ********************************* constructors
// default constructor public Tank() { myAmmo = 10; myX = 0; myY = 0; } // "other" constructor public Tank(int ammo, int x, int y) { myAmmo = ammo; myX = x; myY = y; } // ********************************* accessor methods public int getAmmo() { return myAmmo; } public int getX() { return myX; } public int getY() { return myY; } // ********************************* modifier methods public void setAmmo(int ammo) { myAmmo = ammo; } public void setX(int x) { myX = x; } public void setY(int y) { myY = y; } // ********************************* "interesting" methods public void shoot() { myAmmo = myAmmo - 1; // could be myAmmo--; } public void moveTo(int newX, int newY) { myX = newX; myY = newY; }
public static void displayType() { System.out.println("I am a Tank"); } // ********************************* properties private int myAmmo; private int myX; private int myY; }