/* Tuesday pm lecture - Part 2: Methods for Containment +---------------+ | GradeRecord | +---------------+ +-----------------| Course course | | | String term | | | double grade | | +---------------+ v +---------------+ | Course | +---------------+ | int num | | String title | | int credits | +---------------+ */ // to represent student's grade record class GradeRecord { Course course; String term; double grade; GradeRecord(Course course, String term, double grade) { this.course = course; this.term = term; this.grade = grade; } /* TEMPLATE: ... this.course ... -- Course ... this.term ... -- String ... this.grade ... -- double ... this.course.qPoints(double g) ... -- double */ // compute the quality points for this grade record double qPoints(){ return this.course.qPoints(this.grade); } } // to represent a course in the registrar system class Course { int num; String title; int credits; Course(int num, String title, int credits) { this.num = num; this.title = title; this.credits = credits; } /* TEMPLATE: ... this.num ... -- int ... this.title ... -- String ... this.credits ... -- int */ // compute the quality points for the given grade earned in this course double qPoints(double grade){ return this.credits * grade; } } // client class for the GradeRecord and Course classes class Examples{ Examples(){} // sample courses Course fund = new Course(211, "Fundamentals", 4); Course ovw = new Course(220, "Overview", 1); Course algo = new Course(690, "Algorithms", 4); // sample grade records GradeRecord fundFL05 = new GradeRecord(this.fund, "FL05", 3.66); GradeRecord ovwFL05 = new GradeRecord(this.ovw, "FL05", 4.0); GradeRecord algoSP05 = new GradeRecord(this.algo, "SP05", 3.0); // tests for the method qPoints in the class GradeRecord boolean test1 = check this.fundFL05.qPoints() expect 14.64 within 0.01; boolean test2 = check this.ovwFL05.qPoints() expect 4.0 within 0.01; boolean test3 = check this.algoSP05.qPoints() expect 12.0 within 0.01; boolean test4 = check this.fund.qPoints(3.66) expect 14.64 within 0.01; boolean test5 = check this.ovw.qPoints(4.0) expect 4.0 within 0.01; boolean test6 = check this.algo.qPoints(3.0) expect 12.0 within 0.01; }