$30
CSE110 Principles of Programming Assignment 03::100 pts
Assignment 03 – Automatic Stock Trader
You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course.
Assignment Objectives
After completing this assignment the student should be able to:
● Write programs that make decisions
● Demonstrate the use of if statements in Java
● Use relational and logical operators to compute desired values
Assignment Requirements
For this assignment you are given the following files:
Assignment03.java (you must complete this file)
Problem Description and Given Info
Within the main method in the Assignment03.java file, you must design and write a program to determine whether to
buy, sell, or hold shares in a stock market.
Your program must prompt the user to enter the 4 input values described below. It must collect the user’s input and
store these values in 4 different variables. It must collect the inputs in the order shown below.
Your program must determine whether the user should buy, sell, or hold their shares based on the input data. It must
print out an appropriate message to the user. The output must be formatted exactly like the Expected output examples
shown below.
Inputs
1. Current Shares - This is the number of shares of this stock currently held in the account
2. Purchase Price - (per share) paid for current stock in the account
3. Market Price - (per share) of this stock. This is the current market price for buying or selling this stock
4. Available Funds - the amount the client is willing to spend on a transaction
Outputs
1. Text containing the message to either buy, sell, or hold (see examples below)
Other Details
Any transaction (buy or sell) costs $10. Be sure to account for this transaction fee in your profitability calculations. We
cannot buy or sell if we cannot pay this $10 fee. Note that this fee can be taken out of the profit from a sell (see below).
Each time we sell, it will cost us the $10 transaction fee.
Each time we buy, it will cost us the $10 transaction fee, plus the cost of the shares we are buying:
𝑡𝑜𝑡𝑎𝑙𝐵𝑢𝑦𝐶𝑜𝑠𝑡 = 10 + 𝑚𝑎𝑟𝑘𝑒𝑡𝑃𝑟𝑖𝑐𝑒 ∙ 𝑛𝑢𝑚𝑏𝑒𝑟𝑂𝑓𝑆ℎ𝑎𝑟𝑒𝑠𝑇𝑜𝐵𝑢𝑦
The number of shares we can afford to buy would be:
𝑛𝑢𝑚𝑏𝑒𝑟𝑂𝑓𝑆ℎ𝑎𝑟𝑒𝑠𝑇𝑜𝐵𝑢𝑦 = 𝐹𝐿𝑂𝑂𝑅(
𝑎𝑣𝑎𝑖𝑙𝑎𝑏𝑙𝑒𝐹𝑢𝑛𝑑𝑠−𝑡𝑟𝑎𝑛𝑠𝑎𝑐𝑡𝑖𝑜𝑛𝐹𝑒𝑒
𝑚𝑎𝑟𝑘𝑒𝑡𝑃𝑟𝑖𝑐𝑒 )
In order for a purchase (buy) to be considered profitable, the current market price (per share) must be lower than the
purchase price (per share) paid for current stock in the account. Additionally, the amount the client is willing to spend on
a purchase must allow us to buy enough shares so that the difference in value will cover the $10 transaction fee.
In other words, if the current market price (per share) is lower than the purchase price (per share) paid for current stock
CSE110 Principles of Programming Assignment 03::100 pts
P.Miller 2
in the account, then there is a potential per share value that is equal to the price difference:
𝑝𝑒𝑟𝑆ℎ𝑎𝑟𝑒𝐵𝑢𝑦𝑉𝑎𝑙𝑢𝑒 = 𝑝𝑢𝑟𝑐ℎ𝑎𝑠𝑒𝑃𝑟𝑖𝑐𝑒 − 𝑚𝑎𝑟𝑘𝑒𝑡𝑃𝑟𝑖𝑐𝑒
Assuming that we have the available funds to buy enough shares (and pay the $10 transaction fee), we should buy if the
total value of the shares is greater than the $10 transaction fee. The total value of the shares is the per-share value
times the number of shares we can afford to buy:
𝑡𝑜𝑡𝑎𝑙𝐵𝑢𝑦𝑉𝑎𝑙𝑢𝑒 = 𝑝𝑒𝑟𝑆ℎ𝑎𝑟𝑒𝐵𝑢𝑦𝑉𝑎𝑙𝑢𝑒 ∙ 𝑛𝑢𝑚𝑏𝑒𝑟𝑂𝑓𝑆ℎ𝑎𝑟𝑒𝑠𝑇𝑜𝐵𝑢𝑦
In order for a sale (sell) to be considered profitable, the current market price (per share) must be higher than the
purchase price (per share) paid for current stock in the account. Additionally, the value gained by selling the shares must
also cover the $10 transaction fee.
In other words, if the current market price (per share) is higher than the purchase price (per share) paid for current stock
in the account, then there is a potential per share value that is equal to the price difference:
𝑝𝑒𝑟𝑆ℎ𝑎𝑟𝑒𝑆𝑒𝑙𝑙𝑉𝑎𝑙𝑢𝑒 = 𝑚𝑎𝑟𝑘𝑒𝑡𝑃𝑟𝑖𝑐𝑒 − 𝑝𝑢𝑟𝑐ℎ𝑎𝑠𝑒𝑃𝑟𝑖𝑐𝑒
In this case, we should see if we have enough shares of this stock so that the total value of the shares is greater than the
$10 transaction fee. The total value of the shares is the per-share value times the number of shares we currently have:
𝑡𝑜𝑡𝑎𝑙𝑆𝑒𝑙𝑙𝑉𝑎𝑙𝑢𝑒 = 𝑝𝑒𝑟𝑆ℎ𝑎𝑟𝑒𝑆𝑒𝑙𝑙𝑉𝑎𝑙𝑢𝑒 ∙ 𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑆ℎ𝑎𝑟𝑒𝑠
If neither a buy nor a sell would be profitable, then we should simply hold the existing shares.
Test Data
Test #1
Given Inputs
Current Shares : 10
Purchase Price : 100
Market Price : 1
Available Funds : 10
Expected Output
Hold shares
Rationale: Even though the current market price is very low (compared to the purchase price), after paying the
$10 transaction fee, we would not have any funds left to buy shares; so we can only hold.
Test #2
Given Inputs
Current Shares : 20
Purchase Price : 2
Market Price : 1
Available Funds : 21
Expected Output
Buy 11 shares
Rationale: After paying the $10 transaction fee, there are enough funds remaining to buy 11 shares. At a
purchase price vs. market price difference of $1 per share, our 11 shares represent a value gain of $11 dollars,
which is $1 more than the $10 transaction fee - so we come out $1 ahead.
CSE110 Principles of Programming Assignment 03::100 pts
P.Miller 3
Test #3
Given Inputs
Current Shares : 15
Purchase Price : 12
Market Price : 1
Available Funds : 12
Expected Output
Buy 2 shares
Rationale: After paying the $10 transaction fee, there are enough funds remaining to buy 2 shares. At a purchase
price vs. market price difference of $11 per share, our 2 shares represent a value gain of $22 dollars, which is
$12 more than the $10 transaction fee - so we come out $12 ahead.
Test #4
Given Inputs
Current Shares : 1
Purchase Price : 1
Market Price : 11
Available Funds : 0
Expected Output
Hold shares
Rationale: Selling our 1 share for $11 will leave us with just $1 after we pay the $10 transaction fee. That is the
same as what we paid for it, and we won't make any profit - so we should hold.
Test #5
Given Inputs
Current Shares : 10
Purchase Price : 1
Market Price : 3
Available Funds : 30
Expected Output
Sell 10 shares
Rationale: With a market price vs. purchase price vs. difference of $2 per share, we stand to make $20 from the
sale of our 10 shares. This is $10 more than the price of the transaction fee, so we will come out $10 ahead -
therefore we should sell all 10 shares.
Test #6
Given Inputs
Current Shares : 1
Purchase Price : 1
Market Price : 12
Available Funds : 0
Expected Output
Sell 1 shares
Rationale: Our 1 share is worth $11 more than we paid for it at the current market price. The $11 dollars
obtained by selling that share now will still leave us with a profit of $1 after paying the $10 transaction fee.
Profit is profit, so we should sell.
CSE110 Principles of Programming Assignment 03::100 pts
P.Miller 4
What to turn in
For this assignment you must upload the following files by the due date.
Assignment03.java
Any assignment submitted less than 24 hours after the posted due date will have 10 points deducted.
Any assignment submitted more than 24 hour after the posted due date will receive a zero in the grade book.
Grading Rubric
Criteria Points
All required files are submitted 10
Each file includes a comment header with the following information:
CSE 110 : <Class #> / <meeting days and times>
Assignment : <assignment #>
Author : <name> & <studentID>
Description : <of the file contents>
Partial credit can be awarded
Code is neat and well organized 10
Good naming conventions for all identifiers
Good use of whitespace
Descriptive comments
Partial credit can be awarded
Code compiles with no syntax errors 20
No Partial credit can be awarded
No credit will be awarded for structure or logic
if your code does not compile
Code passes structure tests 30
Code collects 4 inputs
Code uses at least 2 if statements
Code outputs a results
Partial credit can be awarded
(10)
(10)
(10)
Code passes logic tests 30
Partial credit is awarded based on number of tests passed
No credit will be awarded for logic
if your code does not pass all structure tests
See test examples (#1 - #6) above in these instructions
TOTAL 100