Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Course and Project classes


Here is my solution:

using System;

public class BooleanCourse{

  private string name;
  private bool grade;

  public BooleanCourse(string name, bool grade){
    this.name = name;
    this.grade = grade;
  }

  public bool Passed(){
    return grade;
  }

}

public class GradedCourse{

  private string name;
  private int grade;

  public GradedCourse(string name, int grade){
    this.name = name;
    this.grade = grade;
  }  

  public bool Passed(){
    return grade >= 2;
  }

}

public class Project{

  private BooleanCourse c1, c2;
  private GradedCourse c3, c4;

  public Project(BooleanCourse c1, BooleanCourse c2, 
                 GradedCourse c3, GradedCourse c4){
    this.c1 = c1; this.c2 = c2; 
    this.c3 = c3; this.c4 = c4;
  }

  public bool Passed(){
    return 
     (c1.Passed() && c2.Passed() && c3.Passed() && c4.Passed()) ||
     (!(c1.Passed()) && c2.Passed() && c3.Passed() && c4.Passed()) ||
     (c1.Passed() && !(c2.Passed()) && c3.Passed() && c4.Passed()) ||
     (c1.Passed() && c2.Passed() && !(c3.Passed()) && c4.Passed()) ||
     (c1.Passed() && c2.Passed() && c3.Passed() && !(c4.Passed()));
  }

}

public class Program {

  public static void Main(){
    BooleanCourse c1 = new BooleanCourse("Math", true),
                  c2 = new BooleanCourse("Geography", true);
    GradedCourse  c3 = new GradedCourse("Programming", 0),
                  c4 = new GradedCourse("Algorithms", -3);

    Project p = new Project(c1, c2, c3, c4);

    Console.WriteLine("Project Passed: {0}", p.Passed());
  }

}

In several respects, the program shown above is tedious: