Hello All,
I once again find myself desperate for help, and look to my most reliable source..
This time around the program i'm trying to write is a bit daunting(at least for me). The goal is to
create four seperate reports that are each sorted in different ways.
To try to keep it organized, I will post in this way:
1st I'll post the assignment so you can see exactly what I'm TRYING to do.
2nd I'll post the code that I have written so far, although it' s ROUGH.
3rd I'll post the tiny txt file that is used t read into the program.
Thank you so so much for any expertise,
Jake
//Below is the assignment
Problem Statement: You are to read Year 2000 and Year 1990
census data about ranks and median house-hold income in dollars for
various Maryland counties and Baltimore City, store data in appropriate
arrays, and compute percent change in income in 2000 compared with
1990.
Detailed Discussion – Part I:
Census data gathered in 1990 and 2000 in Maryland counties and Baltimore City are stored in a file.
Each line in file (it will be provided to you) consists of five data items in the order listed below:
National rank in 2000 followed by national rank in 1990; followed by county or city name; then median household income in 2000 followed by median household income in 1990. See sample data file on the next page.
Design and write a class, call it CensusProblemService, to declare the following parallel arrays; you may assume that there are NO more than 50 data lines in the file. Declare arrays to store up to 50 data items. You will test the instance methods in this class by writing another class, CensusProblemClient, which contains the main( ) method and calls the instance methods of CensusProblemService.
ranks2000 -- int -- national rank of county in year 2000
ranks1990 -- int -- national rank of county in year 1990
countyNames -- String
incomes2000 – long -- median household income in 2000
incomes1990 – long -- median household income in 1990
count – int -- total number of data lines read from the file
You are to compute percent change in income and store it in an array; call this array percentChange. Declare percentChange array to store double numbers. You will print the percentages to the nearest 10th (one digit after the decimal). Include other attributes (as necessary) such as count to count the number of data lines read from the file. Assume that all data in the file is valid data.
Here is an example of some array declarations:
int [ ] ranks2000 = new int [50]; //no more than 50 elements
String [ ] countyNames = new String [50]; //no more than 50 names
int [ ] incomes2000 = new int [50]; // no more than 50 elements
etc. etc. etc.
Program Design
All work is accomplished by the instance methods to be called from main( ) method in a client class. Include the following methods in the service class definition and document them. Of course you will write topOfPageInfo method as well. Except for columnHeadings method, you do not need to pass anything to any of the other methods. Partial code for the classes is given below. The various methods are:
storeCensusData( ) -- Use while loop to read unknown number of data lines from a file and store them in appropriate arrays. Each line contains five tokens; first two int, third one a String, and last two also int.
computePercentChange( ) – compute percent change in income and store it in percentChange array. Percent change is computed as follows:
percent change = (incomes2000 – incomes1990) / incomes1990 * 100
(Remember to use a type cast to convert incomes to double; otherwise, your answer will be zero.)
columnHeading( ) – write a method that prints appropriate table heading (such as Original List or Alphabetized List; Alphabetized List etc.) and column headings.
printData( ) – print the original list of data along with the percent change in income under appropriate column headings on Page#1. Note: percentage change may be positive or negative depending upon whether household income went up or down in 2000. Percent change is to be printed to the nearest 10th. Sample output of original list is shown in the following pages. Make sure you (i) number the output pages (ii) number the output lines and (iii) print the number of total data lines processed for each of the reports. Leave a couple of lines blank before printing a new report. Use printf to line up data in columns.
alphabetize( ) – write a method that alphabetizes county/city names included in the data file. Call printData( ) to print the alphabetized list on Page#2 along with all other fields you printed in the original list. Give appropriate table heading such as Alphabetized List before printing column headings. You use the same printData( ) method for all reports.
sortByRanks( ) – write a method that sorts county/city ranks in 2000 included in the data file in ascending order (highest rank which means lowest numerical rank first). Call printData( ) to print the sorted list on Page#3 along with all other fields you printed in the original list. Give appropriate table heading such as Sorted by Rank - Highest to Lowest (lower number is higher rank.)
sortByPercentChange( ) – write a method that sorts by percent change in descending order (highest %age change to lowest %age change). Call printData( ) to print the sorted list on Page#4 along with all other fields you printed in the original list. Give appropriate table heading such as Sorted by Percent Change in Descending Order.
Arrangement of Data in the Input File:
National rank in 2000 followed by national rank in 1990; followed by county or city name; then median household income in 2000 followed by median household income in 1990.
Sample Data file: (5 items on each line) P2censusData.txt
Sample output is shown below. To line up output as neatly as shown below, test the county length. If it is < 12 or 13 (check it out) characters, use more spaces or tabs after printing county names; otherwise use less spaces or tabs. You will need if .. else statement.
Note: To keep the project simple, BaltimoreCity and AnneArundel appear as single tokens (one word; no spaces) so every line has only 5 tokens.
Report #1
Original List
Median household income in Maryland
------------------------------------------------------------------------------------------------------------------------
Line National Median household Median household Income
Num- Rank Maryland income in 2000 income in 1990 percent
ber 2000 1990 County (in dollars) (in dollars) change
------------------------------------------------------------------------------------------------------------------------
1. 31 28 Calvert 65945 61795 6.7%
2. 849 670 Caroline 38832 36030 7.8%
3. 2123 1889 Allegany 30821 27967 etc.
4. 1522 1126 Dorchester 34077 32349 .
5. 72 84 Frederick 60276 53714 .
6. 1813 1570 Garrett 32238 29507
7. 60 45 AnneArundel 61768 58601
8. 2282 1292 BaltimoreCity 30078 31210
10. 189 118 Baltimore 50667 50410
11. 13 8 Montgomery 71551 70208
12. 10 6 Howard 74167 70544
.
. etc. etc. etc.
---------------------------------------------------------------------------------------------------------------------
Total Number of Data Lines Processed = ? .
Sample Output for Page #2
Report #2
Alphabetized List
Median household income in Maryland
------------------------------------------------------------------------------------------------------------------------
Line National Median household Median household Income
Num- Rank Maryland income in 2000 income in 1990 percent
ber 2000 1990 County (in dollars) (in dollars) change
------------------------------------------------------------------------------------------------------------------------
1. 2123 1889 Allegany 30821 27967 ?.?%
2. 60 45 AnneArundel 61768 58601 ?.?%
.
. etc. etc. etc.
---------------------------------------------------------------------------------------------------------------------
Total Number of Data Lines Processed = ? .
Give similar output for 3rd and 4th reports; 3rd report sorted records are listed by rank (lowest numerical number for rank first) and 4th report sorted by % change in descending order.
NOTE: Write one method to print column heading and call the same method every time you need to print headings by passing a single character as a parameter. For example to print heading for original list, call may be printColumnHeadings (‘o’); //o for original.
To print alphabetized list, call may be printColumnHeadings (‘a’); //a for alphabetized.
Similarly call the method for printing other page headings.
//Below is what i've manage to write so far code-wise:
CODE
import java.util.*;
import java.io.*;
public class CensusProblemService
{
final String DASHES="--------------------------------------------------";
int count;
int ranks2000[]=new int[50];
int ranks1990[]=new int[50];
String countyNames[]=new String[50];
long incomes2000[]=new long[50];
long incomes1990[]=new long[50];
public void storeCensusData() throws IOException
{
File dataFile=new File("CensusDataFile.txt");
Scanner scanFile=new Scanner(dataFile);
count=0;
while(scanFile.hasNext() && count<50)
{
int r00=scanFile.nextInt();
int r90=scanFile.nextInt();
String names=scanFile.next();
long i00=scanFile.nextLong();
long i90=scanFile.nextLong();
ranks2000[count]=num00;
ranks1990[count]=num90;
incomes2000[count]=inc00;
incomes1990[count]=inc90;
countyNames[count]=cName;
count++;
}
}
public void columnHeading(char ch)
{
switch(ch)
{
'o': System.out.println("\t\tOriginal List Of Records");
break;
'a': System.out.println("\t\tAlphabetized List Of Records");
break;
'r': System.out.println("\t\tRanked List of Records");
break;
'c': System.out.println("\t\tPercent Changed in Income Descending");
break;
}
System.out.println("\t\tMedian Household Income in Maryland");
System.out.println(DASHES);
System.out.print("Line Number");
System.out.print("\tRank 2000");
System.out.print("\tRank 1990");
System.out.print("\tMedian Income 2000");
System.out.print("\tMedian Income 1990");
System.out.print("\tIncome % Change");
}
public void computePercentChange()//need to somehow store the formula into the array, and maybe type cast
{
double percentChange[]=new double[50];
double percent=(incomes2000-incomes1990)/incomes1990*100;
}
public void printData()
{
int index;
for(index=0; index<count; index++)
{
System.out.println("\t\t" + (index+1) + ".\t" + ranks2000[index] + "\t\t" + ranks1990[index]
+ "\t\t" + countyNames[index] + "\t\t" + incomes2000[index]
+"\t\t" + incomes1990[index]);
System.out.println("\n\t\tTotal Number of Lines Processed= " + count);
System.out.println("\t" + DASHES);
}
}
void alphabetize() //this method alphabetizes county names, gives appropriate heading, calls printData()
{
int i, j, temp1, temp2, temp4, temp5;
String temp3;
for(i=0; i<count-1; i++)
for(j=i+1; j<count; j++)
if((countyNames[i].compareTo(countyNames[j])>0))
{
temp3=countyNames[i];
countyNames[i]=countyNames[j];
countyNames[j]=temp3;
temp1=ranks2000[i];
ranks2000[i]=ranks2000[j];
ranks2000[j]=temp1;
temp2=ranks1990[i];
ranks1990[i]=ranks1990[j];
ranks1990[j]=temp2;
temp4=incomes2000[i];
incomes2000[i]=incomes2000[j];
incomes2000[j]=temp4;
temp5=incomes1990[i];
incomes1990[i]=incomes1990[j];
incomes1990[j]=temp5;
}
}
void sortByRanks()
{
int i, j, temp1, temp2, temp4, temp5;
String temp3;
for(i=0; i<count-1; i++)
for(j=i+1; j<count; j++)
if(ranks2000[i]<ranks2000[j]])
{
temp1=ranks2000[i];
ranks2000[i]=ranks2000[j];
ranks2000[j]=temp1;
temp3=countyNames[i];
countyNames[i]=countyNames[j];
countyNames[j]=temp3;
temp2=ranks1990[i];
ranks1990[i]=ranks1990[j];
ranks1990[j]=temp2;
temp4=incomes2000[i];
incomes2000[i]=incomes2000[j];
incomes2000[j]=temp4;
temp5=incomes1990[i];
incomes1990[i]=incomes1990[j];
incomes1990[j]=temp5;
}
}
void sortByPercentChange()
{
//totally lost on what to do for this method!
//supposed to sort by percent change in incomes in descending order
}
void topOfPageInfo()
{
final String MY_NAME = "Jake Reibert";
final String DASHES = "---------------------------------------------------";
System.out.println(DASHES + "\n\t\t\tName: " + MY_NAME);
System.out.println("\t\t\tProject#2");
System.out.println("\t\t\tDue Date: 07/09/2007");
GregorianCalendar today = new GregorianCalendar();
System.out.println("\n\t\t\tToday's Date: "
+ (today.get(Calendar.MONTH)+1)
+ '/' + today.get(Calendar.DAY_OF_MONTH)
+ '/' + today.get(Calendar.YEAR));
System.out.println("\t\t\tThe Time is Now: " + (today.get(Calendar.HOUR))
+ ':' + (today.get(Calendar.MINUTE))
+ ':' + (today.get(Calendar.SECOND)));
System.out.println ("\t" + DASHES + "\n");
}
}
//Below is the data contained in the file that the program uses, although it looks messy when posted for some reason
31 28 Calvert 65945 61795
849 670 Caroline 38832 36030
2123 1889 Allegany 30821 27967
1522 1126 Dorchester 34077 32349
72 84 Frederick 60276 53714
1813 1570 Garrett 32238 29507
60 45 AnneArundel 61768 58601
2282 1292 BaltimoreCity 30078 31210
189 118 Baltimore 50667 50410
13 8 Montgomery 71551 70208
10 6 Howard 74167 70544
74 75 Carroll 60021 55007
194 175 Cecil 50510 46753
55 32 Charles 62199 60247
97 82 Harford 57234 54101
742 459 Kent 39869 39075
120 66 PrinceGeorge's 55256 50869
98 111 QueenAnne's 57037 50869
127 142 St.Mary's 54706 48231
2309 1429 Somerset 29903 30346
447 308 Talbot 43532 41387
665 492 Washington 40617 38462
828 594 Wicomico 39035 37009
662 690 Worcester 40650 35807