CS 1110 Summer 2008 h/w 2 ... due Thursday 17th July
The point of these questions is to end up with as short a main method as possible by delegating
as much of the work as possible to reusable methods, and to learn to write classes. Considering how much
you'll probably have to spend thinking and making sense of these questions, do start early.
- Elaborate on the banking Account and Economy material from class to build a simple model of a small economy,
being careful to exploit as much privacy and security as you can at this stage. Your main method (in the Economy
class) should allow for a reasonable test run of your economic model. You should create classes for People, Things,
Shops, and Banks, and aim to build a simple program to allow People to buy Things from each other or from Shops,
with everyone using bank Accounts to deal with money (ie, no cash). People should be allowed to both save and
borrow money (but only from Banks, having distinct accounts for saving and borrowing. We will re-visit this
question in a couple of weeks to enhance it, so it would be worth thinking ahead when desigining your
solution.
- This question focusses on writing methods to manipulate arrays (all arrays will be arrays of doubles). You should
write a program which has a class Matrix and a class Vect having appropriate fields, methods and
constructors to do each of the following, testing it by having a main method which instantiates some Matrixes
and Vects, applying these methods to them. You should arrange either that Matrix inherits from
Vect or vice versa, whichever you decide is the most appropriate.
(Notice that it might make sense to create additional methods to help out, for example a method to compare two arrays
and return a boolean depending on whether or not they have the same length.)
- a method ip which when given a 1d array, checks to see if it has the same length as the 'host'
array, and if so, returns a double which is the sum of the products of their terms. E.g., if called from a Vect [2,4,8]
and given a Vect [4,12,-6] this method would return 8 (by calculating 2x4 + 4x12 + 8x(-6) ).
- a method norm which uses ip internally so that when called from a 'host' 1d array it
returns a number which is the (positive) square root of the sum of the squares of its terms. E.g., if called from a
Vect [4,12,-6] it would calculate a double which is the square root of 4^2 + 12^2 + (-6)^2 to return 14.
- a method add which when given a 1d array, checks to see if it has the same length as the 'host' array,
and if so, returns a 1d array whose terms are the sums of the given arrays' terms. E.g., if called from a Vect or
Matrix [2,4,8] and given a Matrix or Vect [4,12,-6] this method would return a Matrix or Vect [6,16,2].
- another method add which when given a 2d array, checks to see if it has the same length and 'width' as
the 'host' 2d array, and if so, returns a 2d array whose terms are the corresponding sums of the original arrays' terms.
- a method times which when given a 2d array, checks to see if the host array's 'widths' are all the
same, then that the given array's 'widths' are all the same, then that the 'width' of the host array equals the
length of the given one, and if so, returns a 2d array whose terms are obtained by applying ip to each
'row' of the host array with each 'column' of the given one. E.g., if called from a Matrix
[ 2 4 8 and given a Matrix [ 7 12 this method would return the Matrix with 2 by 2 array [ 30 60
3 5 9 ] -6 25 36 89 ]
5 -8 ]
via the calculation ...
[ 2x7 + 4x(-6) + 8x5 2x12 + 4x25 + 8x(-8)
3x7 + 5x(-6) + 9x5 3x12 + 5x25 + 9x(-8) ].
- other versions of times should allow a Vect to multiply a Matrix and a Matrix to multiply a Vect.
You should test your program by allowing the entry of numerical arrays from a test file you build called testdata.txt,
and displaying the results on the screen.