namespace PeriodicGradeCalculator
public static void Main()
GradeCalculator gc = new GradeCalculator(0.5, 0.1, 0.1, 0.3);
gc.AddQuizzes(q1+10, 20);
gc.AddParticipation(p1, 20);
gc.AddParticipation(p2, 20);
gc.AddParticipation(p3, 20);
int exam = (int)((q1 + p1 + p2 + p3 + h1 + h2 + h3 + h4) / 4.7);
Console.WriteLine("Exam: " + exam);
Console.WriteLine("Prelim: " + gc.PeriodicGrade());
abstract class GradeComponent
protected double weightedAverage;
public GradeComponent(double weight)
public double WeightedAverage()
return weightedAverage * weight;
public abstract void Add(int score, double totalItems);
class SingleGradeComponent : GradeComponent
public SingleGradeComponent(double weight) : base(weight)
public override void Add(int score, double totalItems)
weightedAverage = score / totalItems * 50.0 + 50.0;
class AccumulatedGradeComponent : GradeComponent
private int accumulatedScore;
private double accumulatedTotalItems;
public AccumulatedGradeComponent(double weight) : base(weight)
public override void Add(int score, double totalItems)
accumulatedScore += score;
accumulatedTotalItems += totalItems;
weightedAverage = accumulatedScore / accumulatedTotalItems * 50.0 + 50.0;
class AverageGradeComponent : GradeComponent
private double accumulatedAverage;
public AverageGradeComponent(double weight) : base(weight)
public override void Add(int score, double totalItems)
double average = score / totalItems * 50.0 + 50.0;
accumulatedAverage += average;
weightedAverage = accumulatedAverage / count;
private GradeComponent homework;
private GradeComponent participation;
private GradeComponent quizzes;
private GradeComponent exam;
public GradeCalculator(double w1, double w2, double w3, double w4)
homework = new AccumulatedGradeComponent(w1);
participation = new AccumulatedGradeComponent(w2);
quizzes = new AverageGradeComponent(w3);
exam = new SingleGradeComponent(w4);
public void AddHomework(int score, int totalItems)
homework.Add(score, totalItems);
public void AddQuizzes(int score, int totalItems)
quizzes.Add(score, totalItems);
public void AddParticipation(int score, int totalItems)
participation.Add(score, totalItems);
public void AddExam(int score, int totalItems)
exam.Add(score, totalItems);
public double PeriodicGrade()
return homework.WeightedAverage() + participation.WeightedAverage() + quizzes.WeightedAverage() + exam.WeightedAverage();