CS U370 Assignment #1 Spring 2008, Prof. Hafner Assigned: Friday, January 11, 2008 Due: Tuesday, January 22, 2008 The purpose of this assignment is: * To review basic Java programming concepts, such as objects, methods, variables, etc. * To introduce the use of Sun's Java SDK (software development kit) and the Java API (application programming interface). * To learn the required procedure for submitting homework in this course * To begin our study of abstraction-based software design by providing a formal specification for the City class * To introduce the use of factory methods for creating new objects * To introduce the canonical method toString() * To highlight the difference between class (static) variables and methods and instance variables and methods. You will complete an implementation in Java of the City class that is specified below. Collaboration between students is forbidden on this assignment. You are responsible for keeping your code hidden from all other students. Turn in your homework by submitting your City.java source file. Detailed submission instructions will be provided before the due date. The submitted file should begin with a block comment that lists 1. Your name, as you want the instructor to write it. 2. Your email address. 3. Any remarks that you wish to make to the instructor. Part of your grade will depend on the quality and correctness of your code; part will depend on the readability of your code (including comments and indentation), and part will depend on your following the procedure above for submitting your work. Late assignments may be discounted, and very late assignments may be discarded. -------------------------------------------------- Your assignment is to write the code for a single file, City.java, that implements the specification below. For this first assignment, a main program will be provided via the class web site to test your solution. (In future assignments, you will be required to develop your own test program.) -------------------------------------------------- Specification of the City class. The City class shall be implemented in Java, and will be tested using Sun's Java 2 Runtime Environment, Standard Edition. The code for this implementation shall be in the default package, and shall define an immutable public class named City , which provides the interface which is specified below. ####################### Signature: /* Creators */ public static City basic (String, int, double, double); public static City capital(String, String, int, double, double); /* Accessors*/ public String getName(); public int getPopulation(); public double getLatitude(); public double getLongitude(); public String capitalOf(); public double northSouthDist(City); public double eastWestDist(City); /* Predicates*/ public boolean isCapital(); public boolean isLarger(City); /* Canonical methods*/ public String toString (); /* Class methods */ public static int cityCount(); public static int populationTotal(); public static String northMost(); public static String southMost(); ########################## Behavior /* Accessors*/ City.basic(n, p, la, lo).getName() = n; City.capital(n, s, p, la, lo).getName() = n; City.basic(n, p, la, lo).getPopulation() = p; City.capital(n, s, p, la, lo).getPopulation() = p; City.basic(n, p, la, lo).getLatitude() = la; City.capital(n, s, p, la, lo).getLatitude() = la; City.basic(n, p, la, lo).getLongitude() = lo; City.capital(n, s, p, la, lo).getLongitude() = lo; City.capital(n, s, p, la, lo).capitalOf() = s; City.basic(n, p, la, lo).northSouthDist(other) = 69.17 * |la - other.getLatitude()| City.capital(n, s, p, la, lo).northSouthDist(other) = 69.17 * |la - other.getLatitude()| City.basic(n, p. la, lo).eastWestDist(other) = |lo - other.getLongitude()| * 69.17 * cos(la * PI/180) if |lo - other.getLongitude()| <= 180 (360 - |lo - other.getLongitude()|) * 69.17 * cos(la * PI/180) OTHERWISE City.capital(n, s, p, la, lo).eastWestDist(other) = |lo - other.getLongitude()| * 69.17 * cos(la * PI/180) if |lo - other.getLongitude()| <= 180 (360 - |lo - other.getLongitude()|) * 69.17 * cos(la * PI/180) OTHERWISE /* Predicates*/ City.basic(n, p, la, lo).isCapital() = false; City.capital(n, s, p, la, lo).isCapital() = true; City.basic(n, p, la, lo).isLarger(other) = true if p > other.getPopulation() false otherwise City.capital(n, s, p, la, lo).isLarger() = true if p > other.getPopulation() false otherwise /* Canonical methods*/ City.basic(n, p, la, lo).toString() = `"n has population p and is located at [la, lo]" City.capital(n, s, p, la, lo).toString() = `"n is the capital of s, it has population p and is located at [la, lo]" /* Class methods */ City.cityCount() = the number of City instances; initially 0 City populationTotal() = the total population of all City instances; initially 0 City.northMost() = the name of the northernmost city; initially "" City.southMost() = the name of the southernmost city; initially "" **************************** NOTES ********************************** Note: in defining the output of toString(), the use of a "backquote" is intended to signify that whenever a variable appears in the given string it should be replaced by its value. Note: The latitude must be no less than -90.0 and no greater than 90.0. The longitude must be no less than -180.0 and no greater than 180.0 The population must be greater than 0 If any of these conditions is violated, the implementation should throw an exception as follows: throw new IllegalArgumentException("......."); where ..... is replaced by an appropriate comment. Note: If the client invokes a method whose behavior is undefined, the implementation should throw and exception as follows: throw new UnsupportedOperationException(" ....."); where ..... is replaced by an appropriate comment.