Assignment 2
Due Date : 9/25 @ 11:59pm
Instructions
Each of you should have a repository in GitHub with the name
assignment1-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):
-
Sign in to GitHub.
-
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. -
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
assignment1-githubHandle
, where githubHandle is your GitHub account name, e.g,assignment1-john123
, and click on it. -
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
. - 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
One of the operators in logic is the if operator. Design a program that consumes two boolean variables and returns a boolean. The program should have the same behavior as the logical if operator. For your reference, here is the truth table for if
A | B | if |
---|---|---|
true | true | true |
true | false | false |
false | false | true |
false | true | true |
HINT: do not name your function if
,
DrRacket has a built in function named if
and will
not allow you to re-define it.
Problem 2
Design a program that given a number grade for a student returns back the letter grade as a string. Here is the table with the mapping from number grades to letter grades.
Number Grade (g)
|
Letter Grade |
---|---|
95 < g <= 100 | A+ |
90 < g <= 95 | A- |
85 < g <=90 | B+ |
80 < g <= 85 | B- |
75 < g <= 80 | C+ |
70 < g <= 75 | C- |
60 < g <= 70 | D |
g <= 60 | E |
Problem 3
You are hired as a contractor for a small gaming company. You are asked to design a program that given the current position of the car, its speed and the car's health will calculate the new position of the car.
The car can only move horizontally, and only from left to right. The position of the car is given as a number that indicates the car's position on the x-axis. The speed of the car is given as a number representing the number of pixels to move to the right per unit time. The health of the car is given as a one of three symbols,
- either
'good
- or,
'bad
- or,
'superdrive
The speed of the car is affected by the car's health.
When the car's health is 'good
the car travels
at the speed given, when the car's health is 'bad
the car travels at half the speed given, and, when the car's
health is 'superdrive
the car travels at double
the speed given.
Problem 4
The gaming company is also working on another game and is now asking you to design a program that will consume two coordinates and report back one of the following three values
'aim
'fire
'load
A coordinate will be given as two numbers one for x and one for y coordinates, your program should thus allow for 4 inputs. The behavior of your program is defined by the following rules
-
if the first coordinate is closer to the origin (0,0)
than the second then the program should respond with
'aim
-
if the first and second coordinate are equidistant from the
origin (0,0), then the program should respond with
'fire
-
if the first coordinate is further from the origin (0,0)
than the second coordinate, then the program should respond
with
'load
Problem 5
You are helping out a friend who works in the stock exchange. Your friend would like to automate the actions taken for certain stocks with a program and is asking you to design the program for him.
The company has bought some stock at a given price. During a trading window brokers observe the current price of the stock and the expected behavior of the stock. Based on these inputs brokers decide to either sell, buy or stay (do nothing).
The expected behaviour of the stock is one of
'up
'down
'no-change
The rules in detail are:
-
if the current stock price is more than $5 difference than
the price at which the stock was purchased, and, the expectation is
'up
then return'sell
-
if the current stock price is equal or less than $5 difference
from the price at which the stock was purchased, and, the expectation
is
'up
then return'stay
-
if the current stock price is more than $2 difference from
the price at which the stock was purchased, and, the expectation is
'down
then return'sell
-
if the current stock price is less than or equal to $2
difference from the price at which the stock was purchased, and, the
expectation is
'down
then return'buy
-
if the current stock price is greater than $5 difference
from the price at which the stock was purchased, and, the expectation
is
'no-change
then return'sell
-
if the current stock price is greater than $2 and less or
equal to $5 difference from the price at which the stock was purchased,
and, the expectation is
'no-change
then return'stay
-
if the current stock price is less than or equal to $2
difference from the price at which the stock was purchased, and, the
expectation is
'no-change
then return'buy
Your program should consume the purchased price, the current
price, the expected behavior and then based on the preceding
rule return 'sell
, or, 'stay
, or
buy
accordingly.
Problem 6
An online store has hired you to help out with their fraud detection software. The online store accepts credit cards for payment. The store however follows certain rules depending on the history of the credit card and the amount to be charged on the credit card and reports one of three results
'proceed
; charge the card and complete the transaction'pending
; stop the transaction and pass it on for further investigation'reject
; fail the transaction
'none
'good
'bad
-
if the card is rated
'none
and the amount to charge is less than $100, then return'proceed
-
if the card is rated
'none
and the amount to charge is greater or equal to $100, then return'pending
-
if the card is rated
'good
and the amount to charge is less than or equal to $200, then return'proceed
-
if the card is rated
'good
and the amount to charge is greater than $200 and less than $500, then return'pending
-
if the card is rated
'good
and the amount to charge is greater or equal to $500, then return'rejected
-
if the card is rated
'bad
and the amount to charge is less than $100, then return'pending
-
if the card is rated
'bad
and the amount to charge is greater or equal to $100, then return'reject
Design a program that would consume a card's rating and the amount to be charged and will produce results based on the preceding rules.