Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package itu.ip.assignment12;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- ArrayList<Teacher> teachers;
- ArrayList<Course> courses;
- /**
- * Constructor for class Course.
- * @param name --name of the course
- * @param teacher --teacher of the course
- */
- public Main(){
- teachers = new ArrayList<Teacher>();
- courses = new ArrayList<Course>();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- //enterStudentData();
- //printTeacherData();
- //printCourseData();
- //Create objects to test findTeacherWithXCourses()
- Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
- Teacher t2 = new Teacher("Hans", "cpr2", "street2");
- Teacher t3 = new Teacher("Peter", "cpr3", "street3");
- Course c1 = new Course("Intro Programming", t1);
- Course c2 = new Course("Database Design", t1);
- Course c3 = new Course("Algorithm Design", t2);
- Course c4 = new Course("Artificial Intelligence", t2);
- Course c5 = new Course("Model Driven Development",t3);
- Course c6 = new Course("Interactive Multimedia",t3);
- Course c7 = new Course("Interaction Design",t3);
- //Add teachers and courses to Main class
- Main main = new Main();
- main.addTeacher(t1);
- main.addTeacher(t2);
- main.addTeacher(t3);
- main.addCourse(c1);
- main.addCourse(c2);
- main.addCourse(c3);
- main.addCourse(c4);
- main.addCourse(c5);
- main.addCourse(c6);
- main.addCourse(c7);
- //Now test findTeacherWithXCourses method
- main.findTeacherWithXCourses(2);
- main.findTeacherWithXCourses(3);
- }
- private void findTeacherWithXCourses(int numberOfCourses){
- System.out.println("These teachers, if any, has got "+numberOfCourses+" courses");
- for(Teacher teacher : teachers){
- if(teacher.getCourses().size()==numberOfCourses){
- System.out.println(teacher.toString());
- }
- }
- }
- private static void enterStudentData(){
- ArrayList<Student> students = new ArrayList<Student>();
- //add a couple of Studen objects
- students.add(new Student("Hans", "cpr 1"));
- students.add(new Student("Grete", "cpr 2"));
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter 'student name cpr' to create a student'" );
- System.out.println("Enter 'exit' to stop creating student objects" );
- String input = sc.nextLine();
- while(!input.equalsIgnoreCase("exit")){
- //the input requries the word "student" followed by a name and a cpr number
- if(input.toLowerCase().startsWith("student")){
- String[] studentArray = input.split(" ");
- students.add(new Student(studentArray[1],studentArray[2]));
- }
- input = sc.nextLine();
- }
- //print student objects
- for(Student student: students){
- System.out.println(student.toString());
- }
- }
- private static void printTeacherData(){
- ArrayList<Teacher> teachers = new ArrayList<Teacher>();
- Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
- t1.addCourse(new Course(t1));
- teachers.add(t1);
- teachers.add(new Teacher("Sally", "cpr2", "street2"));
- //print teacher information
- for(Teacher teacher : teachers){
- System.out.println(teacher.toString());
- }
- }
- private static void printCourseData(){
- ArrayList<Course> courses = new ArrayList<Course>();
- Teacher t1 = new Teacher("Mogens", "cpr1", "street1");
- Course course1 = new Course(t1);
- courses.add(course1);
- Course course2 = new Course("Intro Programming", 15.0d, t1);
- //print course information
- for(Course course : courses){
- System.out.println(course.toString());
- }
- }
- public void addTeacher(Teacher teacher){
- teachers.add(teacher);
- }
- public void addCourse(Course course){
- courses.add(course);
- }
- }
- -------------------------------
- Student
- package itu.ip.assignment12;
- import java.util.ArrayList;
- public class Student {
- private String name;
- private String cpr;
- ArrayList<Course> courses;
- private static String about = "CourseManager v.1.0, copyright 2012";
- private static String creditBox = "This is the credit box";
- /**
- * Constructor for class Student.
- * @param name --name of student
- * @param cpr --cpr of student
- */
- public Student(String name, String cpr){
- this.name = name;
- this.cpr = cpr;
- System.out.println("A new student '"+name+"' has been created");
- }
- @Override
- /**
- * Returns a string with the fields of the class.
- */
- public String toString(){
- String studentInfo = "";
- studentInfo += "Name: "+name+"\n cpr: "+cpr+"\n";
- studentInfo += "Courses: \n";
- if(courses!=null&&courses.size()>0){
- for(Course course: courses){
- studentInfo += course.getName()+", ";
- }
- }
- return studentInfo;
- }
- /**
- * Adds a course to the student
- */
- public void addCourse(Course course){
- courses.add(course);
- }
- private static void printCredit(){
- System.out.println(creditBox);
- }
- private static void printAbout(){
- System.out.println(about);
- }
- //Getter and setter methods
- public void setName(String name){
- this.name = name;
- }
- public void setCpr(String cpr){
- this.name = cpr;
- }
- public String getName(){
- return name;
- }
- public String getCpr(){
- return cpr;
- }
- }
- ---------------------------------
- Teacher
- package itu.ip.assignment12;
- import java.util.ArrayList;
- public class Teacher {
- private String name;
- private String cpr;
- private String address;
- ArrayList<Course> courses;
- /**
- * Constructor for class Teacher.
- * @param name --name of teacher
- * @param cpr --cpr of teacher
- */
- public Teacher(String name, String cpr, String address){
- this.name = name;
- this.cpr = cpr;
- this.address = address;
- courses = new ArrayList<Course>();
- System.out.println("A new teacher '"+name+"' has been created");
- }
- @Override
- /**
- * Returns a string with the fields of the class.
- */
- public String toString(){
- String teacherInfo = "";
- teacherInfo += "Name: "+name+"\n cpr: "+cpr+" address: "+address+"\n";
- teacherInfo += "Courses: \n";
- if(courses!=null&&courses.size()>0){
- for(Course course: courses){
- teacherInfo += course.getName()+", ";
- }
- }
- return teacherInfo;
- }
- /**
- * Adds a course to the teacher
- */
- public void addCourse(Course course){
- courses.add(course);
- }
- public String getName(){
- return name;
- }
- public ArrayList<Course> getCourses(){
- return courses;
- }
- }
- -----------------------------
- Course
- package itu.ip.assignment12;
- import java.util.ArrayList;
- public class Course {
- private String name;
- private Double credits;
- private String examForm;
- private int hoursPrWeek;
- private String courseManager;
- private ArrayList<Teacher> teacherAssistants;
- private Teacher teacher;
- /**
- * Constructor for class Course.
- * @param teacher --teacher of the course
- */
- public Course(Teacher teacher){
- this.teacher = teacher;
- //add course to teacher
- teacher.addCourse(this);
- }
- /**
- * Constructor for class Course.
- * @param name --name of the course
- * @param teacher --teacher of the course
- */
- public Course(String name, Teacher teacher){
- this.name = name;
- this.teacher = teacher;
- //add course to teacher
- teacher.addCourse(this);
- }
- /**
- * Constructor for class Course.
- * @param name --name of the course
- * @param credits --credits of the course
- * @param teacher --teacher of the course
- */
- public Course(String name, Double credits, Teacher teacher){
- this.name = name;
- this.credits = credits;
- this.teacher = teacher;
- //add course to teacher
- teacher.addCourse(this);
- }
- @Override
- /**
- * Returns a string with the fields of the class.
- */
- public String toString(){
- String studentInfo = "";
- studentInfo += "Name: "+name+"\n teacher: "+teacher.getName()+" credits: "+credits+"\n";
- return studentInfo;
- }
- public void setName(String name){
- this.name = name;
- }
- public void setCredits(Double credits){
- this.credits = credits;
- }
- public void setExamForm(String examForm){
- this.examForm = examForm;
- }
- public void setHoursPrWeek(int hoursPrWeek){
- this.hoursPrWeek = hoursPrWeek;
- }
- public void setCourseManager(String courseManager){
- this.courseManager = courseManager;
- }
- public void setTeacher(Teacher teacher){
- this.teacher = teacher;
- }
- public String getName(){
- return name;
- }
- public Double getCredits(){
- return credits;
- }
- public String getExamForm(){
- return examForm;
- }
- public int getHoursPrWeek(){
- return hoursPrWeek;
- }
- public String getCourseManager(){
- return courseManager;
- }
- public Teacher getTeacher(){
- return teacher;
- }
- }
- ----------------------------------------------
- MyMatrix
- package itu.ip.assignment12;
- public class MyMatrix {
- int[][] matrix;
- public static void main(String[] args){
- //Test diagonal method
- MyMatrix m1 = new MyMatrix(2,2,0);
- m1.setValue(0, 0, 1);
- MyMatrix m2 = new MyMatrix(2,2,8);
- //System.out.println(m1.isDiagonal());
- //System.out.println(m2.isDiagonal());
- //Test sum and multiplication methods
- MyMatrix m3 = new MyMatrix(3,3,3);
- MyMatrix m4 = new MyMatrix(3,3,2);
- MyMatrix.sum(m3, m4);
- MyMatrix.multiplication(m3, m4);
- MyMatrix.scalarMultiplication(m4, 2);
- }
- /**
- * Constructor for class MyMatrix.
- * @param dim --number of rows and columns in MyMatrix
- */
- public MyMatrix(int dim){
- matrix = new int[dim][dim];
- }
- /**
- * Constructor for class MyMatrix.
- * @param rows --number of rows in MyMatrix
- * @param cols --number of columns in MyMatrix
- */
- public MyMatrix(int rows, int cols){
- matrix = new int[rows][cols];
- }
- /**
- * Constructor for class MyMatrix.
- * @param rows --number of rows in MyMatrix
- * @param cols --number of columns in MyMatrix
- * @param init --initial number of all numbers
- */
- public MyMatrix(int rows, int cols, int init){
- matrix = new int[rows][cols];
- for(int i = 0; i<matrix.length;i++){
- for(int j = 0; j<matrix[0].length;j++){
- matrix[i][j] = init;
- }
- }
- }
- public void printRowByRow(){
- //the matrix consists of many integer arrays, int[]. These are looped through
- for(int[] row : matrix){
- //every rows consists of many integers. These are looped trough
- for(int i : row){
- System.out.print(i+" ");
- }
- System.out.println("");
- }
- }
- /**
- * This method checks if the matrix is diagonal
- */
- public boolean isDiagonal(){
- boolean nonZeroValueAlongDiagonal = false;
- //The matrix must be squared
- if(matrix.length!=matrix[0].length){
- return false;
- }
- for(int i = 0; i<matrix.length;i++){
- for(int j = 0; j<matrix[0].length;j++){
- //If an element outside the diagonal is not 0, the matrix is not diagonal.
- //An element is outside the diagonal, if i doesn't equal j.
- if(j!=i&&matrix[i][j]!=0){
- return false;
- }
- //There has to be at least one non-zero element along the diagonal
- if(j==i&&matrix[i][j]!=0){
- nonZeroValueAlongDiagonal = true;
- }
- }
- }
- return nonZeroValueAlongDiagonal;
- }
- public static void sum(MyMatrix m1, MyMatrix m2){
- //check if the matrices have the same dimensions
- if(m1.getNumberOfRows()==m2.getNumberOfRows()&&
- m1.getNumberOfCols()==m2.getNumberOfCols()){
- for(int i = 0; i<m1.getNumberOfRows();i++){
- for(int j = 0; j<m1.getNumberOfCols();j++){
- System.out.print(m1.getValue(i, j)+m2.getValue(i, j) +" ");
- }
- System.out.println("");
- }
- }
- }
- public static void multiplication(MyMatrix m1, MyMatrix m2){
- /*
- * check if the number of columns in the first matrix equals
- * the number of rows in the second matrix
- */
- if(m1.getNumberOfRows()==m2.getNumberOfCols()){
- //create new matrix for the matrix product
- MyMatrix productMatrix = new MyMatrix(m1.getNumberOfCols(), m2.getNumberOfRows(),0);
- for(int i = 0; i<productMatrix.getNumberOfRows();i++){
- for(int j = 0; j<productMatrix.getNumberOfCols();j++){
- int temp = 0;
- for(int k = 0;k<m2.getNumberOfCols();k++){
- temp += m1.getValue(k, j)*m2.getValue(i, k);
- }
- System.out.print(temp+" ");
- }
- System.out.println("");
- }
- }
- }
- public static void scalarMultiplication(MyMatrix m1, int scalar){
- //multiply every value in the matrix with the scalarvalue
- for(int i = 0; i<m1.getNumberOfRows();i++){
- for(int j = 0; j<m1.getNumberOfCols();j++){
- System.out.print(m1.getValue(i, j)*scalar +" ");
- }
- System.out.println("");
- }
- }
- public void setValue(int rowPos, int colPos, int newValue){
- matrix[rowPos][colPos] = newValue;
- }
- public void setMatrix(int[][] matrix){
- this.matrix = matrix;
- }
- public int[][] getMatrix(){
- return matrix;
- }
- public int getValue(int rowPos, int colPos){
- return matrix[rowPos][colPos];
- }
- public int[] getDimensions(){
- int[] returnArray = new int[2];
- returnArray[0] = matrix.length;
- returnArray[1] = matrix[0].length;
- return returnArray;
- }
- public int getNumberOfRows(){
- return matrix.length;
- }
- public int getNumberOfCols(){
- return matrix[0].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement