I have just enrolled in my first Computer Science course this spring and encountered a problem with completing these codes.
I have one main and four classes. The object of the program is to create a program that gives the user information about the popularity of different names from the years 1900 to 2000. Given a file with All of the information.
I have to:
Display all the rankings from 1900-2000 for a single name given
Display a competition between two names through the years 1900-2000 and tell which was more popular.
Find and sort the Top 50 names for a year by rank.
Here are my different codes so far.
this is my main code:
import javax.swing.*;
import java.io.*;
public class PA6JBER
{
public static void main(String [] args)
{
String input = JOptionPane.showInputDialog(null, "What is the name of the file you would like to input?");
Processor myProcessor = new Processor();
myProcessor.readData();
int option = menu();
switch(option)
{
case 1:
double Rankings = myProcessor.Rankings();
JOptionPane.showMessageDialog(null, " rankings for that name from 1900 - 2000 are " +Rankings ); //add method that reads the rankings and call
break;
case 2:
String Competition = myProcessor.Competition();
JOptionPane.showMessageDialog(null, "The winner of that year between the two names is" +Competition );//add method that compares
break;
case 3:
int top50 = myProcessor.Top50();
JOptionPane.showMessageDialog(null, "Here is a list of the top 50 names for that year:" + top50); //top 50 method
break;
case 4:
JOptionPane.showMessageDialog(null, "Goodbye and thank you for using this program" );
break;
default:
JOptionPane.showMessageDialog(null, "Goodbye");
}
option = menu();
}
private static void announce()
{
JOptionPane.showMessageDialog(null,"This program will process the file names according to your choice.");
}
private static int menu()
{
String option = JOptionPane.showInputDialog(null,"Please enter what you would like to do:\n"
+ "1: Find the rankings from 1900 - 2000 for a single name\n" + "2: Compare the rankings between two names and see which was more popular\n"
+ "3: Find the top 50 names for a single year\n" + "4: End program");
return Integer.parseInt(option);
}
}
This is my Names Class:
import javax.swing.*;
import java.io.*;
public class Names
{
double Year1;
double Year2;
double Year3;
double Year4;
double Year5;
double Year6;
double Year7;
double Year8;
double Year9;
double Year10;
double Year11;
String [] names;
String name;
double [] rankings;
String names(String n, double y1, double y2, double y3, double y4, double y5, double y6, double y7, double y8, double y9, double y10, double y11)
{
String name = n;
Year1 = y1;
Year2 = y2;
Year3 = y3;
Year4 = y4;
Year5 = y5;
Year6 = y6;
Year7 = y7;
Year8 = y8;
Year9 = y9;
Year10 = y10;
Year11= y11;
}
public String names(String n)
{
name = n;
names = new String[11]; //empty array of grades
}
public String getName()
{
return name;
}
public double getRanking(int num)
{
return rankings[num];
}
public double getYear1()
{
return Year1;
}
public double getYear2()
{
return Year2;
}
public double getYear3()
{
return Year3;
}
public double getYear4()
{
return Year4;
}
public double getYear5()
{
return Year5;
}
public double getYear6()
{
return Year6;
}
public double getYear7()
{
return Year7;
}
public double getYear8()
{
return Year8;
}
public double getYear9()
{
return Year9;
}
public double getYear10()
{
return Year10;
}
public double getYear11()
{
return Year11;
}
public String toString()
{
return Year1 + "" + Year2 + "" + Year3 + "" +Year4 + "" + Year5 + "" + Year6 + "" + Year7 + "" + Year8 + "" + Year9 + "" + Year10 + "" + Year11 + "";
}
public String Names(String name,double year1,double year2,double year3,double year4,double year5,double year6,double year7,double year8,double year9,double year10,double year11)
{
}
public int compareToRank(Names s, int rank)
{
int index=rank-1; //since index starts at 0, but test starts at 1
/* compare the 2 test scores */
if(this.rankings [index] == s.rankings [index])
return 0;
else if(this.rankings [index] > s.rankings [index])
return 1;
else
return -1;
}
}
This is my Processor Class
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class Processor
{
public void readData()
{
File myFile; //file to read from
int names;
Names [] myNames;
int count = 0;
final int NUM_NAMES = 11; //number of grades to read & store
int index = 0; //where in the student array to insert next
String name; //declare name variable to hold name that is read from file
double[] names3 = new double[NUM_NAMES];
double year1 = 0;
double year2 = 0;
double year3 = 0;
double year4 = 0;
double year5 = 0;
double year6 = 0;
double year7 = 0;
double year8 = 0;
double year9 = 0;
double year10 = 0;
double year11 = 0;
try{
Scanner scan = new Scanner(myFile);
/* read and store all data */
while(scan.hasNext())
{
/* read in each line */
name = scan.next(); //read in name
year1 = scan.nextDouble();
year2 = scan.nextDouble();
year3 = scan.nextDouble();
year4 = scan.nextDouble();
year5 = scan.nextDouble();
year6 = scan.nextDouble();
year7 = scan.nextDouble();
year8 = scan.nextDouble();
year9 = scan.nextDouble();
year10 = scan.nextDouble();
year11 = scan.nextDouble();
/* add to array */
myNames[index] = new Names(name,year1,year2,year3,year4,year5,year6,year7,year8,year9,year10,year11);
index++;
}//end of while
//save number of students
numNames = index;
}//end of try
catch(IOException e)
{
System.err.println("Error opening file. Ending program.");
System.exit(1);
}
}
public String Competition()
{
int Count1 = 0;
int Count2 = 0;
int Count3 = 0;
string name1 = JOptionPane.ShowInputDiolog(null,"What is the name of the first person?");
string name2 = JOptionPane.ShowInputDiolog(null,"What is the name of the second person?");
/* create Student objects for comparison */
names n1 = new names(name1);
names n2 = new names(name2);
/* find their indices */
int i1 = SearchName.binSearch(myNames,n1,myNames.length);
int i2 = SearchName.binSearch(myNames,n2,myNames.length);
while(Count1<11)
{
if(myNames[i1].getRanking(Count1) > myNames[i2].getRanking(Count1))
{
Count2++;
}
else
if(myNames[i1].getRanking(Count1) < myNames[i2].getRanking(Count1))
{
Count3++;
}
else
{
return JOptionPane.ShowMessageDialog(null, "That is an invalid name");
}
Count1++;
}
if(Count2>Count3)
{
JOptionPane.ShowMessageDialog(null, Name1 + "is more popular overall");
}
else
{
JOptionPane.ShowMessageDialog(null, Name2 + "is more popular overall");
}
}
public int Top50()
{
names [] Top50 = new names[11];
int count=0;
/* look through entire student array and only remember those who got an A on test "test" */
for(int i=0; i<numNames; i++)
{
if(count < 50)
{
Top50[count] = myNames[i];
count++;
}
}
/* sort list of A students by grade */
SorterNames.selectionSort(Top50,count,rank);
/* output list of A students to System.out.println */
for(int i=0; i<count; i++)
{
JOptionPane.showInputDialog(null, "Here are the Top 50 Names");
System.out.println(Top50[i]);
}
}
public double Rankings()
{
Names Methods = new Names();
String rank = JOptionPane.showInputDialog(null, "What is the name you want to see the rankings of?");
int tempIndex = SearchName.binSearch(myNames,rank, myNames.length);
if( tempIndex != -1)
{
return myNames[index].toString();
}
}
}
//get rank method
//returns ranks(#)
Search Name Class
import javax.swing.*;
import java.io.*;
public class SearchName
{
// return the index of target in the array which contains
// numbers are indices 0..num-1. Return -1 if traget is not found.
public static double binSearch( names [] array, names target, double num)
{
int startIndex = 0;
int endIndex = num - 1;
int middle;
while ( endIndex >= startIndex )
{
middle = ( startIndex + endIndex )/ 2; //find new middle spot to look at
if (array[middle].equals(target))
return middle; //we found it!
else if ( array[middle].compareTo(target)>0 ) //if we didn't find it, change end or start
endIndex = middle - 1;
else
startIndex = middle + 1;
}
return -1; // if we reached this line we didn't find it
}
}
And my sorter Names class
import javax.swing.*;
import java.io.*;
// A class with a static sort method
public class SorterNames
{
/** Uses Selection Sort to sort
* an integer array in ascending order
* @param array the array to sort
* @param n is the number of values in array (at indices 0..n-1)
*/
public static void selectionSort( myNames [] array, int n, int rank )
{
Names temp; // temporary location for swap
int max; // index of maximum value in subarray
for ( int i = 0; i < n; i++ )
{
// find index of largest value in subarray
max = indexOfLargestElement( array, n - i, test );
// swap array[max] and array[n ñ i - 1]
temp = array[max];
array[max] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
/** Finds index of largest element
* @param size the size of the subarray
* @ return the index of the largest element in the subarray
*/
private static int indexOfLargestElement( myNames [] array, int size, int rank )
{
int index = 0;
for( int i = 1; i < size; i++ )
{
if ( array[i].compareToRank(array[index],rank) > 0 )
index = i;
}
return index;
}
}
I am having certain compile errors for most of these and any help or tips would be very appreciated.

New Topic/Question
Reply



MultiQuote



|