using System; using System.Collections.Generic; using System.Text; /********************************* * this class takes in any number of test * scores and writes them to an array. * Class methods return the average and * the gradepoint * steve conger 11/6/2008 * *******************************/ namespace LoopsandArrays { class TestAverage { //constructor takes in studentID and the number //of test scores the user wants to enter public TestAverage(string stid, int numOfScores) { //the studentid is assigned to the private field //the property only has a get, so //that makes this a read only value studentid = stid; //set the number of scores NumberofScores = numOfScores; //initialize the array to the length the user //passed in Scores = new double[NumberofScores]; } //this declares an array of doubles //to store the scores double[] scores; //a property encaspulating the score array public double[] Scores { get { return scores; } set { scores = value; } } string studentid; //read only property public string Studentid { get { return studentid; } } int numberofScores; public int NumberofScores { get { return numberofScores; } set {numberofScores = value; } } //public methods public double CalculateAverage() { double average = 0; double sum = 0; //a foreach loop to loop through //all the double elements in the array foreach (double s in Scores) { sum += s; //add them all to sum } //divide by the length of the array to //get the average average = sum / Scores.Length; return average; } public double GradePoint() { double gp = 0; //call the average method to make sure //it has run before assessing the grade point double avg = CalculateAverage(); if (avg >= 90) { gp = 4.0; } else if (avg >= 80) { gp = 3.0; } else if (avg >= 70) { gp = 2.0; } else if (avg >= 60) { gp = 1.0; } else { gp = 0; } return gp; } } }