AP Computer Science --- Haas --- SelectionSortArrayList
Objective: Implement a Selection Sort on an ArrayList.
/**
* US States have name, population, and land area instance variables
*
* <<< This code is NOT complete >>>
*
* You must complete the compareTo method.
*/
public class State implements Comparable {
private String name;
private int population;
private int landArea; // in square miles
public State(String name, int landArea, int population) {
this.name = name;
this.landArea = landArea;
this.population = population;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public int getLandArea() {
return landArea;
}
public String toString() {
String blanks = " ";
return (name + blanks.substring(name.length()) +
" People Per Square Mile: " + population/landArea);
}
/**
* <<< COMPLETE THE CODE: write a compareTo method >>>
* Compares two states by population density.
* (Population density is the number of people per land area.)
*
* 1) returns value < 0 if "this" is less than "other"
* 2) returns value = 0 if "this" is equal to "other"
* 3) returns value > 0 if "this" is greater that "other"
*/
public int compareTo(Object other) {
//<<< COMPLETE THE CODE >>>
}
}
/**
* <<< This code is NOT complete. >>>
*
* This class has a method "sort" which should perform
* a selection sort on an ArrayList of States.
*/
import java.util.*;
public class SelectionSortArrayList {
/**
* performs a selection sort on an ArrayList of States
*/
public static void sort(ArrayList < State > aList) {
// <<<< complete the code >>>>
}
}
/**
* <<< This code IS complete. >>>
*
* 1) An ArrayList of US States is created.
* 2) Each state has a name, population, and land area.
* 3) The states are sorted by population density.
*
*/
import java.util.*;
public class Tester
{
public static void main(String args[])
{
ArrayList< State > myList = new ArrayList < State >();
myList.add(new State("Alabama" , 50750 , 4447100));
myList.add(new State("Alaska" , 570374 , 626932));
myList.add(new State("Arizona" , 113642 , 5130632));
myList.add(new State("Arkansas" , 52075 , 2673400));
myList.add(new State("California" , 155973 , 33871648));
myList.add(new State("Colorado" , 103729 , 4301261));
myList.add(new State("Connecticut", 4845 , 3405565));
myList.add(new State("Delaware" , 1955 , 783600));
myList.add(new State("Florida" , 53937 , 15982378));
myList.add(new State("Georgia" , 57919 , 8186453));
myList.add(new State("Hawaii" , 6423 , 1211537));
myList.add(new State("Idaho" , 82751 , 1293953));
myList.add(new State("Illinois" , 55593 , 12419293));
myList.add(new State("Indiana" , 35870 , 6080485));
myList.add(new State("Iowa" , 55875 , 2926324));
myList.add(new State("Kansas" , 81823 , 2688418));
myList.add(new State("Kentucky" , 39732 , 4041769));
myList.add(new State("Louisiana" , 43566 , 4468976));
myList.add(new State("Maine" , 30865 , 1274923));
myList.add(new State("Maryland" , 9775 , 5296486));
myList.add(new State("Massachusetts", 7838 , 6349097));
myList.add(new State("Michigan" , 56809 , 9938444));
myList.add(new State("Minnesota" , 79617 , 4919479));
myList.add(new State("Mississippi", 46914 , 2844658));
myList.add(new State("Missouri" , 68898 , 5595211));
myList.add(new State("Montana" , 145556 , 902195));
myList.add(new State("Nebraska" , 76878 , 1711263));
myList.add(new State("Nevada" , 109806 , 1998257));
myList.add(new State("New Hampshire", 8969 , 1235786));
myList.add(new State("New Jersey" , 7419 , 8414350));
myList.add(new State("New Mexico" , 121364 , 1819046));
myList.add(new State("New York" , 47224 , 18976457));
myList.add(new State("North Carolina",48718 , 8049313));
myList.add(new State("North Dakota" ,68994 , 642200));
myList.add(new State("Ohio" , 40953 , 11353140));
myList.add(new State("Oklahoma" , 68679 , 3450654));
myList.add(new State("Oregon" , 96002 , 3421399));
myList.add(new State("Pennsylvania", 44820 , 12281054));
myList.add(new State("Rhode Island", 1045 , 1048319));
myList.add(new State("South Carolina",30111 , 4012012));
myList.add(new State("South Dakota", 75896 , 754844));
myList.add(new State("Tennessee" , 41219 , 5689283));
myList.add(new State("Texas" , 261914 , 20851820));
myList.add(new State("Utah" , 82168 , 2233169));
myList.add(new State("Vermont" , 9249 , 608827));
myList.add(new State("Virginia" , 39598 , 7078515));
myList.add(new State("Washington" , 66581 , 5894121));
myList.add(new State("West Virginia", 24087 , 1808344));
myList.add(new State("Wisconsin" , 54314 , 5363675));
myList.add(new State("Wyoming" , 97105 , 493782));
// print out states UNSORTED
System.out.println(" --------------- U - N - S - O - R - T - E - D ---------------");
for (State i : myList) { System.out.println(i); }
// sort states by population density
SelectionSortArrayList.sort(myList);
// print out states SORTED by population density
System.out.println(" \n --------------- S - O - R - T - E - D ---------------");
for (Comparable i : myList) { System.out.println(i); }
}
}