CMPE 261 - Lab worksheet - Week 3

Problem 1: Matrix

Write a Java class named Matrix to represent a two dimensional array of real numbers. Your class must have two constructors, one which accepts matrix dimensions, and another which accepts the matrix data as an array. Also your class must support following methods:

You may use the following tester code to test your class:

    class Tester {
        public static void main(String[] args){
            int m=2;
            int n=3;
            Matrix m1=new Matrix(m,n);
            for(int i=0;i<m;i++)
                for (int j=0;j<n;j++)
                    m1.setElement(i,j,i+j);
            System.out.println(String.format("%.2f",m1.getElement(0,0)));
            double[][] data={{1,2,3},{4,5,6}};
            Matrix m2=new Matrix(data);
            System.out.println(m1);
            System.out.println(m2);
            try {
              System.out.println(m1.add(m2));
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

Solution

    class Matrix {
      double[][] data;

      Matrix(int rows, int columns) {
        data=new double[rows][columns];
      }

      Matrix(double[][] data){
        this.data=data;
      }

      int numRows() {return data.length;}

      int numColumns() {return data[0].length;}

      void setElement(int r, int c, double element) {
        data[r][c]=element;
      }

      double getElement(int r, int c) {
        return data[r][c];
      }

      Matrix add(Matrix other) throws Exception{
        if ( numRows()!=other.numRows() || numColumns()!=other.numColumns())
          throw new Exception("Matrix sizes are different");
        Matrix retval=new Matrix(data.length, data[0].length);
        for(int i=0;i<data.length;i++) 
          for (int j=0;j<data[0].length;j++)
          retval.setElement(i,j,getElement(i,j)+other.getElement(i,j));
        return retval;
      }

      public String toString() {
        String retval="";
        for(int i=0;i<data.length;i++) {
          for (int j=0;j<data[0].length;j++)
            retval+=" "+String.format("%.2f",getElement(i,j));
          retval+="\n";
        }
        return retval;
      }
    }

Problem 2: Students and courses

Write two classes, one to represent a Student (with firstname, lastname, and id) and another to represent a Course (with course-name, code, and max-capacity). Put methods and necessary data fields (e.g. an ArrayList or an array) into your Course class so that a student can be added to or removed from the course.

Solution

    class Student {
      String firstName, lastName, id;
      Student(String firstName, String lastName, String id) {
        this.firstName=firstName;
        this.lastName=lastName;
        this.id=id;
      }
    }

    class Course {
      String name, code;
      int capacity;
      Student[] students;
      Course(String name, String code, int capacity) {
        this.name=name;
        this.code=code;
        this.capacity=capacity;
        students=new Student[capacity];
      }
      void addStudent(Student student) throws Exception {
        for(int i=0;i<capacity;i++)
            if (students[i]==null) {
                students[i]=student;
                return;
            }
        throw new Exception("Capacity is full");
      }
      void removeStudent(Student student) throws Exception {
        for(int i=0;i<capacity;i++)
            if (students[i]==student) {
                students[i]=null;
                return;
            }
        throw new Exception("Student not found");
      }
    }