/* * file name: Agent.java * author: YOUR NAME HERE * date: * purpose: The abstract parent class of every agent in the simulation. * * CS231 Project 3 - Agent-Based Simulation * * Agent is ABSTRACT: you can never write new Agent(...). * It holds everything that ALL agents share (a position, a radius, whether * they moved) and it declares - without writing - the two behaviors that * each kind of agent must provide for itself: updateState() and draw(). * * Agent (abstract) * / \ * SocialAgent AntiSocialAgent (concrete: these can be created) */ import java.awt.Graphics; import java.util.Random; public abstract class Agent { // One random number generator shared by every Agent (used in Task 6). private static Random gen = new Random(); // ====================================================================== // TASK 2 - Fields // ====================================================================== // TODO (Task 2): declare the four fields that every Agent has. // // x, y double private Only Agent's own methods touch these. // Subclasses use getX()/setX() etc. // // radius int protected Subclasses read it directly in updateState(). // moved boolean protected Subclasses set it directly in updateState() // and read it in draw(). // // protected means: visible in this class AND in every subclass. // private means: visible ONLY in this class - even subclasses cannot see it. /** * Creates an Agent at (x0, y0) with the given neighbor radius. * A new Agent has not moved yet. */ public Agent(double x0, double y0, int radius) { // TODO (Task 2): set all four fields. } /** Returns the x position. */ public double getX() { // TODO (Task 2) return 0; } /** Returns the y position. */ public double getY() { // TODO (Task 2) return 0; } /** Returns the radius within which another Agent counts as a neighbor. */ public int getRadius() { // TODO (Task 2) return 0; } /** Returns true if this Agent moved during its most recent updateState(). */ public boolean getMoved() { // TODO (Task 2) return false; } /** Sets the x position. */ public void setX(double newX) { // TODO (Task 2) } /** Sets the y position. */ public void setY(double newY) { // TODO (Task 2) } /** Sets the radius. */ public void setRadius(int newRadius) { // TODO (Task 2) } /** * Returns the position with three decimal places, for example "(3.024, 4.245)". * * Hint: String.format("(%.3f, %.3f)", x, y) */ public String toString() { // TODO (Task 2) return ""; } // ====================================================================== // TASK 6 - Shared movement (protected helper method) // ====================================================================== /** * Moves this Agent by a random amount between -10 and 10 in x AND a * random amount between -10 and 10 in y, then keeps it inside the * Landscape: 0 <= x <= scape.getWidth() and 0 <= y <= scape.getHeight() * * Both kinds of agents move the same way; they only disagree about WHEN * to move. So the HOW is written once, here, and is protected so that * subclasses can call it. * * Hints: * gen.nextDouble() returns a value in [0, 1). How do you turn that into * a value in [-10, 10)? * Math.max(0, Math.min(limit, value)) "clamps" value into [0, limit]. */ protected void moveRandomly(Landscape scape) { // TODO (Task 6) } // ====================================================================== // Abstract methods - every concrete subclass MUST write these. // Notice: no body, just a semicolon. // ====================================================================== /** Decides whether this Agent moves this step, and moves it if so. */ public abstract void updateState(Landscape scape); /** Draws this Agent. */ public abstract void draw(Graphics g); }