So there's this old programming assignment from last semester and he's giving extra credit if we finish it.
Here's the assignment
http://www.cs.oswego.edu/~odendahl/coursework/csc212/201009/assignments/04.html
I really want to finish this but it's the first time my class is trying to do it without pseudo code.
Here's what I have so far for each java class
Main. java
/**
* File: Main.java
* Function: store list of states and their populations
*
* Input Format:
* Data file:
* state name # population-year-1 population-year-2 population-year-3
*
* Output Format:
*
*/
package csc212project04a;
import static java.lang.System.out;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
// public static void fillList(Scanner sc, U_S_States list)
public static void fillList(Scanner sc, int[] years, U_S_States list)
{
while ( sc.hasNext() ) {
String line = sc.nextLine();
String[] stateAndPops = line.split("#");
String name = stateAndPops[0];
String[] yearlyPopulationFields = stateAndPops[1].split("\\s+");
int[] yearlyPopulations = new int[yearlyPopulationFields.length];
for (int i = 0; i < yearlyPopulations.length; i++) {
yearlyPopulations[i] = Integer.parseInt(yearlyPopulationFields[i]) * 1000;
}
// State state = new State(name, yearlyPopulations);
State state = new State(name, years, yearlyPopulations);
list.add(state);
}
}
public static void main(String[] args) throws Exception, FileNotFoundException
{
System.out.println("Welcome to State of Affairs' ...");
System.out.println("\t... a program which tells you population statistics\n" +
"\t of some U.S. states.\n");
State state = null;
Scanner sc = new Scanner(new File("/home/rick/data1.txt"));
String header = sc.nextLine();
String[] headerFields = header.split("\\s+");
int[] years = new int [headerFields.length-1];
for (int i = 0; i < years.length; i++) {
try {
years[i] = Integer.parseInt(headerFields[i+1]);
} catch (NumberFormatException nfe) {
years[i] = 0;
out.println("Error " + nfe);
}
}
U_S_States states = new U_S_States(headerFields);
// fillList(sc, states);
fillList(sc, years, states);
sc.close();
out.println("Here is the list of states and their populations");
out.println("------------------------------------------------");
String[] headers = states.getHeaders();
for ( int i = 0; i < headers.length; i++ ) {
if (i == 0) out.format("%20s", headers[i]);
else out.format("%10s", headers[i]);
}
out.format("\n");
for ( int i = 0; i < states.size(); i++ ) {
state = states.get(i);
out.format("%20s", state.name());
for (int year = 0; year < state.numberOfYears(); year++) {
out.format("%10d", state.population(year));
}
out.format("\n");
}
out.format("\n\n");
// result 1
state = states.stateWithMaximumPopulation(0);
out.format("1. State with maximum population at start year is %s\n", state.name());
// result 2
state = states.stateWithMaximumPopulation(state.numberOfYears()-1);
out.format("2. State with maximum population at last year is %s\n", state.name());
// result 3
state = states.stateWithMinimumPopulation(0);
out.format("1. State with minimum population at last year is %s\n", state.name());
// result 4
state = states.stateWithMinimumPopulation(state.numberOfYears()-1);
out.format("2. State with minimum population at last year is %s\n", state.name());
// result 5
state = states.stateWithMaximumPopulationChange();
out.format("2. State with minimum population at last year is %s\n", state.name());
// result 6
state = states.stateWithMinimumPopulationChange();
out.format("2. State with minimum population at last year is %s\n", state.name());
// result 7
state = states.stateWithMaximumPercentPopulationChange();
out.format("2. State with minimum population at last year is %s\n", state.name());
state = states.stateWithMinimumPercentPopulationChange();
out.format("2. State with minimum population at last year is %s\n", state.name());
//
out.println("\n\n9. Here are the total and average population change over this period");
out.println("--------------------------------------------------------------------\n");
out.format("%20s%15s%15s\n", "State", "Total Change", "Average Change");
out.format("%20s%15s%15s\n", "-----", "------------", "--------------");
for ( int i = 0; i < states.size(); i++ ) {
// LEFT AS AN EXERCISE
//
}
out.println("\nThanks for using our system.");
}
}
State.java
/**
* File: State.java
* Represents a U.S. state.
*
*/
package csc212project04a;
public class State
{
private String name;
private int[] years;
private int[] population;
// public State(String name, int[] population)
public StateSolved(String name, int[] years, int[] population)
{
// LEFT AS AN EXERCISE
}
public String name()
{
return name;
}
public int population(int year)
{
return population[year];
}
public int numberOfYears()
{
return population.length;
}
public int maxPopulation()
{
int res = population[0];
for (int i = 1; i < population.length; i++) {
if ( population[i] > res ) res = population[i];
}
return res;
}
public int minPopulation()
{
// LEFT AS AN EXERCISE
int res = population[0];
for (int i = 1; i > population.length; i++){
if (population[i] < res ) res = population[i];
}
return res;
// remove this line after "exercise" is completed
//return 0;
}
public int populationChange()
{
// LEFT AS AN EXERCISE
// remove this line after "exercise" is completed
return 0;
}
public double percentPopulationChange()
{
// LEFT AS AN EXERCISE
// remove this line after "exercise" is completed
return 0.0;
}
public double averagePopulationChange()
{
// LEFT AS AN EXERCISE
// Note: average would be calculated by dividing by
// (years[years.length-1] - years[0])
// remove this line after "exercise" is completed
return 0.0;
}
}
U_S_States.java
/**
* File: U_S_States.java
* A "container class" for a collection of U.S. states.
*
*/
package csc212project04a;
import java.util.ArrayList;
public class U_S_States
{
private ArrayList<State> states;
private String[] headers;
public U_S_States(String[] headers)
{
states = new ArrayList<State>();
this.headers = headers;
}
public String[] getHeaders()
{
return headers;
}
public State get(int i)
{
return states.get(i);
}
public void add(State state)
{
states.add(state);
}
public int size() { return states.size(); }
public State stateWithMaximumPopulation()
{
State temp = states.get(0);
for (int i = 1; i < states.size(); i++) {
if (temp.maxPopulation() < states.get(i).maxPopulation()) {
temp = states.get(i);
}
}
return temp;
}
public State stateWithMaximumPopulation(int year)
{
State temp = states.get(0);
for (int i = 1; i < states.size(); i++) {
if (temp.population(year) < states.get(i).population(year)) {
temp = states.get(i);
}
}
return temp;
}
public State stateWithMinimumPopulation()
{
State temp = states.get(0);
for (int i = 1; i > states.size(); i++) {
if (temp.minPopulation() > states.get(i).minPopulation()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
// return null;
}
public State stateWithMinimumPopulation(int year)
{
// LEFT AS AN EXERCISE
State temp = states.get(0);
for (int i = 1; i > states.size(); i++) {
if (temp.minPopulation() > states.get(i).minPopulation()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
// return null;
}
public State stateWithMaximumPopulationChange()
{
State temp = states.get(0);
for (int i = 1; i < states.size(); i++) {
if (temp.populationChange() < states.get(i).populationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
//return null;
}
public State stateWithMinimumPopulationChange()
{
State temp = states.get(0);
for (int i = 1; i > states.size(); i++) {
if (temp.populationChange() > states.get(i).populationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
//return null;
}
public State stateWithMaximumAveragePopulationChange()
{
// LEFT AS AN EXERCISE
State temp = states.get(0);
for (int i = 1; i < states.size(); i++) {
if (temp.averagePopulationChange() < states.get(i).averagePopulationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
// return null;
}
public State stateWithMinimumAveragePopulationChange()
{
// LEFT AS AN EXERCISE
State temp = states.get(0);
for (int i = 1; i > states.size(); i++) {
if (temp.averagePopulationChange() > states.get(i).averagePopulationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
// return null;
}
public State stateWithMaximumPercentPopulationChange()
{
// LEFT AS AN EXERCISE
State temp = states.get(0);
for (int i = 1; i < states.size(); i++) {
if (temp.percentPopulationChange() < states.get(i).percentPopulationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
//return null;
}
public State stateWithMinimumPercentPopulationChange()
{
// LEFT AS AN EXERCISE
State temp = states.get(0);
for (int i = 1; i > states.size(); i++) {
if (temp.percentPopulationChange() > states.get(i).percentPopulationChange()) {
temp = states.get(i);
}
}
return temp;
// remove the next line after "exercise" is complete
//return null;
}
}