first commit
This commit is contained in:
252
CS1322/p1/ArrayTest.java
Normal file
252
CS1322/p1/ArrayTest.java
Normal file
@@ -0,0 +1,252 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ArrayTest.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the ArrayTest class
|
||||
* 1.1 Sep. 02, 2002
|
||||
* Compiled, Tested, Coding Done
|
||||
* 1.2 Sep. 05, 2002
|
||||
* Thanks to a failed WinXP SP1 Install, had to recomment
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.2, Sep. 05, 2002
|
||||
*/
|
||||
|
||||
public class ArrayTest {
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Loop through array and return sum
|
||||
*@return sum of array elements
|
||||
*/
|
||||
public int sum(int intArray[]){
|
||||
int sum = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum");
|
||||
}
|
||||
|
||||
//function code
|
||||
for(int i=0;i<intArray.length; i++){
|
||||
sum += intArray[i];
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum");
|
||||
}
|
||||
|
||||
return sum;
|
||||
} //end of sum(int intArray[]);
|
||||
|
||||
/**
|
||||
*create array of size "size" and set set each element .1 higher
|
||||
*than the previous starting with 0.0
|
||||
*@return array with each element 0.1 larger than the previous
|
||||
*/
|
||||
public double[] buildArray(int size){
|
||||
double[] array = new double[size];
|
||||
array[0] = 0.0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration buildArray");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i = 1; i<array.length;i++){
|
||||
array[i] = array[i-1] + 0.1;
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration buildArray");
|
||||
}
|
||||
return array;
|
||||
} //end of buildArray(int size)
|
||||
|
||||
/**
|
||||
*Create an array containing the smallest and largest element of intArray
|
||||
*@return the array containing the smallest and largest element of intArray
|
||||
*/
|
||||
public int[] bounds(int intArray[]){
|
||||
int min = intArray[0], max = intArray[0];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration bounds");
|
||||
}
|
||||
|
||||
//actual code, again
|
||||
for(int i = 0; i<intArray.length;i++){
|
||||
if(intArray[i] > max){
|
||||
max = intArray[i];
|
||||
}
|
||||
if(intArray[i] < min){
|
||||
min = intArray[i];
|
||||
}
|
||||
} //end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration bounds");
|
||||
}
|
||||
|
||||
int bounds[] = new int[2];
|
||||
bounds[0] = min;
|
||||
bounds[1] = max;
|
||||
return bounds;
|
||||
} //end bounds(int intArray[])
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/////////////////
|
||||
//From Phase IV//
|
||||
/////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
/**
|
||||
*Traverse through twoDArray
|
||||
*@return the sum of the elements of twoDArray
|
||||
*/
|
||||
public static int sum(int twoDArray[][]){
|
||||
int sum=0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum(twoDArray)");
|
||||
}
|
||||
|
||||
for(int row=0; row<twoDArray.length; row++){
|
||||
for(int col=0; col<twoDArray[row].length; col++){
|
||||
sum += twoDArray[row][col];
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum(twoDArray)");
|
||||
}
|
||||
return sum;
|
||||
}//end of sum(int twoDArray[][])
|
||||
|
||||
/**
|
||||
*Traverse grid
|
||||
*@return the number of "true's"
|
||||
*/
|
||||
public static int[] getRowCounts(boolean grid[][]){
|
||||
int[] total = new int[grid.length];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration getRowCounts");
|
||||
}
|
||||
|
||||
for(int row=0; row<grid.length; row++){
|
||||
total[row] = 0;
|
||||
for(int col=0; col<grid[row].length; col++){
|
||||
if(grid[row][col] == true){
|
||||
total[row]++;
|
||||
}
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration getRowCounts");
|
||||
}
|
||||
return total;
|
||||
}//end of getRowCounts
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ArrayTest.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
//////////////////
|
||||
//test Phase III//
|
||||
//////////////////
|
||||
ArrayTest arraytest = new ArrayTest();
|
||||
|
||||
//test sum()
|
||||
int[] array = new int[10];
|
||||
for(int i = 0; i<array.length;i++){
|
||||
array[i] = i+1;
|
||||
}
|
||||
|
||||
System.out.println(arraytest.sum(array));
|
||||
|
||||
//test buildArray()
|
||||
double[] array1;
|
||||
array1 = arraytest.buildArray(8);
|
||||
for(int i=0;i<array1.length;i++){
|
||||
System.out.println(array1[i]);
|
||||
}
|
||||
|
||||
//test bounds
|
||||
int[] array2;
|
||||
array2 = arraytest.bounds(array);
|
||||
for(int i = 0; i<array2.length;i++){
|
||||
System.out.println(array2[i]);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
//from Phase IV//
|
||||
//////////////////
|
||||
|
||||
System.out.println("\nPhase IV begins here\n");
|
||||
|
||||
/////////////////
|
||||
//test Phase IV//
|
||||
/////////////////
|
||||
int[][] array2D = new int[5][4];
|
||||
|
||||
//test sum(twoDArray())
|
||||
for(int row=0; row<array2D.length; row++){
|
||||
for(int col=0; col<array2D[row].length; col++){
|
||||
array2D[row][col] = row+col;
|
||||
}
|
||||
}
|
||||
System.out.println(arraytest.sum(array2D) + "\n");
|
||||
|
||||
//test getRowCounts()
|
||||
boolean[][] array2D_1 = new boolean[4][5];
|
||||
for(int row=0; row<array2D_1.length; row++){
|
||||
for(int col=0; col<array2D_1[row].length; col++){
|
||||
if(col%2 != 0){
|
||||
array2D_1[row][col] = false;
|
||||
}
|
||||
else { //col%2 == 0
|
||||
array2D_1[row][col] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
int[] truthtellers = arraytest.getRowCounts(array2D_1);
|
||||
for(int i=0; i<truthtellers.length; i++){
|
||||
System.out.println(truthtellers[i]);
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class ArrayTest
|
||||
140
CS1322/p1/City.java
Normal file
140
CS1322/p1/City.java
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* City.java
|
||||
* Class City;
|
||||
* A class designed to build cities
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the City class
|
||||
* 1.1 Sep. 02, 2002
|
||||
* Finished, compiled, tested
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Sep. 02, 2002
|
||||
*/
|
||||
|
||||
public class City {
|
||||
//global/field scope variables
|
||||
private String name;
|
||||
private String country;
|
||||
private long population;
|
||||
public static final boolean bDebug = false; //not much to debug here, tho
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for City
|
||||
*@param Sets the name for the City
|
||||
*/
|
||||
public void City(String n){
|
||||
name = n;
|
||||
}
|
||||
|
||||
/**
|
||||
*Default Constructor for City
|
||||
*@param sets the name to "Unknown"
|
||||
*/
|
||||
public void City(){
|
||||
name = "Unknown";
|
||||
}
|
||||
|
||||
/*********************************/
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Returns the City name
|
||||
*@return value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the City name
|
||||
*@param n value to assign to name
|
||||
*/
|
||||
public void setName(String n){
|
||||
name = n;
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the country name
|
||||
*@return value of country
|
||||
*/
|
||||
public String getCountry(){
|
||||
return country;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the Country name
|
||||
*@param c value to assign to country
|
||||
*/
|
||||
public void setCountry(String c){
|
||||
country = c;
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the population
|
||||
*@return value of population
|
||||
*/
|
||||
public long getPopulation(){
|
||||
return population;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the population
|
||||
*@param p the value to assign to population
|
||||
*/
|
||||
public void setPopulation(long p){
|
||||
population = p;
|
||||
}
|
||||
|
||||
/***********************************/
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
*
|
||||
* <br><br>
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return name + ", " + country + ": Population: " + population;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class City.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//run debug with my home town, YAY!!!!
|
||||
City Weston = new City();
|
||||
|
||||
Weston.setName("Weston");
|
||||
Weston.setCountry("United States");
|
||||
Weston.setPopulation(2453); //I think I overshot with that number
|
||||
|
||||
System.out.println(Weston);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class City
|
||||
104
CS1322/p1/IterationTest.java
Normal file
104
CS1322/p1/IterationTest.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* IterationTest.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the IterationTest class
|
||||
* 1.1 Sep. 03, 2002
|
||||
* Compiled, Finished, Tested, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Sep. 02, 2002
|
||||
*/
|
||||
|
||||
public class IterationTest {
|
||||
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
/**
|
||||
* Prints the specified message the specified number of times.
|
||||
* <BR>
|
||||
* For example: printMessageALot("5 times!",5) would print
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* <BR><BR>
|
||||
* @param message the message to print
|
||||
* @param count the number of times to print the message
|
||||
*/
|
||||
public void printMessageALot(String message, int count) {
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i = 0; i<count; i++){
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration");
|
||||
}
|
||||
} //end printMessageALot(String,int)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first number raised to the power of the second number.
|
||||
* <BR>
|
||||
* For example: power(4,3) would return 4 * 4 * 4 = 64
|
||||
* <BR><BR>
|
||||
* @param base the number to raise to the power
|
||||
* @param exponent the power to raise the base to (will always be greater
|
||||
* then 0)
|
||||
* @return the base raised to the exponent
|
||||
*/
|
||||
public int power(int base, int exponent) {
|
||||
int current = base;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i=1; i<exponent;i++){
|
||||
current *= base;
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration");
|
||||
}
|
||||
return current;
|
||||
} //end power(int,int)
|
||||
|
||||
/**
|
||||
* Debugging main for class IterationTest.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
IterationTest iterationtest = new IterationTest();
|
||||
|
||||
//HelloWorld to the XTREME!!! Live the eXPerience!!!
|
||||
iterationtest.printMessageALot("Hello World!",5);
|
||||
|
||||
System.out.println(iterationtest.power(2,2));
|
||||
System.out.println(iterationtest.power(3,3));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class IterationTest
|
||||
236
CS1322/p1/World.java
Normal file
236
CS1322/p1/World.java
Normal file
@@ -0,0 +1,236 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* World.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the World class
|
||||
* 1.1 Sep. 03, 2002
|
||||
* Compiled, Tested, Run
|
||||
* 1.2 Sep. 05, 2002
|
||||
* Commented (late thanks to failed winXP SP1)
|
||||
* 1.3 Sep. 16, 2002
|
||||
* Added omit nulls, gg Outlook Express for not updating...
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.3, Sep. 16, 2002
|
||||
*/
|
||||
|
||||
public class World {
|
||||
|
||||
private City[][] map;
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
////////////
|
||||
//Modifier//
|
||||
////////////
|
||||
public void setWorld(City[][] twoDArray){
|
||||
map = twoDArray;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find city at given latitude and longitude
|
||||
*@param lat, the latitude
|
||||
*@param lon, the longitude
|
||||
*@return the City, or NULL if none exists
|
||||
*/
|
||||
public City getCityAt(int lat, int lon){
|
||||
City temp = new City();
|
||||
|
||||
if(map[lon][lat] != null){
|
||||
temp = map[lon][lat];
|
||||
return temp;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}//end of getCityAt(int lat, int lon)
|
||||
|
||||
/**
|
||||
*Find cities at given latitude
|
||||
*@param lat, the latitude
|
||||
*@return the Cities at the given latitude
|
||||
*/
|
||||
public City[] getCititesAtLatitude(int lat){
|
||||
|
||||
//omit nulls without destroying map
|
||||
int nulls = 0;
|
||||
|
||||
for(int i=0; i<map.length; i++){
|
||||
if(map[i][lat] == null){
|
||||
nulls++;
|
||||
}
|
||||
}
|
||||
|
||||
//create temp array for latitude
|
||||
City[] array = new City[map.length-nulls];
|
||||
int current = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin Iteration getCititesAtLat");
|
||||
}
|
||||
|
||||
for(int i=0; i<map.length; i++){
|
||||
if(map[i][lat] != null){
|
||||
array[current] = map[i][lat];
|
||||
current++;
|
||||
}
|
||||
}
|
||||
/////////////////
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End Iteration getCititesAtLat");
|
||||
}
|
||||
|
||||
return array;
|
||||
}//end of getCititesAtLatitude, yes its misspelled in the .nfo
|
||||
|
||||
/**
|
||||
*Find cities at given longitude
|
||||
*@param lon, the longitude
|
||||
*@return the Cities at the given longitude
|
||||
*/
|
||||
public City[] getCititesAtLongitude(int lon){
|
||||
//omit nulls without destroying map
|
||||
int nulls = 0;
|
||||
|
||||
for(int i=0; i<map[lon].length; i++){
|
||||
if(map[lon][i] == null){
|
||||
nulls++;
|
||||
}
|
||||
}
|
||||
|
||||
//create temp array for longitude
|
||||
City[] array = new City[map[lon].length-nulls];
|
||||
int current = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin Iteration getCititesAtLon");
|
||||
}
|
||||
|
||||
for(int i=0; i<map[lon].length; i++){
|
||||
if(map[lon][i] != null){
|
||||
array[current] = map[lon][i];
|
||||
current++;
|
||||
}
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End Iteration getCititesAtLon");
|
||||
}
|
||||
|
||||
return array;
|
||||
}//end of getCititesAtLongitude
|
||||
|
||||
/**
|
||||
* Debugging main for class World.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
City[][] twoDArray = new City[16][6];
|
||||
|
||||
//create the 8 cities, 2 per row....mwahahaha...
|
||||
|
||||
//city #1
|
||||
twoDArray[0][0] = new City();
|
||||
twoDArray[0][0].setName("Weston");
|
||||
twoDArray[0][0].setCountry("United States");
|
||||
twoDArray[0][0].setPopulation(4332);
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("works");
|
||||
System.out.flush();
|
||||
}
|
||||
|
||||
//city #2
|
||||
twoDArray[0][3] = new City();
|
||||
twoDArray[0][3].setName("Nerd Land");
|
||||
twoDArray[0][3].setCountry("Everquest on WINEX");
|
||||
twoDArray[0][3].setPopulation(4511); //too many, thats the population
|
||||
|
||||
//city #3
|
||||
twoDArray[1][2] = new City();
|
||||
twoDArray[1][2].setName("Atlanta");
|
||||
twoDArray[1][2].setCountry("Georgia");
|
||||
twoDArray[1][2].setPopulation(3304000); //as of 2000, yes I checked
|
||||
|
||||
//city #4
|
||||
twoDArray[1][3] = new City();
|
||||
twoDArray[1][3].setName("Vulcan");
|
||||
twoDArray[1][3].setCountry("Nerdtopia"); //actually, I like Star Trek :|
|
||||
twoDArray[1][3].setPopulation(5252); //as of 2001
|
||||
|
||||
//city #5
|
||||
twoDArray[2][0] = new City();
|
||||
twoDArray[2][0].setName("Leene Square");
|
||||
twoDArray[2][0].setCountry("Chrono Trigger Land");
|
||||
twoDArray[2][0].setPopulation(21); //probably about right ;)
|
||||
|
||||
//city #6
|
||||
twoDArray[2][2] = new City();
|
||||
twoDArray[2][2].setName("Doom");
|
||||
twoDArray[2][2].setCountry("When Id was good");
|
||||
twoDArray[2][2].setPopulation(3); //number of classics Id made
|
||||
|
||||
//city #7
|
||||
twoDArray[3][1] = new City();
|
||||
twoDArray[3][1].setName("John Romero");
|
||||
twoDArray[3][1].setCountry("Ion Storm"); //hehe, not anymore :(
|
||||
twoDArray[3][1].setPopulation(1);
|
||||
|
||||
//city #8
|
||||
twoDArray[3][3] = new City();
|
||||
twoDArray[3][3].setName("Zelda");
|
||||
twoDArray[3][3].setCountry("Miyamato's Best");
|
||||
twoDArray[3][3].setPopulation(4); //number of classics he made
|
||||
/*************************************************************/
|
||||
|
||||
World world = new World();
|
||||
world.setWorld(twoDArray);
|
||||
|
||||
System.out.println(world.getCityAt(0,0));
|
||||
System.out.println(world.getCityAt(2,1));
|
||||
|
||||
//////////
|
||||
System.out.println("\n END TEST GETCITY START LAT\n");
|
||||
//////////
|
||||
|
||||
//test latitude
|
||||
City[] latitude;
|
||||
latitude = world.getCititesAtLatitude(3);
|
||||
|
||||
for(int i = 0; i<latitude.length; i++){
|
||||
System.out.println(latitude[i]);
|
||||
}
|
||||
|
||||
//////////
|
||||
System.out.println("\n END TEST LAT START LON\n");
|
||||
//////////
|
||||
|
||||
//test longitude
|
||||
City[] longitude;
|
||||
longitude = world.getCititesAtLongitude(2);
|
||||
|
||||
for(int i = 0; i<longitude.length; i++){
|
||||
System.out.println(longitude[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class World
|
||||
390
CS1322/p1/p1.nfo
Normal file
390
CS1322/p1/p1.nfo
Normal file
@@ -0,0 +1,390 @@
|
||||
CS1322- Program 1 - The World as Arrays
|
||||
=======================================
|
||||
|
||||
Assigned: Sunday, September 1st, 2002
|
||||
Due: Monday, September 16th, 2002 at 11:59:59PM
|
||||
Stop accepting: Tuesday, September 17th, 2002 at 08:00:00AM
|
||||
Phase II Feedback deadline: Friday, September 6th, 2002 at 11:59:59PM
|
||||
Phase IV Feedback deadline: Thursday, September 12th, 2002 at 11:59:59PM
|
||||
|
||||
|
||||
TOPICS
|
||||
==============================================================================
|
||||
|
||||
Topics you should understand coming into this assignment:
|
||||
o The basic mechanics of java
|
||||
o Basics of Objects and methods
|
||||
|
||||
Topics that you should understand as a result of doing this assignment:
|
||||
o Object Creation and manipulation
|
||||
o Iteration
|
||||
o Arrays
|
||||
o Multi-dimensional Arrays
|
||||
o Testing and Debugging
|
||||
o Arrays of Objects
|
||||
|
||||
|
||||
PROGRAM WALKTHROUGH
|
||||
==============================================================================
|
||||
|
||||
In this program, you will be managing a set of cities around the world.
|
||||
You will be creating a data structure which will hold a map of several major
|
||||
cities around the world. Their locations will be identified by latitude and
|
||||
longitude(more on this later).
|
||||
|
||||
|
||||
PHASE I - Building Citites
|
||||
==============================================================================
|
||||
|
||||
Each City will have a name, country, population, and of course: a location.
|
||||
We will be creating a class to hold all of this information for a city. Then,
|
||||
we will creating many instance of this class (also known as objects) which
|
||||
will represent real cities.
|
||||
|
||||
First, create a new class named City. This City should be public so that
|
||||
any other class can see it. This City should have several pieces of data:
|
||||
o A name(called "name") which will be represented as a String
|
||||
o A country(called "country") which will also be represented as a String
|
||||
o A population(called "population") will be represented as a number. Now,
|
||||
the population of a country can be very, very large, so for now, we will
|
||||
want to use a primitive integer type which can be larger than an int.
|
||||
Figure out which type this is and declare the population variable of this
|
||||
type.
|
||||
|
||||
Now, you are going to create a constructor for a City. A constructor is a
|
||||
special type of method that is used to create a new City. The format for a
|
||||
constructor is the same as a method, except that the name must be the name of
|
||||
the class it is in, and there can be no return type. Create a constructor for
|
||||
City that takes in a String which represents the name and sets the name of the
|
||||
City to this new name. Remember that a constructor does not return anything.
|
||||
|
||||
Now, make another constructor for City that takes in nothing, and sets the
|
||||
name to "Unknown".
|
||||
|
||||
As in the first assignment, you created accessors and modifiers, do that for
|
||||
all of these variables now. Remember that the variables should be private and
|
||||
the accessors and modifiers public.
|
||||
|
||||
Now, create a toString() method which will display the data for a City in the
|
||||
following format:
|
||||
|
||||
City: <NAME>, <COUNTRY>: Population: <POPULATION>
|
||||
|
||||
examples: Albany, United States: Population: 1267326
|
||||
Moscow, Russia: Population: 5234800
|
||||
|
||||
|
||||
PHASE II - Iteration Practice (instant feedback phase)
|
||||
==============================================================================
|
||||
|
||||
Eventually, we will need to go through all of the cities in the world to
|
||||
perform certain operations. So, in this phase, we're going to practice
|
||||
"looping" through things.
|
||||
|
||||
*******
|
||||
*LOOPS*
|
||||
*******
|
||||
|
||||
Most of the following can be found in lecture slides:
|
||||
|
||||
for loop
|
||||
--------
|
||||
Description: a loop used primarily to perform a block of code a certain fixed
|
||||
number of times.
|
||||
Example:
|
||||
for(int i=0; i<10; i++) {
|
||||
System.out.println("Now i is: "+i);
|
||||
} //end for
|
||||
|
||||
while loop
|
||||
----------
|
||||
Description: a loop used to perform a block of code until some event occurs.
|
||||
Example:
|
||||
boolean userDidSomethingCool = false;
|
||||
while(userDidSomethingCool == false) {
|
||||
System.out.println("Not yet");
|
||||
userDidSomethingCool = didUserDoCoolStuff();
|
||||
} //end while
|
||||
|
||||
do while loop
|
||||
-------------
|
||||
Description: a loop which functions exactly like a while loop, but is
|
||||
guaranteed to execute at least once
|
||||
Example:
|
||||
int choice;
|
||||
int QUIT_CHOICE = 9;
|
||||
do {
|
||||
choice = getUserInputFromMenu();
|
||||
handleChoice(choice);
|
||||
} (while choice != QUIT_CHOICE);
|
||||
|
||||
Create a class named IterationTest (make sure it is public). In this class,
|
||||
you are going to implement some methods that MUST USE ITERATION.
|
||||
|
||||
/**
|
||||
* Prints the specified message the specified number of times.
|
||||
* <BR>
|
||||
* For example: printMessageALot("5 times!",5) would print
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* <BR><BR>
|
||||
* @param message the message to print
|
||||
* @param count the number of times to print the message
|
||||
*/
|
||||
public void printMessageALot(String message, int count) {
|
||||
//YOUR CODE HERE
|
||||
} //end printMessageALot(String,int)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first number raised to the power of the second number.
|
||||
* <BR>
|
||||
* For example: power(4,3) would return 4 * 4 * 4 = 64
|
||||
* <BR><BR>
|
||||
* @param base the number to raise to the power
|
||||
* @param exponent the power to raise the base to (will always be greater
|
||||
* then 0)
|
||||
* @return the base raised to the exponent
|
||||
*/
|
||||
public int power(int base, int exponent) {
|
||||
//YOUR CODE HERE
|
||||
} //end power(int,int)
|
||||
|
||||
PHASE III - Fun with Arrays
|
||||
==============================================================================
|
||||
|
||||
Now we will work with your first data structure in CS1322, the array. This
|
||||
structure has already been built for you, all you need to do is use it. This
|
||||
phase will get you accustomed to using arrays so that we may use them to
|
||||
manage the recently-created Cities.
|
||||
|
||||
********
|
||||
*ARRAYS*
|
||||
********
|
||||
|
||||
Most of the following can be found in lecture slides:
|
||||
|
||||
Q: What is an array?
|
||||
A: An array is a data structure that can hold a finite amount of data of the
|
||||
same type. You declare how many elements the array can hold, what type of
|
||||
data can go in it, and where. Arrays can be of any type.
|
||||
|
||||
Declaring an array
|
||||
------------------
|
||||
Here is the syntax(format) for declaring an array:
|
||||
<TYPE>[] <NAME> or <TYPE> <NAME>[]
|
||||
For example: int numbers[]; is the same as int[] numbers; Both create an
|
||||
array of ints named numbers.
|
||||
|
||||
Setting the size
|
||||
----------------
|
||||
To set the size of an array, you can create a new array of the specified type
|
||||
and pass the size in as an array parameter. See the following:
|
||||
int numbers[] = new int[8];
|
||||
This statement declares a new int array named numbers and gives it a size of
|
||||
eight. This statement does the same thing:
|
||||
int numbers[];
|
||||
numbers = new int[8];
|
||||
|
||||
Filling up the array with data
|
||||
------------------------------
|
||||
Now we have our numbers array which can only hold eight ints. So, let's put
|
||||
some data into it. The way we get a spot in the array is by indexing it.
|
||||
Here is how we would get the 4th piece of data from the numbers array.
|
||||
int fourth = numbers[3];
|
||||
Q: Why 3?
|
||||
A: Because array indices always start at zero, if we wanted the first piece of
|
||||
data, we would say numbers[0]. Always remember that the index is one less
|
||||
than the element we want. Therefore, the last "cell" in the array would be
|
||||
cell 7, not 8.
|
||||
We can also place data into the array in the same fashion:
|
||||
numbers[2] = 45;
|
||||
This places the number 45 into the third cell in the array.
|
||||
|
||||
Length of an array
|
||||
------------------
|
||||
Arrays have a special way of getting their length. It is a variable named
|
||||
length. To get the length of our numbers array, we would say:
|
||||
numbers.length //returns the int 8
|
||||
|
||||
Iteration and Arrays
|
||||
--------------------
|
||||
Iteration is very useful when dealing with arrays. We could easily create an
|
||||
array that loops through every cell of the array, by having the loop counter
|
||||
of the loop be the index of the array. This way, we could make an operation
|
||||
that performs on every single cell of the array.
|
||||
|
||||
Create a class named ArrayTest (which should be public).
|
||||
|
||||
Code the following methods:
|
||||
|
||||
public int sum(int intArray[])
|
||||
o This method should loop through every cell of the array and return the sum
|
||||
of all of its elements.
|
||||
|
||||
public double[] buildArray(int size)
|
||||
o This method will create an array of doubles of size "size". Each element in
|
||||
the array should be 0.1 larger than the first, with the first being 0.0
|
||||
Example: buildArray(6) would return an array that looks like this:
|
||||
{0.0, 0.1, 0.2, 0.3, 0.4, 0.5}
|
||||
|
||||
public int[] bounds(int intArray[])
|
||||
o This method will iterate through all elements of intArray and create a new
|
||||
int array of size 2. This array will have in its zero position, the
|
||||
smallest value in intArray. This array will have in its one position, the
|
||||
largest value in intArray.
|
||||
Example: int numbers[] = {4,6,8,1,4,0,3};
|
||||
bounds(numbers) would create an array that looks like this:
|
||||
{0,8}
|
||||
|
||||
PHASE IV - Multi-dimensional arrays (instant feedback phase)
|
||||
==============================================================================
|
||||
|
||||
So far, we have worked with one-dimensional arrays. This means that there
|
||||
is only one sequence being worked with. However, we can link arrays to
|
||||
achieve the effect of many dimensions.
|
||||
|
||||
**************************
|
||||
*MULTI-DIMENSIONAL ARRAYS*
|
||||
**************************
|
||||
|
||||
If you picture an array as a row, or a list(one dimension): [][][][][]
|
||||
It can also be pictured as a grid(two dimensions): [][][]
|
||||
[][][]
|
||||
[][][]
|
||||
[][][]
|
||||
Or as a cube(three dimensions): Not going to try and draw a cube...
|
||||
|
||||
This is all in the lecture slides. For this part of the assignment, take a
|
||||
look at Module 18 of the Lecture slides on the course website. The slides
|
||||
will come in very handy for future assignments (including this one)
|
||||
|
||||
Go back into your ArrayTest class.
|
||||
|
||||
Create the following methods:
|
||||
|
||||
public static int sum(int twoDArray[][])
|
||||
o This method will traverse through twoDArray and return the sum of all
|
||||
elements in the 2D array
|
||||
|
||||
public static int[] getRowCounts(int boolean grid[][])
|
||||
o This method will traverse through grid and return the number of "true's" in
|
||||
each row of the array. The returned array will be a one-dimensional array
|
||||
which contains how many occurrences of the boolean value true are in each
|
||||
row of the array. For the remaining part of this assignment, "row" will
|
||||
always refer to the first dimension of the array, and "column" will always
|
||||
refer to the second
|
||||
Example: boolean grid[][] = {{true, false,false,false}, //Row 0
|
||||
{false,true, false,true}, //Row 1
|
||||
{true, true, true, true}}; //Row 2
|
||||
getRowCounts(grid) would return an array that looks like this:
|
||||
{1,2,4}
|
||||
|
||||
PHASE V - bDEBUG and a debug main
|
||||
==============================================================================
|
||||
|
||||
Now you will learn how to debug your programs.
|
||||
|
||||
***********
|
||||
*CONSTANTS*
|
||||
***********
|
||||
|
||||
Constants are variable in a program whose value never changes. For instance,
|
||||
the value of PI will never change, so we make it a constant. To make a
|
||||
constant in Java, use the "final" modifier.
|
||||
|
||||
public static final double PI = 3.141592653;
|
||||
|
||||
We will use a similar technique to debug our code
|
||||
|
||||
debug: To search for and eliminate malfunctioning elements or errors in
|
||||
source: http://www.dictionary.com
|
||||
|
||||
In each one of your classes, create a constant name bDEBUG. It should be
|
||||
public, static, and of course final. It should be a boolean. Also, give this
|
||||
boolean a default value of true.
|
||||
|
||||
Now, go through some of your methods in IterationTest and find a single line
|
||||
of code that does something important. Place this line below it:
|
||||
if(bDEBUG)
|
||||
System.out.println("Just did something important");
|
||||
You can use this constant everywhere that you want to see output from a
|
||||
method. When bDEBUG is set to true, all of this extra information prints out.
|
||||
If you set the constant to false, it does not. This allows your program to
|
||||
run in "debug mode".
|
||||
|
||||
Add the debug constant to all of your classes and use the debugged prints
|
||||
often. You will be graded for good use of this. From now on, all methods
|
||||
which have more functionality than accessors and modifiers will require at
|
||||
least some debug statements in them.
|
||||
|
||||
Debug main
|
||||
----------
|
||||
|
||||
In each one of your classes, make a main method like you did in p0. Your main
|
||||
method should test all of your methods by making variables/arguments for your
|
||||
methods, invoking them, and printing out any results. All classes from now
|
||||
on will require debug mains.
|
||||
|
||||
PHASE VI - A world of Cities
|
||||
==============================================================================
|
||||
|
||||
Now that you know how to make Cities, iterate, and make multi-dimensional
|
||||
arrays, you will put all of it to the test.
|
||||
|
||||
Create a class named World.
|
||||
|
||||
This class will have a private instance variable named map. This variable
|
||||
will be an 2D array of Citites. That's right, we can have arrays of Objects.
|
||||
Each cell in the 2D array will be a City that you created in Phase 1. The
|
||||
idea of arrays of Objects is also covered in Module 18 of the lecture slides.
|
||||
|
||||
Make a constructor for World that take in a 2D array of Cities and assigns the
|
||||
map variable to it. Now, we're going to give the user the ability to search
|
||||
this map by "latitude" and "longitude". For the purposes of this project,
|
||||
this planet will be flat and latitude will be used to access the
|
||||
x-axis(second dimension) while longitude will traverse the
|
||||
y-axis(first dimension). Here is a picture of the world:
|
||||
|
||||
/ \ [ ][ ][*][ ][ ][ ][ ]
|
||||
| [ ][ ][*][ ][ ][ ][ ] * = City
|
||||
longitude [ ][ ][ ][ ][ ][*][ ]
|
||||
| [*][ ][ ][*][ ][ ][ ]
|
||||
\ / [ ][*][ ][ ][ ][ ][ ]
|
||||
|
||||
<- latitude ->
|
||||
|
||||
Note: By default, a 2D array of Objects has all of its values set to null.
|
||||
----
|
||||
|
||||
Create the following method:
|
||||
public City getCityAt(int lat, int lon)
|
||||
o This method will return the City which is located at the specified latitude
|
||||
and longitude. If there is no City there, return null.
|
||||
|
||||
public City[] getCititesAtLatitude(int lat)
|
||||
o Returns an array of all Cities at the specified latitude.
|
||||
|
||||
public City[] getCititesAtLongitude(int lon)
|
||||
o Returns an array of all Cities at the specified longitude
|
||||
|
||||
Make sure you use a debug main here as well as plenty of debug staments. You
|
||||
will want to create the 2D array in the main method as well as all of the
|
||||
Cities. It is your responsibility to test that each of these methods works.
|
||||
Hint: use the toString() method of your City class to help.
|
||||
|
||||
|
||||
DELIVERABLES
|
||||
==============================================================================
|
||||
|
||||
Please turn in the following to webwork:
|
||||
|
||||
City.java
|
||||
IterationTest.java
|
||||
ArrayTest.java
|
||||
World.java
|
||||
|
||||
Make sure that you turn in ALL files by the due time.
|
||||
BIN
CS1322/p1/p1.zip
Normal file
BIN
CS1322/p1/p1.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user