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 16 Assignment

Please write your code for this assignment in csci102/hw/apr16 and when finished push your code to GitHub.

Program Specification

Write a class named Address that models a mailing address. The class should have the following fields.

      • building number (int)
      • street name (String)
      • zip code (int)

The class should include a constructor that takes the field information as arguments and sets the fields.  A NullPointerException should be thrown if the street name is null.  An IllegalArgumentExceptions should be thrown if the zip code field does not contain a positive 5 digit zip integer or if the building number is negative.

The class should override the toString method and return a string containing all of the field data with a comma between the street name and zip code.

The class should override equals and consider all fields as significant.

The class should override hashCode.

The class should implement the Comparable interface and order the address according to the zip code, then street name, and finally building number.  For example, the following addresses are sorted.

      • 100 Alpha Street, 10001
      • 200 Alpha Street, 10001
      • 100 Beta Street, 10001
      • 100 Alpha Street, 10002

Driver

Write a class named Driver that creates multiple instances of the Address class, and verifies the toString, equals, and hashCode methods are working properly.

Strategically add your instances to an array in an unsorted order.  Then call Arrays.sort() and print the contents of the array to see if the array is in sorted order which will indirectly test your compareTo method.

April 7 Assignment

Copy your Tuple and Pair classes from /csci102/labs/apr6 into csci102/hw/apr7.

Write a class named Point3D that models a point in 3D space.  The class should extend Tuple and implement the Comparable interface.

    • The compareTo method should return 1 if the distance between the instance on which compareTo is called and (0,0,0) is larger than the distance between the instance passed into the method and (0,0,0).
    • The compareTo method should return -1 if the distance between the instance on which compareTo is called and (0,0,0) is smaller than the distance between the instance passed into the method and (0,0,0).
    • The compareTo method should return 0 if the distance between the instance on which compareTo is called and (0,0,0) is equal to the distance between the instance passed into the method and (0,0,0).

Write a class named Driver that creates multiple instances of Point3D and tests the methods in the Point3D and Tuple classes.

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.