All pastes #806925 Raw Edit

Something

public lua v1 · immutable
·published 2007-12-06 00:53 UTC
rendered paste body
public class Ratuski_Nicholas_A5Q1 {    /*     * PURPOSE: calculate population change     */        //Count between start and end    public static int countInRange(String text, char start, char end) {        char[] textArray = text.toCharArray(); //Convert the string to an array        int count; //How much in range?        count = 0;        for (int i=0;i<textArray.length;i++) {        	if (((int)textArray[i] >= (int)start) && ((int)textArray[i] <= (int)end)) {        		count++;        	}        }        return count;    }        //Count between start and end    public static long[] readLongData(int years, int start) {    	String temp;    	int numDigits;    	int startYear;        long[] yearArray = new long[years];                for (int i=0; i<yearArray.length; i++) {        	        	yearArray[i] = start + i;        	temp = JOptionPane.showInputDialog	        ("Enter the population for year " + (start+i));	        	        numDigits = countInRange(temp,'0','9');	        	        while (numDigits < temp.length()) {	        	temp = JOptionPane.showInputDialog	        	("Enter the population for " + (start+i));	       		numDigits = countInRange(temp,'0','9');	        }	        yearArray[i] = Long.parseLong(temp); 	        System.out.println(yearArray[i]);        }        return yearArray;    }        public static double[] findChanges( long[] yearArray ) {    	double[] percentChange = new double[yearArray.length - 1];    	for (int i=1; i<yearArray.length; i++) {    		percentChange[i-1] = (double)((yearArray[i] - yearArray[i-1])/yearArray[i-1]) * 100.0;      		System.out.println("VALUES FOR SLOT "+i+": initial: "+yearArray[i-1]+" final: "+yearArray[i]+" percent change: "+percentChange[i-1]);  		    	}    	return percentChange;    }        public static String getSpaces(int spaces) {    	String line;    	line = " ";    	for (int i=0; i<spaces; i++) {    		line = line + " ";    	}    	return line;    }        public static void printTable( double[] popChange, String name, int year ) {    	int numStars,numSpaces;    	String stars;    	    	numSpaces = 20; //default amount of spaces.    	    	System.out.println("Population change in "+name+": ("+year+" - "+(year+popChange.length)+").\n");    	System.out.println("year|                     |");    	    	for (int i=0; i<popChange.length; i++) {    		    		numStars = (int) popChange[i];    		stars = "";    		for (int j=0; j<numStars; j++) {    			stars = stars+"*";    		}    		if (numStars < 0) {    			numSpaces += numStars;    			System.out.println((year+i)+"|"+getSpaces(numSpaces)+stars+"|");    		} else if (numStars > 0) {    			numSpaces = numSpaces - numStars;    			System.out.println((year+i)+"|"+getSpaces(20)+"|"+stars+getSpaces(numSpaces));    		} else {    			System.out.println((year+i)+"|"+getSpaces(20)+"|");    		}	    	}    }         public static void main (String [] args) {        String temp;                           //temporary input string        String name;                           //city name       	int numCities;                         //how many cities to work with?       	int numYears;                          //amount of years       	int startYear;                         //year to start at       	long[] yearArray;                      //array of years       	double[] percentArray;                 //array of percent changes             numCities = 1;		temp = JOptionPane.showInputDialog	    ("Enter how many cities you want to calculate the population changes for.");	    numCities = Integer.parseInt(temp);				for (int i=0; i<numCities; i++) {				 	temp = JOptionPane.showInputDialog	    	("Enter the name of the city.");	    	name = temp;	    		    	temp = JOptionPane.showInputDialog	    	("Enter the number of years.");	    	numYears = Integer.parseInt(temp);	    		    	temp = JOptionPane.showInputDialog	    	("Enter the starting year. (eg. 1989)");	    	startYear = Integer.parseInt(temp);	    		    	yearArray = readLongData(numYears, startYear);	    	percentArray = findChanges(yearArray);	    	printTable(percentArray, name, startYear);		 			}		System.out.println("\n\nProgrammed by Nick Ratuski");   		System.out.println("Date: Saturday, November 17");	}}