Created
June 11, 2012 22:00
-
-
Save ErisianArchitect/2913013 to your computer and use it in GitHub Desktop.
Rhymey's homework
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ClassGrades | |
{ | |
class Program | |
{ | |
public class Student | |
{ | |
public Student() | |
{ | |
Grades = new int[3]; | |
} | |
public Student(int classCount) | |
{ | |
Grades = new int[classCount]; | |
} | |
int[] Grades; | |
public void CollectGrades() | |
{ | |
string input; | |
int num; | |
for (int i = 0; i < Grades.Length; i++) | |
{ | |
do | |
{ | |
Console.Write("Enter grade for class {0}: ", i + 1); | |
input = Console.ReadLine(); | |
} while (!int.TryParse(input, out num)); | |
Grades[i] = num; | |
} | |
} | |
public int GetAverage() | |
{ | |
return (int)((float)GetTotal() / (float)Grades.Length); | |
} | |
public int GetTotal() | |
{ | |
int total = 0; | |
for (int i = 0; i < Grades.Length; i++) | |
{ | |
total += Grades[i]; | |
} | |
return total; | |
} | |
public void PrintGrades() | |
{ | |
for (int i = 0; i < Grades.Length; i++) | |
{ | |
Console.WriteLine("Class {0}: {1}", i + 1, Grades[i]); | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Student a = new Student(3);//Replace 3 with the number of classes | |
Student b = new Student(3); | |
Console.WriteLine("Input grades of student 1:"); | |
a.CollectGrades(); | |
Console.WriteLine(); | |
Console.WriteLine("Input grades of student 2:"); | |
b.CollectGrades(); | |
Console.WriteLine(); | |
Console.WriteLine("Student 1 grades:"); | |
a.PrintGrades(); | |
Console.WriteLine(); | |
Console.WriteLine("Student 1 total: {0}\nStudent 1 average: {1}", a.GetTotal(), a.GetAverage()); | |
Console.WriteLine(); | |
Console.WriteLine("Student 2 grades:"); | |
b.PrintGrades(); | |
Console.WriteLine(); | |
Console.WriteLine("Student 2 total: {0}\nStudent 2 average: {1}", b.GetTotal(), b.GetAverage()); | |
Console.WriteLine(); | |
Console.WriteLine("Overall total: {0}\nOverall average: {1}", a.GetTotal() + b.GetTotal(), (int)(((float)a.GetAverage() + (float)b.GetAverage()) / 2.0f)); | |
Console.ReadKey(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment