April 20 Lab

Instructions

Please log into cs.bridgewater.edu or your AWS VM and rename the repository directory csci102/labs/apr15 to csci102/labs/apr20.

Please place all of your code for the following assignment in csci102/labs/apr20.

Program Specifications

Write a class that models a 2-dimensional array named Matrix according to the following specifications.

    • The class should be generic.
    • The class should contain a constructor.
      • The constructor should have two integer parameters. The first parameter specifies the number of rows in the 2D array, and the second parameter specifies the number of columns in the 2D array.
      • If either of arguments passed into the array are less than or equal to zero, the constructor should throw an IllegalArgumentException.
      • The constructor should create an underlying 2D array to hold the elements of the matrix.
    • The class has a method named getNumberOfRows which returns an integer whose value is equal to the number of rows in the 2D array.
    • The class has a method named getNumberOfColumns which returns an integer whose value is equal to the number of columns in the 2D array.
    • The class has a method named add.
      • The add method should have 3 parameters. The first parameter is an integer which indicates a row index, the second is an integer which indicates a column index, and the third is an element of the generic type.
      • If the row or column arguments are not valid indices for the 2D array, an IllegalArgumentException is thrown.
      • If the third parameter is null, a NullPointException is thrown.
      • If an exception is not thrown, the element is stored in the 2D array at the location specified by the row and column.
    • The class should have a method named get.
      • The get method takes two integers as arguments indicating the row and column of an element in the 2D array that is to be returned.
      • The method throws an IllegalArgumentException if the row or column arguments are not valid indices for the 2D array.
    • The class overrides toString, returning a string containing all of the elements in the matrix with spaces between the elements and each row of elements separated by new line characters.
    • The class overrides equals and considers all of the elements in the matrix as significant elements.
    • The class overrides hashCode.
    • The class implements the Iterable interface.

April 6 Lab

Instructions

Please log into cs.bridgewater.edu or your AWS VM and rename the repository directory csci102/labs/apr1 to csci102/labs/apr6.

Please place all of your code for the following assignment in csci102/labs/apr6.

Program Specifications

A tuple is a finite ordered list of elements and a k-tuple is an ordered list of k elements.  For example, 2-tuple is a pair of ordered elements and a 3-tuple is an ordered list of 3 elements.

We use tuples all the time without referring to them as tuples.  For example, a 2D point (x,y) on a plane is a 2-tuple and a 3D point (x,y,z) is a 3-tuple.  They each have a k elements and their order matters.

Create a generic abstract class named Tuple that satisfies the following.

    • The class has a private field named arr that holds a reference to an array of Objects.
    • The class contains a constructor that has an integer parameter.  When the constructor is called, the field arr is initialized to an array whose size is equal to the value of the integer passed to the constructor.
    • The class has a method named size() that returns the length of arr.
    • The class contains a method named get() that has an integer parameter and returns the element in arr at the index specified by the value passed to the method. If the value of the first parameter is not a valid index an IndexOutOfBoundsException is thrown.
    • The class contains a method named set() that has an integer parameter and a parameter of the generic type.  The method sets the generic element in arr at the position specified by the value of the first parameter. If the value of the first parameter is not a valid index an IndexOutOfBoundsException is thrown.
    • The class overrides toString() and returns a string displaying a comma separated list of elements within parenthesis.
    • The class overrides equals() and considers an Object passed into the method equal to this, if the Object passed into the method is a Tuple having the same length and all the elements in the Tuples are equal.
    • The class overrides hashCode().
    • The class implements the Iterable interface.

Create a generic class named Pair that satisfies the following.

    • The class extends Tuple.
    • The class contains a constructor that has two parameters of the generic type.  The constructor passes the value 2 to its superclass’ constructor, then sets the two elements passed into the constructor into the tuple by calling set().

Create a driver named Driver that satisfies the following.

    • In main, create numerous Pair objects holding various types and test the methods in the Tuple class.

March 23 Lab

Instructions

  • Log onto cs.bridgewater.edu.
  • Navigate to csci102/labs in your repository.
  • Change the name of the directory named mar25 to mar23 using the following command.
$ mv mar25 mar23
  • Change your working directory to mar23 and complete the assignment.
  • Push your files to GitHub when you are finished.

Program Specifications

  1. Write an Interface named Person
    • The interface should require access methods for a first name (String) and last name (String).
  2. Write a class named Student that implements the Person interface.
    • The Student class should include a major field (String), along with an access method for the field.
    • The class should set all of its fields in the constructor using arguments passed to the constructor.
  3. Write a class named TransferStudent that is a subclass of Student.
    • A transfer student has field that specifies the number of credits transferred in (int).
    • The constructor should set all of the class fields using the values of the arguments passed into the constructor.
    • The class should have access methods to get the values stored in the fields but should not contain modifier methods.
    • The class should have a toString method that returns a string with meaningful information.
  4. Write a driver class named Driver that creates two objects of type TransferStudent for the following students.
    • John Doe has a Computer Science major with 24 credits transferring in.
    • Kim Brown has a Computer Science major with 27 credits transferring in.
    • Call toString on both instances of the TransferStudent class.