/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package test2prob2e; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; /** * * @author jcmtiernan */ public class Test2Prob2e { public static void main(String[] args) throws IOException //throws FileNotFoundException { final int MAXTEAMS = 150; final int TEAM_NUMBER = 0; final int RANKING_PTS = 1; final int QUALIFYING_PTS = 2; final int TEAM_NUM_MAX = 12000; final int RP_MAX = 90; // assume 45 matches given 2 RP for WIN, 1 RP for tie final int QP_MAX = 12375; // assume max of 275 points per match for 45 matches Integer robotTeamNumbers[] = new Integer[MAXTEAMS]; Integer[][] robotTeamInfo = new Integer[MAXTEAMS][3]; String[][][] regionRobotTeams = new String[4][MAXTEAMS][3]; Scanner input = new Scanner(System.in); String inputFileName = "TeamRPQP.dat"; //Scanner inputFile = new Scanner(file); /* System.out.println("Hello and welcome to the Robot Team Manager system."); System.out.println("Your input data should be in a file. Please enter the name of the file here: "); String inputFileName = input.next(); */ File inputFile = new File(inputFileName); //File file = new File(dataFile); //inputFile = makeRandomTeamRPQPFile(inputFile, MAXTEAMS, TEAM_NUM_MAX, RP_MAX, QP_MAX); // creates test data file Scanner inFile; // exception handling try { inFile = new Scanner(inputFile); } catch (FileNotFoundException fnf) { System.out.println("Input file was not found. Input will be read from the keyboard."); System.out.println("Enter a team number followed by a space then the number of team members " + "followed by a space then the year the team started followed by a space or the enter key."); System.out.println("Enter a 0 for the team number in order to stop."); System.out.println("Team# RankingPt QualPt : "); inFile = new Scanner(System.in); // does not handle invalid data from the keyboard } int i = 0; int j = 0; boolean isGreaterThanZero = true; Integer tempNumber = 0; /* System.out.println("We need to first get the numbers of all the robot teams to be managed."); System.out.println("Please enter multiple team numbers on one line seperated by spaces. "); System.out.println("When you have finished entering all team numbers, enter a 0 or a negative number to stop."); */ //while(robotTeamNumbers[i-1] > 0); //System.out.println("The array is size "+robotTeamNumbers.length); System.out.println(); j = 0; while( inFile.hasNextInt() ) { //System.out.println("robotTeamNumbers["+j+"] = "+robotTeamNumbers[j]); robotTeamInfo[j][TEAM_NUMBER] = inFile.nextInt(); //System.out.println("Reading team data from file."); robotTeamInfo[j][RANKING_PTS] = inFile.nextInt(); robotTeamInfo[j++][QUALIFYING_PTS] = inFile.nextInt(); /* System.out.println("robotTeamInfo[j][MEMBER_COUNT] is "+robotTeamInfo[j][MEMBER_COUNT] +" and robotTeamInfo[j][FIRST_YEAR] "+robotTeamInfo[j][FIRST_YEAR]); */ /* System.out.println("Please enter the number of members on team "+robotTeamInfo[j][0]+": "); robotTeamInfo[j][MEMBER_COUNT] = input.nextInt(); // assume user gives good data System.out.println("Please enter the first year team "+robotTeamInfo[j][0]+" competed in robotics: "); robotTeamInfo[j][FIRST_YEAR] = input.nextInt(); // assume user gives good data */ } int numTeamsInFile = j; //System.out.println("The file had total of "+i+" lines of data"); System.out.printf("%8s %5s %8s\n","Team #","RP","QP"); for (j = 0; (j < numTeamsInFile); j++ ) { System.out.printf("%8d %5d %8d\n", robotTeamInfo[j][TEAM_NUMBER], robotTeamInfo[j][RANKING_PTS], robotTeamInfo[j][QUALIFYING_PTS]); // assume user gives good data } i= 1; j= 2; System.out.println(); System.out.println(); //System.out.println("robotTeamInfo["+j+"]["+i+"] is "+robotTeamInfo[j][i]); int maxRP = 0; for (int k=0; k < robotTeamInfo.length; k++) { if (maxRP < robotTeamInfo[k][RANKING_PTS]) { maxRP = robotTeamInfo[k][RANKING_PTS]; } } System.out.println("The maximum score is "+maxRP); for (i = 0; i < robotTeamInfo.length; i++) { if (maxRP == robotTeamInfo[i][RANKING_PTS]) { System.out.println("robotTeamInfo["+i+"] has the maximum RP of "+maxRP +" with QP of "+robotTeamInfo[i][QUALIFYING_PTS]); } } System.out.println("There are "+numTeamsInFile+" teams in the array."); //sort this data System.out.println(); System.out.println(); //compareScores(testScores); File outFile = new File("OutputTeamRPQP.txt"); PrintWriter output = new PrintWriter(outFile); output.printf("%8s %5s %8s\n","Team #","RP","QP"); for (j = 0; (j < numTeamsInFile); j++ ) { output.printf("%8d %5d %8d\n", robotTeamInfo[j][TEAM_NUMBER], robotTeamInfo[j][RANKING_PTS], robotTeamInfo[j][QUALIFYING_PTS]); // assume user gives good data robotTeamNumbers[j] = robotTeamInfo[j][TEAM_NUMBER]; } output.close(); int index; int tempRP, tempQP, tempNum; int swap = 1; int numberOfCompares = numTeamsInFile-1; int ifCount = 0; for (j = 0; ((j < numTeamsInFile) && (swap > 0)); j++, numberOfCompares-- ) // don't elements already in order { //System.out.println("At loop "+j+" swap is "+swap); swap = 0; for (index = 0; (index < numberOfCompares); index++ ) { // compare element i to i+1 ifCount++; if (robotTeamInfo[index][RANKING_PTS] < robotTeamInfo[index+1][RANKING_PTS]) { swap2D(robotTeamInfo, index, RANKING_PTS, QUALIFYING_PTS, TEAM_NUMBER); swap++; } else if (robotTeamInfo[index][RANKING_PTS] == robotTeamInfo[index+1][RANKING_PTS]) { if (robotTeamInfo[index][QUALIFYING_PTS] < robotTeamInfo[index+1][QUALIFYING_PTS]) { swap2D(robotTeamInfo, index, RANKING_PTS, QUALIFYING_PTS, TEAM_NUMBER); swap++; } } } } System.out.println("In RPQP sort, if statement ran "+ifCount+" times"); System.out.println(); System.out.println("After sort by RP and QP"); System.out.printf("%8s %5s %8s\n","Team #","RP","QP"); for (j = 0; (j < numTeamsInFile); j++ ) { System.out.printf("%8d %5d %8d\n", robotTeamInfo[j][TEAM_NUMBER], robotTeamInfo[j][RANKING_PTS], robotTeamInfo[j][QUALIFYING_PTS]); // assume user gives good data } sortTmNum(robotTeamInfo, numTeamsInFile); System.out.println(); System.out.println("After sort by team name"); System.out.printf("%8s %5s %8s\n","Team #","RP","QP"); for (j = 0; (j < numTeamsInFile); j++ ) { System.out.printf("%8d %5d %8d\n", robotTeamInfo[j][TEAM_NUMBER], robotTeamInfo[j][RANKING_PTS], robotTeamInfo[j][QUALIFYING_PTS]); // assume user gives good data } /* for (j = 0; (j < numTeamsInFile); j++ ) { System.out.printf("In location ["+j+"] the value us %8d\n",robotTeamNumbers[j]); } // assume user gives good data // simple bubble int index; int temp; for (j = 0; (j < numTeamsInFile); j++ ) for (index = 0; (index < (numTeamsInFile-1)); index++ ) // compare element i to i+1 if (robotTeamNumbers[index] > robotTeamNumbers[index+1]) { temp = robotTeamNumbers[index]; robotTeamNumbers[index] = robotTeamNumbers[index+1]; robotTeamNumbers[index+1] = temp; } System.out.println(); System.out.println("After sort"); for (j = 0; (j < numTeamsInFile); j++ ) { System.out.printf("In location ["+j+"] the value us %8d\n",robotTeamNumbers[j]); } */ } /** * @param file the data file to put the new random data into * @param maxLines the maximum lines of data to put in the file * @param rangeRP the maximum range of the numeric ranking point values being put in the file * @param qualRP the maximum range of the numeric qualifying point values being put in the file */ public static File makeRandomTeamRPQPFile(File file, int maxLines, int rangeTeamNum, int rangeRP, int rangeQP) throws IOException { FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); int teamNum, randRP, randQP; Random randGen = new Random(); //creating Random number generator for(int k = 0; k < maxLines; k++) { teamNum = randGen.nextInt(rangeTeamNum); randRP = randGen.nextInt(rangeRP); // notice that two values are being printed randQP = randGen.nextInt(rangeQP); bw.write(String.format("%8d%8d%8d", teamNum, randRP,randQP)); bw.newLine(); } bw.close();// be sure to close BufferedWriter return file; } public static void swap2D(Integer[][] robotTeamInfo, int index, int RANKING_PTS, int QUALIFYING_PTS, int TEAM_NUMBER) { int tempRP, tempQP, tempNum; tempRP = robotTeamInfo[index][RANKING_PTS]; robotTeamInfo[index][RANKING_PTS] = robotTeamInfo[index+1][RANKING_PTS]; robotTeamInfo[index+1][RANKING_PTS] = tempRP; tempQP = robotTeamInfo[index][QUALIFYING_PTS]; robotTeamInfo[index][QUALIFYING_PTS] = robotTeamInfo[index+1][QUALIFYING_PTS]; robotTeamInfo[index+1][QUALIFYING_PTS] = tempQP; tempNum = robotTeamInfo[index][TEAM_NUMBER]; robotTeamInfo[index][TEAM_NUMBER] = robotTeamInfo[index+1][TEAM_NUMBER]; robotTeamInfo[index+1][TEAM_NUMBER] = tempNum; } public static void sortTmNum(Integer[][] robotTeamInfo, int numTeamsInFile) { final int TEAM_NUMBER = 0; final int RANKING_PTS = 1; final int QUALIFYING_PTS = 2; // make multiple passes through array for (int j = 0; j < numTeamsInFile; j++) // make one pass of comparisons through array for (int i = 0; i < numTeamsInFile-1; i++) if (robotTeamInfo[i][TEAM_NUMBER] > robotTeamInfo[i+1][TEAM_NUMBER]) { swap2D(robotTeamInfo, i, RANKING_PTS, QUALIFYING_PTS, TEAM_NUMBER); } System.out.println("In Tm Num sort, if statement ran "+(numTeamsInFile*(numTeamsInFile-1))+" times"); } }