State Applet

<< Curve-O-Matic | OtherProjectsTrailIndex | Other Planets >>

See a working copy here.

If its hard to see how the sorting is done, take a look here

Using the utility class Collections we can sort our states. There are a variety of ways to sort them, and there are a variety of ways to compare objects in java.

  1. implementing the compareTo method
  2. making a static anonymous class that implements Comparator in the State Class
  3. making an anonymous class that implements Comparator in the application.
  4. Making a separate class that implements Comparator

each of the implementations of the Comparator interface have to implement the compare method.

See if you can add a getDensity method in the State class, add that to the Panel, and then add the button choice to sort by the population density!

Density would be how people there are per square mile.

Special Thanks to Jake Rasic '23 for updating the population data to 2023!

State.java

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.util.Comparator;


public class State implements Comparable<State>
{
	private String name;
	private String abbr;
	private int pop;
	private int area;
	public State(String name, String abbr, int pop, int area ){
		this.setName(name);
		this.setAbbr(abbr);
		this.setPop(pop);
		this.setArea(area);
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAbbr(String abbr) {
		this.abbr = abbr;
	}
	public String getAbbr() {
		return abbr;
	}
	public void setPop(int pop) {
		this.pop = pop;
	}
	public int getPop() {
		return pop;
	}
	public void setArea(int area) {
		this.area = area;
	}
	public int getArea() {
		return area;
	}
	public Panel getPanel(){
		Panel result=new Panel(new GridLayout(1,0));
		result.add(new Label (getName()));
		result.add(new Label (getAbbr()));
		result.add(new Label (""+getPop()));
		result.add(new Label (""+getArea()));		
		return result;
	}
	@Override
	public int compareTo(State other) {

		return name.compareTo(other.getName());
	}
	public static Comparator<State> AbbrComparator = new Comparator<State>(){

		@Override
		public int compare(State state1, State state2) {
			return state1.getAbbr().compareTo(state2.getAbbr());
		}

	};

}

AreaComparator.java

import java.util.Comparator;

public class AreaComparator implements Comparator<State> {

	@Override
	public int compare(State s1, State s2) {

		return s1.getArea()-s2.getArea();
	}

}

StateApp.java

//import java.applet.Applet;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class StateApp extends Frame implements ActionListener
{
    private ArrayList<State> states;
    private Panel statePanel;
    private JScrollPane sp;
    private Button sortButton;
    JRadioButton name, abbr, pop, area, ascending, descending;
    public  StateApp(){
        super("Compare States");
        setSize(1200,1000);
        this.addWindowListener(new WindowListener() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
            public void windowClosed(WindowEvent e) {}
            public void windowOpened(WindowEvent e) {}
            public void windowIconified(WindowEvent e) {}
            public void windowDeiconified(WindowEvent e) {}
            public void windowActivated(WindowEvent e) {}
            public void windowDeactivated(WindowEvent e) {}
       }); 
        states= new ArrayList<State>();
         states.add(new State("Alabama","AL",5097641,50744));
        states.add(new State("Alaska","AK",740339,571951));
        states.add(new State("Arizona","AZ",7379346,113635));
        states.add(new State("Arkansas","AR",3040207,52068));
        states.add(new State("California","CA",40223504,155959));
        states.add(new State("Colorado","CO",5997070,103718));
        states.add(new State("Connecticut","CT",3615499,4845));
        states.add(new State("Delaware","DE",1017551,1954));
        states.add(new State("Florida","FL",22359251,53927));
        states.add(new State("Georgia","GA",11019186,57906));
        states.add(new State("Hawaii","HI",1483762,6423));
        states.add(new State("Idaho","ID",1920562,82747));
        states.add(new State("Illinois","IL",12807072,55584));
        states.add(new State("Indiana","IN",6876047,35867));
        states.add(new State("Iowa","IA",3233572,55869));
        states.add(new State("Kansas","KS",2963308,81815));
        states.add(new State("Kentucky","KY",4555777,39728));
        states.add(new State("Louisiana","LA",4695071,43562));
        states.add(new State("Maine","ME",1372559,30862));
        states.add(new State("Maryland","MD",6298325,9774));
        states.add(new State("Massachusetts","MA",7174604,7840));
        states.add(new State("Michigan","MI",10135438,56804));
        states.add(new State("Minnesota","MN",5827265,79610));
        states.add(new State("Mississippi","MS",2959473,46907));
        states.add(new State("Missouri","MO",6204710,68886));
        states.add(new State("Montana","MT",1112668,145552));
        states.add(new State("Nebraska","NE",2002052,76872));
        states.add(new State("Nevada","NV",3225832,109826));
        states.add(new State("New Hampshire","NH",1395847,8968));
        states.add(new State("New Jersey","NJ",9438124,7417));
        states.add(new State("New Mexico","NM",2135024,121356));
        states.add(new State("New York","NY",20448194,47214));
        states.add(new State("North Carolina","NC",10710558,48711));
        states.add(new State("North Dakota","ND",811044,68976));
        states.add(new State("Ohio","OH",11878330,40948));
        states.add(new State("Oklahoma","OK",4021753,68667));
        states.add(new State("Oregon","OR",4359110,95997));
        states.add(new State("Pennsylvania","PA",13092796,44817));
        states.add(new State("Rhode Island","RI",1110822,1045));
        states.add(new State("South Carolina","SC",5266343,30109));
        states.add(new State("South Dakota","SD",908414,75885));
        states.add(new State("Tennessee","TN",7080262,41217));
        states.add(new State("Texas","TX",30345487,261797));
        states.add(new State("Utah","UT",3423935,82144));
        states.add(new State("Vermont","VT",648279,9250));
        states.add(new State("Virginia","VA",8820504,39594));
        states.add(new State("Washington","WA",7999503,66544));
        states.add(new State("West Virginia","WV",1775932,24078));
        states.add(new State("Wisconsin","WI",5955737,54310));
        states.add(new State("Wyoming","WY",580817,97100));
        this.setLayout(new BorderLayout());
        statePanel=new Panel(new GridLayout(0,1));
        for (State s:states)
            statePanel.add(s.getPanel());
        sp = new JScrollPane(statePanel);
        this.add(sp, BorderLayout.CENTER);

        setUpButtons();
    }

    public void setUpButtons()
    {
        name = new JRadioButton("Sort by Name");
        abbr = new JRadioButton("Sort by Abbr.");
        pop = new JRadioButton("Sort by Pop");
        area = new JRadioButton("Sort by Area");
        name.setSelected(true);
        ButtonGroup group = new ButtonGroup();
        group.add(name);
        group.add(abbr);
        group.add(pop);
        group.add(area);
        Panel sortTypePanel=new Panel(new GridLayout(0,1));

        sortTypePanel.add(name);
        sortTypePanel.add(abbr);
        sortTypePanel.add(pop);
        sortTypePanel.add(area);
        Panel eastPanel=new Panel(new GridLayout(0,1));
        eastPanel.add(sortTypePanel);
        this.add(eastPanel, BorderLayout.SOUTH);
        sortButton = new Button("Sort");
        sortButton.addActionListener(this);
        Panel buttonPanel = new Panel(new FlowLayout());
        buttonPanel.add(sortButton);
        eastPanel.add(buttonPanel);

        ascending = new JRadioButton("Acending");
        descending = new JRadioButton("Descending");
        ascending.setSelected(true);
        ButtonGroup group2 = new ButtonGroup();
        group2.add(ascending);
        group2.add(descending);
        Panel sortOrderPanel=new Panel(new FlowLayout());
        sortOrderPanel.add(ascending);
        sortOrderPanel.add(descending);
        eastPanel.add(sortOrderPanel);
        eastPanel.setBackground(new Color(255,237,185));
        this.add(eastPanel, BorderLayout.EAST);
    }
     public static void main(String[] args)
    {
        StateApp app= new StateApp();
        app.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==sortButton){
            if (name.isSelected()){
                if(ascending.isSelected()){
                    Collections.sort(states);
                }else{
                    Comparator<State> rev = Collections.reverseOrder();
                    Collections.sort(states, rev);
                }
            }
            if(abbr.isSelected()){
                if(ascending.isSelected()){
                    Collections.sort(states,State.AbbrComparator);
                }else{
                    Comparator<State> rev = Collections.reverseOrder(State.AbbrComparator);
                    Collections.sort(states, rev);
                }
            }
            if(pop.isSelected()){
                Comparator<State> popComp =new Comparator<State>(){
                        public int compare(State s1, State s2){
                            return s1.getPop()-s2.getPop();
                        }
                    };
                if(ascending.isSelected()){

                    Collections.sort(states, popComp);
                }else{
                    Comparator<State> rev = Collections.reverseOrder(popComp);
                    Collections.sort(states, rev);
                }
            }
            if(area.isSelected()){
                if(ascending.isSelected()){
                    Collections.sort(states,new AreaComparator());
                }else{
                    Comparator<State> rev= Collections.reverseOrder(new AreaComparator());
                    Collections.sort(states, rev);
                }
            }
            statePanel=new Panel(new GridLayout(0,1));
            for (State s:states)
                statePanel.add(s.getPanel());
            this.remove(sp);
            sp = new JScrollPane(statePanel);
            this.add(sp, BorderLayout.CENTER);
            this.validate();
        }

    }
}