blackbinary

04-08-2008, 02:25 AM

Alright, so i've been assigned a review program for my g12 programming class. g11 i programmed in C++, so its very similar. That said, I can't get one particular error out of my program, and it seems the teacher is having just about as much luck. If anyone here could try and fix it, or offer some other kind of help, I'd appreciate it greatly.

import java.io.*;

public class IcsMarkbook

{

public static void methodOutput (int sortedMarks, double median, double average)

{

// method responsible for outputting the data and calculations.

System.out.print ("\t Student \t Mark\n"); // \t tabs, \n goes down a line.

for (int i = 0 ; i < 25 ; i++)

{

System.out.print ("\t " + i + " \t" + sortedMarks [i] + "\n"); // outputs the list of sorted marks, and what cell they are in.

}

System.out.print ("\n \f"); // \f adds a seperator

System.out.print ("\t Median \t Average\n");

System.out.print ("\t " + median + " \t" + average + "\n");

System.out.print ("\n\n\t\t ~~ Mitchell Swirski ~~ ");

}

public static void randMarkGen () throws IOException

{

// 25 integer array, loop cycles through each cell, putting in a random number from 40 to 100. it saves them in marks.txt

int array[] = new int [25];

PrintWriter fileOut = new PrintWriter (new FileWriter ("marks.txt"));

for (int i = 0 ; i < 25 ; i++)

{

array [i] = (int) (Math.random () * 100) + 40; // Goes over 100, why?

fileOut.println (array [i] + "\n");

}

fileOut.close ();

}

public static int sortMarks (int rawMarks[])

{

// the function which uses bubblesort to put the list of marks in decending order from highest to lowest.

for (int pass = 1 ; pass < rawMarks.length - 1 ; pass++) // controls the number of times algorithm analyzes the list

{

for (int element = 0 ; element < rawMarks.length - 1 ; element++) // controls the number of comparisons it will make

{

//below: compares two elements. If the first is smaller then the second, they switch. This means the highest will be in the first cell.

if (rawMarks [element] < rawMarks [element + 1])

{

int hold = rawMarks [element]; // hold is used to assist in swapping the data from one cell to the other.

rawMarks [element] = rawMarks [element + 1];

rawMarks [element + 1] = hold;

}

}

}

return rawMarks; // returns the array to the main. This becomes sortedMarks.

}

public static int markCalcs (int sortedMarks, int trigger)

// does the calculations for median and average of the marks.

{

if (trigger = 1) // if a 1 is sent with the list, then calc the median.

{

double median = sortedMarks [13];

return median; // returns median to the main function.

}

else // If something other than a 1 is sent with the list, then calculate the average.

{

double total = 0; // collective total of the marks.

for (int i = 0 ; i < 25 ; i++)

{

total = total + sortedMarks [i]; //adds all the marks together

}

double average = total / 25; //divides by 25 to get the average

return average; // returns the average to the main program.

}

}

public static void main (String[] args) throws IOException

{

BufferedReader readFile = new BufferedReader (new FileReader ("marks.txt")); // Used to read the file of Random Numbers.

System.out.println ("\t Mitchell Swirski ~ 2008 - ICS Markbook\n");

randMarkGen (); // Calls a random mark generator. Stores 25 marks ranging from 40-100 in a text file called marks.txt. Nothing is returned.

int rawMarks[] = new int [25]; // the array the marks are stored in before they are sorted.

for (int i = 0 ; i < 25 ; i++) // for loop used to cycle from cell 0 of the array to cell 25.

{

rawMarks [i] = Integer.parseInt (readFile.readLine ()); // parses the text file to fill the array

}

int sortedMarks[] = int sortMarks (int rawMarks); // calls the function sortMarks, which takes the unsorted marks and returns them sorted.

// below; calls the markCalcs function, with the list of sorted marks, with 1 or 0 so that markCalcs can decide which to calculate.

double median, average;

int trigger1 = 1, trigger2 = 0;

//median = int markCalcs (int sortedMarks, int trigger1);

//average = int markCalcs (int sortedMarks, int trigger2);

//methodOutput (int sortedMarks, double median, double average)

// Calls the method that outputs median, average, and the list of marks.

}

// End of Program

import java.io.*;

public class IcsMarkbook

{

public static void methodOutput (int sortedMarks, double median, double average)

{

// method responsible for outputting the data and calculations.

System.out.print ("\t Student \t Mark\n"); // \t tabs, \n goes down a line.

for (int i = 0 ; i < 25 ; i++)

{

System.out.print ("\t " + i + " \t" + sortedMarks [i] + "\n"); // outputs the list of sorted marks, and what cell they are in.

}

System.out.print ("\n \f"); // \f adds a seperator

System.out.print ("\t Median \t Average\n");

System.out.print ("\t " + median + " \t" + average + "\n");

System.out.print ("\n\n\t\t ~~ Mitchell Swirski ~~ ");

}

public static void randMarkGen () throws IOException

{

// 25 integer array, loop cycles through each cell, putting in a random number from 40 to 100. it saves them in marks.txt

int array[] = new int [25];

PrintWriter fileOut = new PrintWriter (new FileWriter ("marks.txt"));

for (int i = 0 ; i < 25 ; i++)

{

array [i] = (int) (Math.random () * 100) + 40; // Goes over 100, why?

fileOut.println (array [i] + "\n");

}

fileOut.close ();

}

public static int sortMarks (int rawMarks[])

{

// the function which uses bubblesort to put the list of marks in decending order from highest to lowest.

for (int pass = 1 ; pass < rawMarks.length - 1 ; pass++) // controls the number of times algorithm analyzes the list

{

for (int element = 0 ; element < rawMarks.length - 1 ; element++) // controls the number of comparisons it will make

{

//below: compares two elements. If the first is smaller then the second, they switch. This means the highest will be in the first cell.

if (rawMarks [element] < rawMarks [element + 1])

{

int hold = rawMarks [element]; // hold is used to assist in swapping the data from one cell to the other.

rawMarks [element] = rawMarks [element + 1];

rawMarks [element + 1] = hold;

}

}

}

return rawMarks; // returns the array to the main. This becomes sortedMarks.

}

public static int markCalcs (int sortedMarks, int trigger)

// does the calculations for median and average of the marks.

{

if (trigger = 1) // if a 1 is sent with the list, then calc the median.

{

double median = sortedMarks [13];

return median; // returns median to the main function.

}

else // If something other than a 1 is sent with the list, then calculate the average.

{

double total = 0; // collective total of the marks.

for (int i = 0 ; i < 25 ; i++)

{

total = total + sortedMarks [i]; //adds all the marks together

}

double average = total / 25; //divides by 25 to get the average

return average; // returns the average to the main program.

}

}

public static void main (String[] args) throws IOException

{

BufferedReader readFile = new BufferedReader (new FileReader ("marks.txt")); // Used to read the file of Random Numbers.

System.out.println ("\t Mitchell Swirski ~ 2008 - ICS Markbook\n");

randMarkGen (); // Calls a random mark generator. Stores 25 marks ranging from 40-100 in a text file called marks.txt. Nothing is returned.

int rawMarks[] = new int [25]; // the array the marks are stored in before they are sorted.

for (int i = 0 ; i < 25 ; i++) // for loop used to cycle from cell 0 of the array to cell 25.

{

rawMarks [i] = Integer.parseInt (readFile.readLine ()); // parses the text file to fill the array

}

int sortedMarks[] = int sortMarks (int rawMarks); // calls the function sortMarks, which takes the unsorted marks and returns them sorted.

// below; calls the markCalcs function, with the list of sorted marks, with 1 or 0 so that markCalcs can decide which to calculate.

double median, average;

int trigger1 = 1, trigger2 = 0;

//median = int markCalcs (int sortedMarks, int trigger1);

//average = int markCalcs (int sortedMarks, int trigger2);

//methodOutput (int sortedMarks, double median, double average)

// Calls the method that outputs median, average, and the list of marks.

}

// End of Program