java - A txt file with scores(92 92 92 92 92) get assigned all A's, but with scores(92 10 70 95 84) 92 and 95 get assigned 'B'? -
so need debugging code. problem code incorrect grades assign each score in array. file reads in txt file include first name[space]last name[tab]score. stores them in arrays, calculates mean , standard deviation of scores , should assign letter grades scores, thing if rewrite txt file (92 92 92 92 92) assigned 'a' right, when txt file is(92 10 70 95 84) 92 , 95 assigned 'b' instead of 'a', problem. have spent hours trying find out why this, have had no luck. how calculate letter grades:
a = mean + standard <= score, b = mean + (standard/3) <= score < mean + standard, c = mean - (standard/3) <= score < mean + (standard/3), d = mean - standard <= score < mean - (standard/3), f = score < mean - standard
import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class scannerreadfilesplit { public static int[] scores = new int[5]; public static string[] names = new string[5]; public static char[] grades = new char[5]; public static int mean = 0; public static double standard = 0.0; public static void main(string[] args) { readdata(); mean = fndmean(); standard = fndstandard(); for(int x = 0; x < scores.length; x++) { if(mean + standard <= scores[x]) { grades[x] = 'a'; } if( (mean + (standard/3) <= scores[x]) && (scores[x] < mean + standard)) { grades[x] = 'b'; } if( (mean - (standard/3) <= scores[x]) && (scores[x] < mean+(standard/3))) { grades[x] = 'c'; } if( (mean - standard <= scores[x]) && (scores[x] < mean - (standard/3))) { grades[x] = 'd'; } if(scores[x] < mean - standard) { grades[x] = 'f'; } } system.out.printf("%-22s%-22s%-22s\n", "names", "scores", "grade"); for(int k=0; k<5; k++) { system.out.printf("%-22s%-22d%-22c\n", names[k], scores[k], grades[k]); } system.out.println(); } //method called readdata() reads input file , stores names //in array called names , scores in array called scores. * each names has //to seperate line , scores have seperated tab. public static void readdata() { file file = new file("namesscore.txt"); int = 0; try { scanner scanner = new scanner(file); while (scanner.hasnextline()) { string line = scanner.nextline(); string [] words = line.split("\t"); names[i] = words[0]; scores[i] = integer.parseint(words[1]); i++; } } catch (filenotfoundexception e) { e.printstacktrace(); system.exit(0); } } //method called fndmean() goes through scores array, adds elements //and divides sum number of elements, stores value //in variable mean1 returns value. public static int fndmean() { int mean1 = 0; int sum = 0; for(int j = 0; j < scores.length; j++) { sum += scores[j]; } mean1 = (sum/(scores.length)); system.out.printf("the mean of scores is: %d\n", mean1); return mean1; } //method called fndstandard() finds standard deviation of scores using //the following formula. value stores in variable standarddeviation1 //returns value public static double fndstandard() { double sd = 0; for(int = 0; < scores.length; i++) { sd += ((scores[i] - mean) * (scores[i] - mean)) / (scores.length - 1); } double standarddeviation1 = math.sqrt(sd); system.out.printf("the standard deviation is: %.2f\n", standarddeviation1); return standarddeviation1; } }
for second case, have mean of 52.2 , std of ~40. 92, (mean + (standard/3) <= scores[x]) && (scores[x] < mean + standard))
computes 52.2+40/3<92 , 92<52.2+40 assigned b, looks correct.
Comments
Post a Comment