Assignment 1B - Scorelist Flashcards
How would you make a private DeepCopy method?
private int[] deepCopy(int a[]) { int[] temp = new int[a.length]; for (int k = 0; k < a.length; ++k) { temp[k] = a[k]; } return temp; }
create a ScoreList default constructor that has a quantity parameter between 0 and 100 with random integers.
public ScoreList(int quantity) { Random rand = new Random(); scores = new int[quantity];
int max = 100; for(int i = 0; i < scores.length; i++) { scores[i] = rand.nextInt(max +1); } }
Given the following javadoc information, create a
ScoreList constructor that utilizes the DeepCopy method:
/** * Constructor with Student Grades Provided * Initilized a nrely created ScoreList object initializing its scores array using the supplied array. * @param students, an array of student grades * /
public ScoreList(int students[]) { scores = deepCopy(students); }
Create an accessor method using deepCopy and Scores.
public int[] getScores() { return deepCopy(scores); }
Create an mutator method using deepCopy and Scores.
public ScoreList setScores(int scores[]) { this.scores = deepCopy(scores); return this; }
Create a toString to list the scores.
@Override public String toString() { String gradeString = ""; for (int i = 0; i < scores.length; i++) gradeString += scores[i] + " "; return gradeString; }
Create an equals method to compare the scores.
@Override public boolean equals(Object o) { if(o instanceof ScoreList) { return Arrays.equals(scores, ((ScoreList)o).scores); } return false; }
Create a method that inputs an array of scores and returns letter grades.
public char[] getLetterGrades() { char[] letterGrades = new char[scores.length]; int i=0; for(int m : scores) { if(m >= 90) letterGrades[i] = 'A'; else if(m >= 80) letterGrades[i] = 'B'; else if(m >= 70) letterGrades[i] = 'C'; else if(m >= 60) letterGrades[i] = 'D'; else letterGrades[i] = 'F'; i++; } return letterGrades; }
Create a sortScores method
public int[] sortScores()
{
for(int i=0; i
create a highestScore method
public int highestScore () { int max = scores[0]; for (int i = 1; i < scores.length; i++) { if (scores[i] > max) { max = scores[i]; } } return max; }
create a lowestScore method
public int lowestScore() { int min = scores[0]; for (int i = 0; i < scores.length; i++) { if (scores[i] <= min) { min = scores[i]; } } return min; }
Create a method that outputs an average
public double average() { int length = scores.length; double sum = 0; for(int i = 0; i
Create a median method
public double median() { int n = scores.length; sortScores(); if (n % 2 != 0) return (double)scores[n / 2]; return (double)(scores[(n - 1) / 2] + scores[n / 2]) / 2.0; }
Create a mode method
public double mode() { int []modeArray=new int[101]; int i,mode; for(i=0;imodeArray[mode]) mode=i; return mode; }