Assignment 3

Due Date : 10/2 @ 11:59pm

Instructions

Each of you should have a repository in GitHub with the name assignment3-githubHandle. Use this repository and add all your work there. In order to clone the repositories from GitHub to your machine (or watch this video video instructions):

  1. Sign in to GitHub.

  2. On your GitHub home page your Github Handle should appear on the left as a button/drop down menu. Click that button and from the drop down menu select cs2500f14. The GitHub page will then navigate to the class' web page and to the contents that is available to you.

  3. On the right you should be able to see all repositories to which you have been given access. Find the repository whose name matches the pattern assignment3-githubHandle, where githubHandle is your GitHub account name, e.g,assignment3-john123, and click on it.

  4. On the repositories home page look at the bottom right hand corner for a button with the text Clone in Desktop. By clicking on this button your browser will launch the GitHub client that we installed in class and ask for a location on your drive to store the repository.

    If you are not using the GitHub client the clone URL is located above the Clone in Desktop button. Copy the URL and issue the following command on your shell git clone URL.

  5. Create a file and save it under the folder on your drive that you selected in the preceding step.

Remember to push your changes to the GitHub repository often.

Note

For each question that asks you to design a program you are expected to follow the Design Recipe in the same way as we did in class. Failure to show all your work for each step will cost you points. All tests should use check-expect.

Problem 1

Consider the following structure definitions:

  1. (define-struct movie (title producer year))
  2. (define-struct pet (name age))
  3. (define-struct player (name number))
  4. (define-struct CD (artist title price))
  5. (define-struct sweater (material size producer))
For each structure definition
  1. Provide the names of the constructor and selector functions.
  2. Provide the contract for each constructor and selector function.
  3. Provide templates (one for each structure definition).

Problem 2

For this problem ensure that you have added the following line at the top of your file

(require 2htdp/image) 
Here is a data definition for keeping track of time on a clock (which, of course, is just a computer with a display attached to it):
;; Hours is a Number that is always between 0 and 11 
;; Minutes is a Number that is always betwee 0 and 59
;; Time is (make-time Hours Minutes)
;; interpretation: represent time as hours and minutes 
(define-struct time (hours minutes))
Use these definitions for the following exercises:
  1. Design the function tock from Time to Time. It adds one minute to the given time.
  2. Design the function time->text, which converts a time to a text. The text should look like the display of a common alarm clock, i.e., it should separate the minutes from the hours with a colon. Hint: a text is an image, not a string, but you will need a string version of the time, too. See HelpDesk here for more on the text function.

Problem 3

You just joined an online retailer and you have been assigned to work on their online billing system. The team you are joining has already worked out a data definition for transactions

;; A TransactionId is Number greater than 0
;;
;; An Amount is a Number 
;;
;; An AccountId is a Number greater than 0
;;
;; A Transaction is (make-transaction TransactionId Amount AccountId Boolean)
;; interpretation: holds the transaction id, amount for the transaction, 
;;                 account id affected by the transaction and 
;;                 whether or not it has been processed or not. 
(define-struct transaction (trId amount accId processed?))

  1. The online store has noticed that sometimes, due to latencies, there are duplicate transactions because of system retries. Design a program that given two transactions returns true if all fields except trId hold the same values, and returns false otherwise.
  2. Design a program that given a transaction if the transaction has been processed then return the transaction intact, else if the transaction is not been processed (i.e. processed? is false) and the balance on the account with account Id accId
    • if the account's balance is greater or equal to the transaction amount return a new transaction with the same values as the original except for processed? which should now be true
    • if the account's balance is less than the transaction amount return a new transaction that holds the same values as the original except for amount which will hold the remaining amount of money needed to complete the transaction, for example (make-transaction 111 2000 1234 false) and an account balance of 1000 returns (make-transaction 111 1000 1234 false)
  3. When a new credit card is used on the site for a purchase the system creats two transactions, one transcation for the amount of $1 and another transaction for the remaining amount of the original transaction. The reason is so that if the first transaction fails the company does not incurr any costs and can stop the second transaction. If the first transaction succeeds then the second transaction is processed.

    These two sub-transactions are represented differently in the system

    ;; A SubTransactionId is a Number greater than 0
    ;; A SubTransaction is 
    ;;     (make-sub-transaction (SubTransactionId  TransactionId Amount AccountId Boolean))
    ;; interpretation:  the sub-transaction Id, the transaction Id that
    ;;                  this sub-transaction is a part of, the sub-transaction amount
    ;;                  the account id and whether it has been processed or not. 
    (define-struct sub-transaction (subtrId trId amount accId processed?))
     

    This process however yields two transactions in the customer's statement. You are asked to design a program that takes in two processed sub-transactions and returns one processed transaction. For example when given (make-sub-transaction 12 1234 1 789 true) and (make-sub-transaction 12 1234 199 789 true) returns (make-transaction 1234 200 789 true). Note: that the two sub-transactions have been processed and they have the same trId. Also note that the transaction returned by the program has the same trId as the trId of both sub-transactions. Your program is not expected to deal with inputs that are either not processed or sub-transaction of the different transactions.