Created
December 12, 2015 01:32
-
-
Save RascalTwo/00a57773cc7adcdfce0b to your computer and use it in GitHub Desktop.
Exercise 102 from Programming Part 1 from http://mooc.fi/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class ExamScores{ | |
private ArrayList<Integer> examScores; | |
private int[] gradeDistribution; | |
public static void main(String[] args){ | |
ExamScores es = new ExamScores(34, 41, 53, 36, 55, 27, 43, 40); | |
es.printDistribution(); | |
es.printAcceptancePercentage(); | |
} | |
public ExamScores(int... scores){ | |
examScores = new ArrayList<Integer>(); | |
for (int score : scores){ | |
addScore(score); | |
} | |
gradeDistribution = new int[6]; | |
} | |
public void addScore(int score){ | |
examScores.add(score); | |
} | |
public void printScores(){ | |
for (Integer score : examScores){ | |
System.out.println(score); | |
} | |
} | |
public void howManyOfEachDistribution(){ | |
gradeDistribution = new int[6]; | |
for (int i = 0; i < examScores.size(); i++){ | |
if (50 <= examScores.get(i) && examScores.get(i) <= 60) { | |
gradeDistribution[5]++; | |
} | |
else if (45 <= examScores.get(i) && examScores.get(i) <= 49) { | |
gradeDistribution[4]++; | |
} | |
else if (40 <= examScores.get(i) && examScores.get(i) <= 44) { | |
gradeDistribution[3]++; | |
} | |
else if (35 <= examScores.get(i) && examScores.get(i) <= 39) { | |
gradeDistribution[2]++; | |
} | |
else if (30 <= examScores.get(i) && examScores.get(i) <= 34) { | |
gradeDistribution[1]++; | |
} | |
else if (0 <= examScores.get(i) && examScores.get(i) <= 29) { | |
gradeDistribution[0]++; | |
} | |
} | |
} | |
public void printSingleDistribution(int arrayPlace){ | |
for (int j = 0; j < gradeDistribution[arrayPlace]; j++){ | |
System.out.print("*"); | |
} | |
} | |
public void printDistribution(){ | |
howManyOfEachDistribution(); | |
for (int x = 5; x >= 0; x--){ | |
System.out.print(x + ": "); | |
printSingleDistribution(x); | |
System.out.println(); | |
} | |
} | |
public void printAcceptancePercentage(){ | |
int total = 0; | |
for (int examScore : examScores){ | |
if (examScore > 29) { | |
total++; | |
} | |
} | |
System.out.println("Acceptance percentage: " + (double) total / examScores.size() * 100); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class ExamScores{ | |
private ArrayList<ArrayList<Integer>> scores; | |
public static void main(String[] args){ | |
ExamScores es = new ExamScores(34, 41, 53, 36, 55, 27, 43, 40); | |
es.printDistribution(); | |
es.printAcceptancePercentage(); | |
} | |
public ExamScores(int... scores){ | |
this.scores = new ArrayList<ArrayList<Integer>>(); | |
for (int i = 0; i < 6; i++){ | |
this.scores.add(new ArrayList<Integer>()); | |
} | |
for (int score : scores){ | |
addScore(score); | |
} | |
} | |
public void addScore(int score){ | |
if (50 <= score && score <= 60) { | |
scores.get(5).add(score); | |
} | |
else if (45 <= score && score <= 49) { | |
scores.get(4).add(score); | |
} | |
else if (40 <= score && score <= 44) { | |
scores.get(3).add(score); | |
} | |
else if (35 <= score && score <= 39) { | |
scores.get(2).add(score); | |
} | |
else if (30 <= score && score <= 34) { | |
scores.get(1).add(score); | |
} | |
else if (0 <= score && score <= 29) { | |
scores.get(0).add(score); | |
} | |
} | |
public void printScores(){ | |
for (ArrayList<Integer> scoreList : scores){ | |
for (int score : scoreList){ | |
System.out.println(score); | |
} | |
} | |
} | |
public void printSingleDistribution(int grade){ | |
for (int j = 0; j < scores.get(grade).size(); j++){ | |
System.out.print("*"); | |
} | |
} | |
public void printDistribution(){ | |
System.out.println(scores); | |
for (int x = 5; x >= 0; x--){ | |
System.out.print(x + ": "); | |
printSingleDistribution(x); | |
System.out.println(); | |
} | |
} | |
public void printAcceptancePercentage(){ | |
int validScores = 0; | |
int invalidScores = 0; | |
for (ArrayList<Integer> scoreList : scores){ | |
if (scores.indexOf(scoreList) != 0) { | |
validScores += scoreList.size(); | |
} | |
else{ | |
invalidScores += scoreList.size(); | |
} | |
} | |
System.out.println("Acceptance percentage: " + (double) validScores / (validScores + invalidScores) * 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment