Advertisement
kristintg

Solution_Assignment 12_Alex

May 22nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.49 KB | None | 0 0
  1. package itu.ip.assignment12;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main {
  8. ArrayList<Teacher> teachers;
  9. ArrayList<Course> courses;
  10.  
  11. /**
  12. * Constructor for class Course.
  13. * @param name --name of the course
  14. * @param teacher --teacher of the course
  15. */
  16. public Main(){
  17. teachers = new ArrayList<Teacher>();
  18. courses = new ArrayList<Course>();
  19. }
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) {
  24. //enterStudentData();
  25. //printTeacherData();
  26. //printCourseData();
  27.  
  28. //Create objects to test findTeacherWithXCourses()
  29. Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
  30. Teacher t2 = new Teacher("Hans", "cpr2", "street2");
  31. Teacher t3 = new Teacher("Peter", "cpr3", "street3");
  32. Course c1 = new Course("Intro Programming", t1);
  33. Course c2 = new Course("Database Design", t1);
  34. Course c3 = new Course("Algorithm Design", t2);
  35. Course c4 = new Course("Artificial Intelligence", t2);
  36. Course c5 = new Course("Model Driven Development",t3);
  37. Course c6 = new Course("Interactive Multimedia",t3);
  38. Course c7 = new Course("Interaction Design",t3);
  39.  
  40. //Add teachers and courses to Main class
  41. Main main = new Main();
  42. main.addTeacher(t1);
  43. main.addTeacher(t2);
  44. main.addTeacher(t3);
  45. main.addCourse(c1);
  46. main.addCourse(c2);
  47. main.addCourse(c3);
  48. main.addCourse(c4);
  49. main.addCourse(c5);
  50. main.addCourse(c6);
  51. main.addCourse(c7);
  52.  
  53. //Now test findTeacherWithXCourses method
  54. main.findTeacherWithXCourses(2);
  55. main.findTeacherWithXCourses(3);
  56. }
  57.  
  58. private void findTeacherWithXCourses(int numberOfCourses){
  59. System.out.println("These teachers, if any, has got "+numberOfCourses+" courses");
  60. for(Teacher teacher : teachers){
  61. if(teacher.getCourses().size()==numberOfCourses){
  62. System.out.println(teacher.toString());
  63. }
  64. }
  65. }
  66.  
  67. private static void enterStudentData(){
  68. ArrayList<Student> students = new ArrayList<Student>();
  69. //add a couple of Studen objects
  70. students.add(new Student("Hans", "cpr 1"));
  71. students.add(new Student("Grete", "cpr 2"));
  72.  
  73. Scanner sc = new Scanner(System.in);
  74.  
  75. System.out.println("Enter 'student name cpr' to create a student'" );
  76. System.out.println("Enter 'exit' to stop creating student objects" );
  77.  
  78. String input = sc.nextLine();
  79. while(!input.equalsIgnoreCase("exit")){
  80. //the input requries the word "student" followed by a name and a cpr number
  81. if(input.toLowerCase().startsWith("student")){
  82. String[] studentArray = input.split(" ");
  83. students.add(new Student(studentArray[1],studentArray[2]));
  84. }
  85. input = sc.nextLine();
  86. }
  87.  
  88. //print student objects
  89. for(Student student: students){
  90. System.out.println(student.toString());
  91. }
  92. }
  93.  
  94. private static void printTeacherData(){
  95. ArrayList<Teacher> teachers = new ArrayList<Teacher>();
  96. Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
  97. t1.addCourse(new Course(t1));
  98. teachers.add(t1);
  99. teachers.add(new Teacher("Sally", "cpr2", "street2"));
  100.  
  101. //print teacher information
  102. for(Teacher teacher : teachers){
  103. System.out.println(teacher.toString());
  104. }
  105. }
  106.  
  107. private static void printCourseData(){
  108. ArrayList<Course> courses = new ArrayList<Course>();
  109. Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
  110. Course course1 = new Course(t1);
  111. courses.add(course1);
  112. Course course2 = new Course("Intro Programming", 15.0d, t1);
  113.  
  114. //print course information
  115. for(Course course : courses){
  116. System.out.println(course.toString());
  117. }
  118. }
  119.  
  120. public void addTeacher(Teacher teacher){
  121. teachers.add(teacher);
  122. }
  123.  
  124. public void addCourse(Course course){
  125. courses.add(course);
  126. }
  127. }
  128. -------------------------------
  129. Student
  130.  
  131. package itu.ip.assignment12;
  132.  
  133. import java.util.ArrayList;
  134.  
  135. public class Student {
  136. private String name;
  137. private String cpr;
  138. ArrayList<Course> courses;
  139. private static String about = "CourseManager v.1.0, copyright 2012";
  140. private static String creditBox = "This is the credit box";
  141.  
  142. /**
  143. * Constructor for class Student.
  144. * @param name --name of student
  145. * @param cpr --cpr of student
  146. */
  147. public Student(String name, String cpr){
  148. this.name = name;
  149. this.cpr = cpr;
  150. System.out.println("A new student '"+name+"' has been created");
  151. }
  152.  
  153. @Override
  154. /**
  155. * Returns a string with the fields of the class.
  156. */
  157. public String toString(){
  158. String studentInfo = "";
  159. studentInfo += "Name: "+name+"\n cpr: "+cpr+"\n";
  160. studentInfo += "Courses: \n";
  161. if(courses!=null&&courses.size()>0){
  162. for(Course course: courses){
  163. studentInfo += course.getName()+", ";
  164. }
  165. }
  166. return studentInfo;
  167. }
  168.  
  169. /**
  170. * Adds a course to the student
  171. */
  172. public void addCourse(Course course){
  173. courses.add(course);
  174. }
  175.  
  176. private static void printCredit(){
  177. System.out.println(creditBox);
  178. }
  179.  
  180. private static void printAbout(){
  181. System.out.println(about);
  182. }
  183.  
  184. //Getter and setter methods
  185. public void setName(String name){
  186. this.name = name;
  187. }
  188.  
  189. public void setCpr(String cpr){
  190. this.name = cpr;
  191. }
  192.  
  193. public String getName(){
  194. return name;
  195. }
  196.  
  197. public String getCpr(){
  198. return cpr;
  199. }
  200. }
  201.  
  202. ---------------------------------
  203. Teacher
  204.  
  205. package itu.ip.assignment12;
  206.  
  207. import java.util.ArrayList;
  208.  
  209. public class Teacher {
  210. private String name;
  211. private String cpr;
  212. private String address;
  213. ArrayList<Course> courses;
  214.  
  215. /**
  216. * Constructor for class Teacher.
  217. * @param name --name of teacher
  218. * @param cpr --cpr of teacher
  219. */
  220. public Teacher(String name, String cpr, String address){
  221. this.name = name;
  222. this.cpr = cpr;
  223. this.address = address;
  224. courses = new ArrayList<Course>();
  225. System.out.println("A new teacher '"+name+"' has been created");
  226. }
  227.  
  228. @Override
  229. /**
  230. * Returns a string with the fields of the class.
  231. */
  232. public String toString(){
  233. String teacherInfo = "";
  234. teacherInfo += "Name: "+name+"\n cpr: "+cpr+" address: "+address+"\n";
  235. teacherInfo += "Courses: \n";
  236. if(courses!=null&&courses.size()>0){
  237. for(Course course: courses){
  238. teacherInfo += course.getName()+", ";
  239. }
  240. }
  241. return teacherInfo;
  242. }
  243.  
  244. /**
  245. * Adds a course to the teacher
  246. */
  247. public void addCourse(Course course){
  248. courses.add(course);
  249. }
  250.  
  251. public String getName(){
  252. return name;
  253. }
  254.  
  255. public ArrayList<Course> getCourses(){
  256. return courses;
  257. }
  258. }
  259. -----------------------------
  260. Course
  261.  
  262. package itu.ip.assignment12;
  263.  
  264. import java.util.ArrayList;
  265.  
  266. public class Course {
  267. private String name;
  268. private Double credits;
  269. private String examForm;
  270. private int hoursPrWeek;
  271. private String courseManager;
  272. private ArrayList<Teacher> teacherAssistants;
  273. private Teacher teacher;
  274.  
  275. /**
  276. * Constructor for class Course.
  277. * @param teacher --teacher of the course
  278. */
  279. public Course(Teacher teacher){
  280. this.teacher = teacher;
  281. //add course to teacher
  282. teacher.addCourse(this);
  283. }
  284.  
  285. /**
  286. * Constructor for class Course.
  287. * @param name --name of the course
  288. * @param teacher --teacher of the course
  289. */
  290. public Course(String name, Teacher teacher){
  291. this.name = name;
  292. this.teacher = teacher;
  293. //add course to teacher
  294. teacher.addCourse(this);
  295. }
  296.  
  297. /**
  298. * Constructor for class Course.
  299. * @param name --name of the course
  300. * @param credits --credits of the course
  301. * @param teacher --teacher of the course
  302. */
  303. public Course(String name, Double credits, Teacher teacher){
  304. this.name = name;
  305. this.credits = credits;
  306. this.teacher = teacher;
  307. //add course to teacher
  308. teacher.addCourse(this);
  309. }
  310.  
  311. @Override
  312. /**
  313. * Returns a string with the fields of the class.
  314. */
  315. public String toString(){
  316. String studentInfo = "";
  317. studentInfo += "Name: "+name+"\n teacher: "+teacher.getName()+" credits: "+credits+"\n";
  318. return studentInfo;
  319. }
  320.  
  321. public void setName(String name){
  322. this.name = name;
  323. }
  324.  
  325. public void setCredits(Double credits){
  326. this.credits = credits;
  327. }
  328.  
  329. public void setExamForm(String examForm){
  330. this.examForm = examForm;
  331. }
  332.  
  333. public void setHoursPrWeek(int hoursPrWeek){
  334. this.hoursPrWeek = hoursPrWeek;
  335. }
  336.  
  337. public void setCourseManager(String courseManager){
  338. this.courseManager = courseManager;
  339. }
  340.  
  341. public void setTeacher(Teacher teacher){
  342. this.teacher = teacher;
  343. }
  344.  
  345. public String getName(){
  346. return name;
  347. }
  348.  
  349. public Double getCredits(){
  350. return credits;
  351. }
  352.  
  353. public String getExamForm(){
  354. return examForm;
  355. }
  356.  
  357. public int getHoursPrWeek(){
  358. return hoursPrWeek;
  359. }
  360.  
  361. public String getCourseManager(){
  362. return courseManager;
  363. }
  364.  
  365. public Teacher getTeacher(){
  366. return teacher;
  367. }
  368. }
  369.  
  370. ----------------------------------------------
  371. MyMatrix
  372.  
  373. package itu.ip.assignment12;
  374.  
  375.  
  376. public class MyMatrix {
  377. int[][] matrix;
  378.  
  379. public static void main(String[] args){
  380. //Test diagonal method
  381. MyMatrix m1 = new MyMatrix(2,2,0);
  382. m1.setValue(0, 0, 1);
  383. MyMatrix m2 = new MyMatrix(2,2,8);
  384. //System.out.println(m1.isDiagonal());
  385. //System.out.println(m2.isDiagonal());
  386.  
  387. //Test sum and multiplication methods
  388. MyMatrix m3 = new MyMatrix(3,3,3);
  389. MyMatrix m4 = new MyMatrix(3,3,2);
  390.  
  391. MyMatrix.sum(m3, m4);
  392. MyMatrix.multiplication(m3, m4);
  393. MyMatrix.scalarMultiplication(m4, 2);
  394. }
  395.  
  396. /**
  397. * Constructor for class MyMatrix.
  398. * @param dim --number of rows and columns in MyMatrix
  399. */
  400. public MyMatrix(int dim){
  401. matrix = new int[dim][dim];
  402. }
  403.  
  404. /**
  405. * Constructor for class MyMatrix.
  406. * @param rows --number of rows in MyMatrix
  407. * @param cols --number of columns in MyMatrix
  408. */
  409. public MyMatrix(int rows, int cols){
  410. matrix = new int[rows][cols];
  411. }
  412.  
  413. /**
  414. * Constructor for class MyMatrix.
  415. * @param rows --number of rows in MyMatrix
  416. * @param cols --number of columns in MyMatrix
  417. * @param init --initial number of all numbers
  418. */
  419. public MyMatrix(int rows, int cols, int init){
  420. matrix = new int[rows][cols];
  421. for(int i = 0; i<matrix.length;i++){
  422. for(int j = 0; j<matrix[0].length;j++){
  423. matrix[i][j] = init;
  424. }
  425. }
  426. }
  427.  
  428. public void printRowByRow(){
  429. //the matrix consists of many integer arrays, int[]. These are looped through
  430. for(int[] row : matrix){
  431. //every rows consists of many integers. These are looped trough
  432. for(int i : row){
  433. System.out.print(i+" ");
  434. }
  435. System.out.println("");
  436. }
  437. }
  438. /**
  439. * This method checks if the matrix is diagonal
  440. */
  441. public boolean isDiagonal(){
  442. boolean nonZeroValueAlongDiagonal = false;
  443. //The matrix must be squared
  444. if(matrix.length!=matrix[0].length){
  445. return false;
  446. }
  447. for(int i = 0; i<matrix.length;i++){
  448. for(int j = 0; j<matrix[0].length;j++){
  449. //If an element outside the diagonal is not 0, the matrix is not diagonal.
  450. //An element is outside the diagonal, if i doesn't equal j.
  451. if(j!=i&&matrix[i][j]!=0){
  452. return false;
  453. }
  454. //There has to be at least one non-zero element along the diagonal
  455. if(j==i&&matrix[i][j]!=0){
  456. nonZeroValueAlongDiagonal = true;
  457. }
  458. }
  459. }
  460. return nonZeroValueAlongDiagonal;
  461. }
  462.  
  463. public static void sum(MyMatrix m1, MyMatrix m2){
  464. //check if the matrices have the same dimensions
  465. if(m1.getNumberOfRows()==m2.getNumberOfRows()&&
  466. m1.getNumberOfCols()==m2.getNumberOfCols()){
  467. for(int i = 0; i<m1.getNumberOfRows();i++){
  468. for(int j = 0; j<m1.getNumberOfCols();j++){
  469. System.out.print(m1.getValue(i, j)+m2.getValue(i, j) +" ");
  470. }
  471. System.out.println("");
  472. }
  473. }
  474. }
  475.  
  476. public static void multiplication(MyMatrix m1, MyMatrix m2){
  477. /*
  478. * check if the number of columns in the first matrix equals
  479. * the number of rows in the second matrix
  480. */
  481. if(m1.getNumberOfRows()==m2.getNumberOfCols()){
  482. //create new matrix for the matrix product
  483. MyMatrix productMatrix = new MyMatrix(m1.getNumberOfCols(), m2.getNumberOfRows(),0);
  484. for(int i = 0; i<productMatrix.getNumberOfRows();i++){
  485. for(int j = 0; j<productMatrix.getNumberOfCols();j++){
  486. int temp = 0;
  487. for(int k = 0;k<m2.getNumberOfCols();k++){
  488. temp += m1.getValue(k, j)*m2.getValue(i, k);
  489. }
  490. System.out.print(temp+" ");
  491. }
  492. System.out.println("");
  493. }
  494. }
  495. }
  496.  
  497. public static void scalarMultiplication(MyMatrix m1, int scalar){
  498. //multiply every value in the matrix with the scalarvalue
  499. for(int i = 0; i<m1.getNumberOfRows();i++){
  500. for(int j = 0; j<m1.getNumberOfCols();j++){
  501. System.out.print(m1.getValue(i, j)*scalar +" ");
  502. }
  503. System.out.println("");
  504. }
  505. }
  506.  
  507. public void setValue(int rowPos, int colPos, int newValue){
  508. matrix[rowPos][colPos] = newValue;
  509. }
  510.  
  511. public void setMatrix(int[][] matrix){
  512. this.matrix = matrix;
  513. }
  514.  
  515. public int[][] getMatrix(){
  516. return matrix;
  517. }
  518.  
  519. public int getValue(int rowPos, int colPos){
  520. return matrix[rowPos][colPos];
  521. }
  522.  
  523. public int[] getDimensions(){
  524. int[] returnArray = new int[2];
  525. returnArray[0] = matrix.length;
  526. returnArray[1] = matrix[0].length;
  527. return returnArray;
  528. }
  529.  
  530. public int getNumberOfRows(){
  531. return matrix.length;
  532. }
  533.  
  534. public int getNumberOfCols(){
  535. return matrix[0].length;
  536. }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement