Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. public class GradeBookApp4 {
  2. public static void main(String args[]) {
  3. Student Jie = new Student ("Jie");
  4. Student Albert = new Student ("Albert");
  5. Student Alex = new Student ("Alex"); //++++++++++
  6.  
  7. Teacher Nick = new Teacher ("Nick");
  8.  
  9. Albert.setEmail("albert@gmail.com"); //++++++++++
  10. Jie.setEmail("jie@gmail.com");
  11. Nick.setEmail("nick@gmail.com");
  12.  
  13. Member[] members = {Albert, Jie, Nick}; //++++++++++
  14. for (Member m: members)
  15. m.showInfo();
  16.  
  17. Course Java = new Course ("Java", 3);
  18. Course Python = new Course ("Python", 3);
  19.  
  20. Nick.offer(Java);
  21. Nick.offer(Python);
  22.  
  23. Jie.takeCourse(Java);
  24. Albert.takeCourse(Java);
  25. Alex.takeCourse(Java); //++++++++++
  26.  
  27. Nick.score(Java, Jie, 100);
  28. Nick.score(Java, Albert, 98);
  29. Nick.score(Java, Alex, 20); //++++++++++
  30.  
  31. // Jie.showGrade();
  32. // Albert.showGrade();
  33. // Alex.showGrade(); //++++++++++
  34. Java.showCourseInfo();
  35. }
  36. }
  37.  
  38.  
  39. class Course {
  40. String cName;
  41. private int degree;
  42. Student[] students = new Student[10];
  43. int studentCount = 0;
  44. Teacher teacher = new Teacher("None");
  45. int [] grades = new int[10];
  46. int scoreCount = 0; //++++++++++
  47. double sum=0; //++++++++++
  48. double average=0; //++++++++++
  49.  
  50. public Course (String name, int degree) {
  51. this.cName = name;
  52. this.degree = degree;
  53. }
  54.  
  55. public void registeredBy(Student s) {
  56. if (studentCount <= 9)
  57. students[studentCount++] = s;
  58. else
  59. System.out.println("Students overflow in a class");
  60. }
  61.  
  62. public void showCourseInfo() {
  63. System.out.println("Course: "+ cName);
  64. System.out.println("-- Teacher: " + teacher.name); //*****
  65. String s = "";
  66. String g = "";
  67. for (int i=0; i<studentCount ; i++) {
  68. s += students[i].name + ", "; //*****
  69. g += Integer.toString(grades[i]) + ", ";
  70. }
  71. System.out.println("-- Students: " + s);
  72. System.out.println("-- Grades: " + g);
  73. System.out.println("-- Average: "+ average); //++++++++++
  74. }
  75.  
  76. public void setTeacher(Teacher t) {
  77. teacher = t;
  78. }
  79.  
  80. public boolean takenBy(Student s) {
  81. for (int i=0; i<studentCount ; i++) {
  82. if (students[i] == s) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88.  
  89. public void score(Student s, int g) {
  90. int idx = getIndex(s);
  91. if (idx == -1)
  92. System.out.println(s.name + " is not in " + cName);
  93. else {
  94. grades[idx] = g;
  95. scoreCount++; //++++++++++
  96. sum += g;
  97. average = sum/scoreCount;
  98. }
  99. }
  100.  
  101. private int getIndex(Student s) {
  102. for (int i=0; i<studentCount; i++) {
  103. if (students[i] == s) {
  104. return i;
  105. }
  106. }
  107. return -1;
  108. }
  109.  
  110. public int getGrade(Student s) {
  111. int idx = getIndex(s);
  112. if (idx != -1) {
  113. return grades[idx];
  114. }
  115. return idx;
  116. }
  117. }
  118.  
  119. class Teacher extends Member { //*****
  120. // String tName; //----------
  121. // private String email;
  122. Course[] courses = new Course[10];
  123. int courseCount = 0;
  124.  
  125. public Teacher(String name) {
  126. super(name); //*****
  127. }
  128. // public void setEmail(String e) {
  129. // this.email = e;
  130. // }
  131. public void offer(Course c) {
  132. if (courseCount <= 9) {
  133. courses[courseCount++] = c;
  134. c.setTeacher(this);
  135. }
  136. else
  137. System.out.println("Offer too many courses");
  138. }
  139.  
  140. public void showCourse() {
  141. for (Course c: courses) {
  142. if (c != null)
  143. System.out.println(c.cName);
  144. else
  145. break;
  146. }
  147. }
  148.  
  149. public void score(Course c, Student s, int g) {
  150. if (offeredByMe(c) && c.takenBy(s)) {
  151. c.score(s, g);
  152. }
  153. }
  154.  
  155. private boolean offeredByMe(Course c) {
  156. for (int i=0; i<courseCount; i++) {
  157. if (courses[i] == c) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. }
  164.  
  165. class Student extends Member { //*****
  166. Course[] courses = new Course[10];
  167. // String sName; //----------
  168. // private String email;
  169. int courseCount = 0;
  170. public Student (String name){
  171. super(name); //*****
  172. }
  173. public void takeCourse(Course c) {
  174. if (courseCount <=9) {
  175. courses[courseCount++] = c;
  176. c.registeredBy(this);
  177. }
  178. else
  179. System.out.println("Hei, you take too many courses");
  180. }
  181. public void showGrade() {
  182. System.out.println("The grades of student " + name); //*****
  183. for (int i=0; i<courseCount; i++) {
  184. Course c = courses[i];
  185. String gString = "no grade";
  186. int g = c.getGrade(this);
  187. if (g != -1) gString = Integer.toString(g);
  188. System.out.println("-- " + c.cName + ": " + gString);
  189. }
  190. }
  191. }
  192.  
  193. abstract class Member { //++++++++++
  194. String name;
  195. private String email;
  196. public Member(String name) {
  197. this.name = name;
  198. }
  199. public void setEmail(String e) {
  200. this.email = e;
  201. }
  202. public void showInfo() { //++++++++++
  203. System.out.println(name + ", email: " + email);
  204. }
  205. }
  206.  
  207.  
  208.  
  209. //******************************************************************
  210.  
  211.  
  212.  
  213. /*
  214. INTERFACE and its implementation
  215. * an Instructor interface define the "teach" behavior
  216. * Teacher or IndustryExpert may server as an instructor
  217. * all instructors are required to set their Qualification
  218.  
  219. * OCP principle
  220. * when we have different types of qualification, we use "extension", not
  221. modify the code
  222.  
  223.  
  224. * add an interface Instructor, methods: setQualification(Qualification)
  225. and showQualification()
  226. * add a new class IndustryExpert, to implement the Instructor
  227. * modify Teacher to implement the Instructor
  228. * add a Qualification subclass Certification, which requires the
  229. year getting the certification.
  230. * add a static method to show all instructors
  231. */
  232.  
  233. public class GradeBookApp5 {
  234. public static void main(String args[]) {
  235. Student Jie = new Student ("Jie");
  236. Student Albert = new Student ("Albert");
  237. Student Alex = new Student ("Alex");
  238.  
  239. Teacher Nick = new Teacher ("Nick");
  240.  
  241. IndustryExpert Peter = new IndustryExpert("Peter"); //++++++++++
  242. Nick.setQualification(new Qualification("IECS Ph.D")); //++++++++++
  243. Peter.setQualification(new Certification(2000, "Cisco")); //++++++++++
  244. Instructor tutors[] = {(Instructor)Nick, (Instructor)Peter}; //++++++++++
  245. System.out.println("\n=== INSTRUCTORS QUALIFICATION ==="); //++++++++++
  246.  
  247. for (Instructor i: tutors)
  248. i.showQualification();
  249.  
  250. Albert.setEmail("albert@gmail.com");
  251. Jie.setEmail("jie@gmail.com");
  252. Nick.setEmail("nick@gmail.com");
  253.  
  254. Member[] members = {Albert, Jie, Nick};
  255. System.out.println("\n=== MEMBERS ==="); //++++++++++
  256. for (Member m: members)
  257. m.showInfo();
  258.  
  259. Course Java = new Course ("Java", 3);
  260. Course Python = new Course ("Python", 3);
  261.  
  262. Nick.offer(Java);
  263. Nick.offer(Python);
  264.  
  265. Jie.takeCourse(Java);
  266. Albert.takeCourse(Java);
  267. Alex.takeCourse(Java);
  268.  
  269. Nick.score(Java, Jie, 100);
  270. Nick.score(Java, Albert, 98);
  271. Nick.score(Java, Alex, 20);
  272.  
  273. Course[] courses = {Java, Python};
  274. showCourses(courses);
  275. }
  276.  
  277. public static void showCourses(Course[] courses) { //++++++++++
  278. System.out.println("\n=== COURSES ===");
  279. for (Course c: courses)
  280. c.showCourseInfo();
  281. }
  282.  
  283. }
  284.  
  285. Code
  286. class Course {
  287. String cName;
  288. private int degree;
  289. Student[] students = new Student[10];
  290. int studentCount = 0;
  291. Teacher teacher = new Teacher("None");
  292. int [] grades = new int[10];
  293. int scoreCount = 0;
  294. double sum=0;
  295. double average=0;
  296.  
  297. public Course (String name, int degree) {
  298. this.cName = name;
  299. this.degree = degree;
  300. }
  301.  
  302. public void registeredBy(Student s) {
  303. if (studentCount <= 9)
  304. students[studentCount++] = s;
  305. else
  306. System.out.println("Students overflow in a class");
  307. }
  308.  
  309. public void showCourseInfo() {
  310. System.out.println("Course: "+ cName);
  311. System.out.println("-- Teacher: " + teacher.name);
  312. String s = "";
  313. String g = "";
  314. for (int i=0; i<studentCount ; i++) {
  315. s += students[i].name + ", ";
  316. g += Integer.toString(grades[i]) + ", ";
  317. }
  318. System.out.println("-- Students: " + s);
  319. System.out.println("-- Grades: " + g);
  320. System.out.println("-- Average: "+ average);
  321. }
  322.  
  323. public void setTeacher(Teacher t) {
  324. teacher = t;
  325. }
  326.  
  327. public boolean takenBy(Student s) {
  328. for (int i=0; i<studentCount ; i++) {
  329. if (students[i] == s) {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335.  
  336. public void score(Student s, int g) {
  337. int idx = getIndex(s);
  338. if (idx == -1)
  339. System.out.println(s.name + " is not in " + cName);
  340. else {
  341. grades[idx] = g;
  342. scoreCount++;
  343. sum += g;
  344. average = sum/scoreCount;
  345. }
  346. }
  347.  
  348. private int getIndex(Student s) {
  349. for (int i=0; i<studentCount; i++) {
  350. if (students[i] == s) {
  351. return i;
  352. }
  353. }
  354. return -1;
  355. }
  356.  
  357. public int getGrade(Student s) {
  358. int idx = getIndex(s);
  359. if (idx != -1) {
  360. return grades[idx];
  361. }
  362. return idx;
  363. }
  364. }
  365.  
  366. class Teacher extends Member implements Instructor { //*****
  367. Course[] courses = new Course[10];
  368. int courseCount = 0;
  369. Qualification qualification; //++++++++++
  370.  
  371. public Teacher(String name) {
  372. super(name);
  373. }
  374.  
  375. public void offer(Course c) {
  376. if (courseCount <= 9) {
  377. courses[courseCount++] = c;
  378. c.setTeacher(this);
  379. }
  380. else
  381. System.out.println("Offer too many courses");
  382. }
  383.  
  384. public void showCourse() {
  385. for (Course c: courses) {
  386. if (c != null)
  387. System.out.println(c.cName);
  388. else
  389. break;
  390. }
  391. }
  392.  
  393. public void score(Course c, Student s, int g) {
  394. if (offeredByMe(c) && c.takenBy(s)) {
  395. c.score(s, g);
  396. }
  397. }
  398.  
  399. private boolean offeredByMe(Course c) {
  400. for (int i=0; i<courseCount; i++) {
  401. if (courses[i] == c) {
  402. return true;
  403. }
  404. }
  405. return false;
  406. }
  407.  
  408. public void setQualification(Qualification q) { //++++++++++
  409. this.qualification = q;
  410. }
  411.  
  412. public void showQualification() { //++++++++++
  413. System.out.println(name + " is a teacher because " + qualification);
  414. }
  415. }
  416.  
  417. class Student extends Member {
  418. Course[] courses = new Course[10];
  419.  
  420. int courseCount = 0;
  421. public Student (String name){
  422. super(name); //*****
  423. }
  424. public void takeCourse(Course c) {
  425. if (courseCount <=9) {
  426. courses[courseCount++] = c;
  427. c.registeredBy(this);
  428. }
  429. else
  430. System.out.println("Hei, you take too many courses");
  431. }
  432. public void showGrade() {
  433. System.out.println("The grades of student " + name);
  434. for (int i=0; i<courseCount; i++) {
  435. Course c = courses[i];
  436. String gString = "no grade";
  437. int g = c.getGrade(this);
  438. if (g != -1) gString = Integer.toString(g);
  439. System.out.println("-- " + c.cName + ": " + gString);
  440. }
  441. }
  442. }
  443.  
  444. abstract class Member {
  445. String name;
  446. private String email;
  447. public Member(String name) {
  448. this.name = name;
  449. }
  450. public void setEmail(String e) {
  451. this.email = e;
  452. }
  453. public void showInfo() {
  454. System.out.println(name + ", email: " + email);
  455. }
  456. }
  457.  
  458. interface Instructor { //++++++++++
  459. public void setQualification(Qualification q);
  460. public void showQualification();
  461. }
  462.  
  463. class Qualification { //++++++++++
  464. String description;
  465. public Qualification(String desc) {
  466. this.description = desc;
  467. }
  468. public String toString() {
  469. return description;
  470. }
  471. }
  472.  
  473. class Certification extends Qualification { //++++++++++
  474. public Certification(int year, String desc) {
  475. super(desc + " " + Integer.toString(year));
  476. }
  477. }
  478.  
  479. class IndustryExpert implements Instructor { //++++++++++
  480. Qualification q;
  481. String name;
  482. public IndustryExpert(String name) {
  483. this.name = name;
  484. }
  485. public void setQualification(Qualification q) {
  486. this.q = q;
  487. }
  488. public void showQualification() {
  489. System.out.println(name + " is qualified to be an instructor because " + q);
  490. }
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement