All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class JBMSTours.serializabletypes.City

java.lang.Object
   |
   +----JBMSTours.serializabletypes.City

public class City
extends Object
implements Externalizable, WeatherSpot
Look at the Source.

The City class includes information about the city, the country, the language, average monthly temperature, airport and alternate airport, Location, and Time Zone. It includes methods for calculating the distance between cities and time differences between cities.

This class is the richest data type in JBMSTours.

This class is a good example of how to use Externalizable instead of Serializable for storage and retrieval efficiency.

Beginning with the 3.0 release, this class is ORDERABLE (because it supplies the compareTo method). It also supplies the equals and hashCode methods for correct = and <> comparisons.


Variable Index

 o averageTemperature
array to store the average monthly temperature
 o city_id
identifier for the city
 o country
the country the city is located in
 o countryLong
the long name of the country, initialized to an empty string
 o language
the predominant language spoken in the city
 o measurementSystem
default system: metric or non-metric
 o name
name of city

Constructor Index

 o City()
No-arg constructor.
 o City(int, String, String, String, String)
Constructs a City object

Method Index

 o airportInCity(String)
Returns a boolean indicating whether the airport belongs to the city object.
 o compareTo(Object)
This method allows a City object to be orderable within SQL-J statements.

We have arbitrarily decided to order cities primarily by their Locations, and secondarily by their time zones.

 o equals(Object)
Good Cloudscape practice requires that an object override equals and hashCode for correct implementation of = and <> in queries.
 o findCity(Connection, int)
Queries the City table to return the appropriate city object based on the city_id.
 o findCity(Connection, String)
Method that queries the City table.
 o findCity(Connection, String, String)
Method that queries the City table to return a City object.
 o getAirport()
Returns code for city's main airport.
 o getCountryCode()
Returns country code.
 o getDisplayCountry()
Returns the full country name.
 o getDistanceFrom(City)
Calculates the distance between current city and another city.
 o getDistanceFrom(City, int)
Calculates the distance between current city and another city and converts units to the appropriate measurement system.
 o getDistanceFrom(Connection, int)
Calculates the distance between current city and another city.
 o getDistanceFrom(Connection, String)
Calculates the distance between current city and another city.
 o getDistanceFromHomeCityAsString(City)
Calculates the distance between current city and home city using measurement system of home city, and converts units to kilometers if necessary
 o getEastWestOrderableLocation()
Returns a double latitude value.
 o getInfo(Date, City)
Print city information to the output stream.
 o getLanguage()
Returns language.
 o getLocation()
Returns the city's Location.
 o getLocationAsString()
Returns the city's Location's toString() value.
 o getName()
Returns city name.
 o getNorthSouthOrderableLocation()
Returns a double latitude value.
 o getTimeDifference(Connection, Date, String)
Calculates the time zone difference between current city and another city for date specified.
 o getTimeDifference(Date, City)
Calculates the time zone difference between current city and another city for date specified.
 o getTimeDifferenceAsString(Date, City)
Calculates the time zone difference between current city and another city for date specified and returns a String.
 o getTimeZone()
Returns the city's TimeZone object.
 o getTimeZoneAsString()
Returns the city's TimeZone's toString() value.
 o hashCode()
 o isTropical()
Returns true if the city lies within the tropics.
 o prepareStaticStatements(Connection, boolean)
Prepares some or all of the PreparedStatements.
 o readExternal(ObjectInput)
Externalizable interface Read this object from ObjectOutput stream
 o setDisplayCountry(Connection, PreparedStatement)
sets the full country name (looks it up in a table based on the code).
 o setLocation(int, int, String, int, int, String)
Sets the Location object for the city.
 o setTemperatures(double, double, double, double)
For those cities for which only January, April, July, and October average temperatures are available, interpolates the temperatures for the missing months to set the values in the temperature array.
 o setTemperatures(double, double, double, double, double, double, double, double, double, double, double, double)
Sets the array of average monthly temperatures (Farenheit) for the city.
 o setTemperatures(double, double, double, double, double, double, double, double, double, double, double, double, String)
if you want to specify the scale (farenheit or celsius) you are using when setting the temperature array, use this method.
 o setTimeZone(double)
Sets the TimeZone object for the city.
 o setTimeZone(double, double, String, String)
Sets the TimeZone object for the city.
 o showTemperature()
shows the city's average temperature for the current month, using Farenheit.
 o showTemperature(Date, City)
Displays the average temperature for the city for the month of the date specified.
 o showTemperatureAsString(Date, City)
Returns the average temperature for the city for the month of the date specified as a String The group's home city determines whether they want to see the temperature as farenheit ("F") or as Celsius ("C").
 o showTheTemperature(Date, String)
Returns the average temperature for the city for the month of the date specified.
 o toString()
Get a String representation of this object
 o writeExternal(ObjectOutput)
Externalizable interface Write this object to ObjectOutput stream

Variables

 o city_id
 public int city_id
identifier for the city

 o name
 public String name
name of city

 o language
 public String language
the predominant language spoken in the city

 o country
 public String country
the country the city is located in

 o countryLong
 public String countryLong
the long name of the country, initialized to an empty string

 o averageTemperature
 public double averageTemperature[]
array to store the average monthly temperature

 o measurementSystem
 public String measurementSystem
default system: metric or non-metric

Constructors

 o City
 public City()
No-arg constructor. Default city is San Francisco.

 o City
 public City(int id,
             String theName,
             String theLanguage,
             String countryCode,
             String ap)
Constructs a City object

Parameters:
id - the city id
theName - the Name of the City
theLanguage - lower-case two character code for language
countryCode - upper-case two-character ISO code
ap - three-letter code for the airport

Methods

 o toString
 public String toString()
Get a String representation of this object

Returns:
String name of city
Overrides:
toString in class Object
 o setTemperatures
 public void setTemperatures(double Jan,
                             double Feb,
                             double Mar,
                             double Apr,
                             double May,
                             double Jun,
                             double Jul,
                             double Aug,
                             double Sep,
                             double Oct,
                             double Nov,
                             double Dec)
Sets the array of average monthly temperatures (Farenheit) for the city.

Parameters:
Jan - average monthly temperature for January.
Feb - average monthly temperature for February.
Nar - average monthly temperature for March.
Apr - average monthly temperature for April.
May - average monthly temperature for May.
Jun - average monthly temperature for June.
Jul - average monthly temperature for July.
Aug - average monthly temperature for August.
Sep - average monthly temperature for Sept.
Oct - average monthly temperature for October.
Nov - average monthly temperature for November.
Dec - average monthly temperature for December.
 o setTemperatures
 public void setTemperatures(double Jan,
                             double Apr,
                             double Jul,
                             double Oct)
For those cities for which only January, April, July, and October average temperatures are available, interpolates the temperatures for the missing months to set the values in the temperature array.

Parameters:
Jan - average monthly temperature for January.
Apr - average monthly temperature for April.
Jul - average monthly temperature for July.
Oct - average monthly temperature for October.
 o setTemperatures
 public void setTemperatures(double Jan,
                             double Feb,
                             double Mar,
                             double Apr,
                             double May,
                             double Jun,
                             double Jul,
                             double Aug,
                             double Sep,
                             double Oct,
                             double Nov,
                             double Dec,
                             String scale)
if you want to specify the scale (farenheit or celsius) you are using when setting the temperature array, use this method.

Parameters:
Jan - average monthly temperature for January.
Feb - average monthly temperature for February.
Nar - average monthly temperature for March.
Apr - average monthly temperature for April.
May - average monthly temperature for May.
Jun - average monthly temperature for June.
Jul - average monthly temperature for July.
Aug - average monthly temperature for August.
Sep - average monthly temperature for Sept.
Oct - average monthly temperature for October.
Nov - average monthly temperature for November.
Dec - average monthly temperature for December.
scale - "F" or "C" (for Farenheit or Celsius).
 o showTheTemperature
 public double showTheTemperature(Date adate,
                                  String scale)
Returns the average temperature for the city for the month of the date specified. You also specify whether you want to see the temperature as farenheit ("F") or as Celsius ("C").

Parameters:
adate - Date you are inquiring about.
scale - "F" or "C"
 o showTemperature
 public double showTemperature(Date stayDate,
                               City homeCity)
Displays the average temperature for the city for the month of the date specified. The group's home city determines whether they want to see the temperature as farenheit ("F") or as Celsius ("C").

Parameters:
Dec - Date you are inquiring about.
homeCity - the group's home city/
 o showTemperatureAsString
 public String showTemperatureAsString(Date stayDate,
                                       City homeCity)
Returns the average temperature for the city for the month of the date specified as a String The group's home city determines whether they want to see the temperature as farenheit ("F") or as Celsius ("C").

Parameters:
Dec - Date you are inquiring about.
homeCity - the group's home city/
 o showTemperature
 public double showTemperature()
shows the city's average temperature for the current month, using Farenheit.

 o getName
 public String getName()
Returns city name.

 o getDisplayCountry
 public String getDisplayCountry()
Returns the full country name.

Returns:
String full country name.
 o setDisplayCountry
 public void setDisplayCountry(Connection conn,
                               PreparedStatement pstmt) throws SQLException
sets the full country name (looks it up in a table based on the code). called by InsertCities.

Parameters:
conn - Connection.
pstmt - a prepared statement (prepared in InsertCities).
Throws: SQLException
a error occurred.
 o getLanguage
 public String getLanguage()
Returns language.

 o findCity
 public static City findCity(Connection conn,
                             String acity,
                             String acountry) throws SQLException
Method that queries the City table to return a City object.

SELECT city FROM Cities WHERE city.getName() = ? and city.getDisplayCountry() = ?

server-side method getDisplayCountry is used instead of a join.

Parameters:
conn - Connection
acity - Cityname (in english)
acountry - Countryname (in english)
Throws: SQLException
a error occurred.
See Also:
getDisplayCountry
 o findCity
 public static City findCity(Connection conn,
                             int cityId) throws SQLException
Queries the City table to return the appropriate city object based on the city_id.

SELECT city FROM Cities WHERE city_id = ?

Parameters:
conn - Connection
cityId - City ID
Returns:
City object
Throws: SQLException
Thrown if there is a SQL error.
 o findCity
 public static City findCity(Connection conn,
                             String ap) throws SQLException
Method that queries the City table.

SELECT city FROM Cities WHERE city.getAirport() = ?

Parameters:
conn - Connection
ap - airport code
Returns:
City object
Throws: SQLException
a error occurred.
 o getCountryCode
 public String getCountryCode()
Returns country code.

Returns:
String country
 o getAirport
 public String getAirport()
Returns code for city's main airport.

 o airportInCity
 public boolean airportInCity(String ap)
Returns a boolean indicating whether the airport belongs to the city object.

Returns:
boolean airport
 o getInfo
 public String getInfo(Date adate,
                       City homeCity)
Print city information to the output stream.

Parameters:
adate - date of visit to the city
homeCity - the visitor's home city
Returns:
String object information about the city
 o setLocation
 public void setLocation(int latDeg,
                         int latMin,
                         String latDir,
                         int longDeg,
                         int longMin,
                         String longDir)
Sets the Location object for the city.

Parameters:
latDeg - degrees latitude
latMin - minutes latitude
latDir - "N" or "S" for latitude
longDeg - degrees longitude
longMin - minutes longitude
longDir - "E" or "W" for latitude
 o getLocation
 public Location getLocation()
Returns the city's Location.

Returns:
Location cityLocation
 o getLocationAsString
 public String getLocationAsString()
Returns the city's Location's toString() value. For use in Cloudview.

Returns:
Location cityLocation
 o getDistanceFrom
 public int getDistanceFrom(Connection conn,
                            String ap) throws SQLException
Calculates the distance between current city and another city. You specify the second city by stating its airport code.

Parameters:
conn - Connection
ap - the other city's airport code
Returns:
int number of miles between them.
Throws: SQLException
Thrown if there is a SQL error.
 o getDistanceFrom
 public int getDistanceFrom(Connection conn,
                            int i) throws SQLException
Calculates the distance between current city and another city. You specify the second city by stating its city id.

Parameters:
conn - Connection
i - the other city's id
Returns:
int number of miles between them.
Throws: SQLException
Thrown if there is a SQL error.
 o getDistanceFrom
 public int getDistanceFrom(City cityb)
Calculates the distance between current city and another city.

Parameters:
conn - Connection
cityb - the other city
Returns:
int number of miles between them.
See Also:
getDistance
 o getDistanceFrom
 public int getDistanceFrom(City cityb,
                            int i)
Calculates the distance between current city and another city and converts units to the appropriate measurement system.

Parameters:
cityb - the other city
Returns:
int number of miles between them OR number of kilometers.
See Also:
Location
 o getDistanceFromHomeCityAsString
 public String getDistanceFromHomeCityAsString(City homeCity)
Calculates the distance between current city and home city using measurement system of home city, and converts units to kilometers if necessary

Parameters:
cityb - the other city
Returns:
String String showing distance between cities
See Also:
Location
 o setTimeZone
 public void setTimeZone(double GMToff,
                         double DSTGMToff,
                         String beginDST,
                         String endDST)
Sets the TimeZone object for the city.

Parameters:
GMToff - Offset in hours from Greenwich Mean Time
DSTGMToff - Offset in hours from Greenwich Mean Time when Daylight Savings Time is in effect.
beginDST - A String code that represents when Daylight Savings Time begins. The possible values are:
      "FSA" - First Sunday in April
      "FSO" - First Sunday in October
      "SSO" - Second Sunday in October
      "SSM" - Second Sunday in March
      "SSF" - Second Sunday in February
      "FSS" - First Sunday in September
      "FSM15" - First Sunday on or after March 15
      "LSO" - Last Sunday in October
      "LSM" - Last Sunday in March
      "FSM" - First Sunday in May
      "TSO" - Third Sunday in October
      "TSS" - Third Sunday in September
      "FFM" - First Friday in May
      "LWS" - Last Wednesday in September
      "LSS" - Last Sunday in September
      "SSA" - Second Sunday in April
      "TSM" - Third Sunday in March
  
endDST - A String code that represents when Daylight Savings Time ends. See above for possible values.
See Also:
TimeZone
 o setTimeZone
 public void setTimeZone(double GMToff)
Sets the TimeZone object for the city. Use for cities that don't have Daylight Savings Times.

Parameters:
GMToff - the GMT time offset (e.g., -3)
 o getTimeZone
 public TimeZone getTimeZone()
Returns the city's TimeZone object.

Returns:
TimeZone cityTimeZone
 o getTimeZoneAsString
 public String getTimeZoneAsString()
Returns the city's TimeZone's toString() value. For use in Cloudview.

Returns:
TimeZone cityTimeZone
 o getTimeDifference
 public double getTimeDifference(Date theDate,
                                 City cityb)
Calculates the time zone difference between current city and another city for date specified.

Parameters:
theDate - desired date
cityb - the other city
Returns:
double the time difference
 o getTimeDifferenceAsString
 public String getTimeDifferenceAsString(Date theDate,
                                         City cityb)
Calculates the time zone difference between current city and another city for date specified and returns a String.

Parameters:
theDate - desired date
cityb - secondCity
Returns:
String string showing time difference
 o getTimeDifference
 public double getTimeDifference(Connection conn,
                                 Date theDate,
                                 String ap) throws SQLException
Calculates the time zone difference between current city and another city for date specified. You specify the second city by its airport code.

Parameters:
theDate - desired date
Returns:
ap the airport code of the other city.
Throws: SQLException
Thrown if there is a SQL error.
 o isTropical
 public boolean isTropical()
Returns true if the city lies within the tropics.

 o getNorthSouthOrderableLocation
 public double getNorthSouthOrderableLocation()
Returns a double latitude value. Allows you to order cities from north to south.

 o getEastWestOrderableLocation
 public double getEastWestOrderableLocation()
Returns a double latitude value. Allows you to order cities from east to west.

 o equals
 public boolean equals(Object o)
Good Cloudscape practice requires that an object override equals and hashCode for correct implementation of = and <> in queries.

Returns:
boolean if the two objects are equals.
Overrides:
equals in class Object
 o compareTo
 public int compareTo(Object o) throws ClassCastException
This method allows a City object to be orderable within SQL-J statements.

We have arbitrarily decided to order cities primarily by their Locations, and secondarily by their time zones. See the javadoc for Location and TimeZone to see how those objects are ordered.

Returns:
int -1 if this object is "less than" the parameter, 0 if they or equal, or 1 if it is "greater than" the parameter..
Throws: ClassCastException
Thrown if the objects cannot be compared.
See Also:
TimeZone, Location
 o hashCode
 public int hashCode()
Overrides:
hashCode in class Object
 o prepareStaticStatements
 public static boolean prepareStaticStatements(Connection conn,
                                               boolean all) throws SQLException
Prepares some or all of the PreparedStatements. This method is called by static methods.

Parameters:
connn - a connection to the database
Returns:
boolean if statements are prepared without error
Throws: SQLException
if there is an SQL error
 o writeExternal
 public void writeExternal(ObjectOutput out) throws IOException
Externalizable interface Write this object to ObjectOutput stream

Throws: IOException
Thrown if there is an IO Error writing the object
 o readExternal
 public void readExternal(ObjectInput in) throws IOException
Externalizable interface Read this object from ObjectOutput stream

Throws: IOException
Thrown if there is an IO Error reading the object or the input stream does not contain the expected objects in the expected order

All Packages  Class Hierarchy  This Package  Previous  Next  Index