All pastes #1255041 Raw Edit

City.java

public java v1 · immutable
#1255041 ·published 2008-11-13 07:16 UTC
rendered paste body
// City program for ISM001 homework 2import java.util.Scanner;public class City {  //----------------------------  // Attributes of the city:  //----------------------------  private String name="My City";  private int population=500;  private int wealth=population;  //----------------------------  // Constructors  //----------------------------  public City( String name ){ this.name = name; }  public City( String name, int population ){     this(name);    this.population = population;  }  public City( String name, int population, int wealth ){    this(name, population);    this.wealth = wealth;  }  //----------------------------  // Get methods  //----------------------------  public String toString(){ return name; }  public int getPopulation(){ return population; }  public int getWealth(){ return wealth; }  //----------------------------  // Change the attributes  // (another form of set methods)  //----------------------------  // Add citizens to the city  // When n < 0, it will remove citizens.  // It will also guarantee that population will not be negative.  public void addPopulation(int n){    population += (population + n > 0)? n: (-population);  }  // Add n units of wealth to the city  public void addWealth(int n){ wealth += n; }    //----------------------------  // Check the attributes  //----------------------------  // Check if the city is empty  public boolean isEmpty(){ return (population<=0); }  // Check if there is any debt  public boolean isDebit(){ return (wealth<0); }  //----------------------------  // Events  //----------------------------  // Earthquake  public void earthquake(){    addPopulation( (int)(getPopulation() * -.8) );    addWealth(-1000);  }  // Trade with city c  public boolean tradeWith( City c ){    if(!isEmpty() && !c.isEmpty() && !c.isDebit()){      addWealth(500); addPopulation(100);      c.addWealth(500); c.addPopulation(100);      return true;    }    return false;  }  //----------------------------  // Other methods  //----------------------------  public void displayMessage(){    System.out.printf("%s (%d, %d)\n", toString(), getPopulation(), getWealth());  }  //--------------------------------------  // Main program to test the code  //--------------------------------------  public static void main( String args [] ){      City city[] = { new City("Hong Kong"), new City("Macau", 50), new City("Shanghai", 2000, 1000) };    // Initial    System.out.println("\nInitial:");    for( City c: city ) c.displayMessage();    // Events:    System.out.println("\nAfter some events:");    city[0].tradeWith(city[2]);    city[1].earthquake();    city[2].earthquake();    for( City c: city ) c.displayMessage();    // More changes:    System.out.println("\nAfter some more changes");    city[0].addPopulation(200);    city[1].addPopulation(-100);    city[2].addPopulation(300);    city[2].addWealth(-1400);    for( City c: city ) c.displayMessage();    for( City c: city ){      if( c.isEmpty() ) System.out.printf("There are no more citizens in %s!\n", c);      if( c.isDebit() ) System.out.printf("%s is in debt!\n", c);    }      } }