3 problems, 25 points for #1 and #2, Extra Credit for #3
Time Travel Securities, Inc., (NYSE: TTS) has contracted you to write a program that will help it analyze prices and provide advice to its customers. The company is interested in a number of different statistical measures of stock prices -- and, in particular, in helping its customers plan when to buy and sell stocks before sending them back in time to actually do so.
Your program should begin by prompting the user for a number (an
int) of stock prices to be considered. The user is then prompted to
enter that many prices, which are stored in an array of doubles of
the appropriate size. You may assume that these are the daily closing price of a
particular security, starting with Day 0. You may also assume they will all be
valid prices (nonnegative doubles).
(0) Print prices and days (1) Sum (2) Average (3) Standard Deviation (4) Print minimum price (5) Print the minimum price's DAY (6) Print maximum price (7) Print the maximum price's DAY (8) Print the best investment strategy! (9) QuitIf the user picks a valid choice, that action should be performed. Otherwise an error should be displayed. In either case, the menu should then be redisplayed and the process repeated, until the user chooses the Quit option (number 9).
All of the options should be clear with the exception, perhaps, of number 8. We will go over several of the options in class; the sample shows examples of each option. Here is an explanation of option #8:
Option 8Thus, your program should print the day and the value of the stock price at which the user should BUY the stock. Then, it should print the day and the value at which the user should SELL the stock in order to obtain the maximum possible profit on this stock. The profit that will be made should also be printed.
Note that you can not sell a stock before you buy it! Note also that we are limited to ONE transaction (clients don't want to lose any more of their capital to brokers' fees than necessary, after all!)
Here is an example. Suppose the stock prices are as follows:
Then, the correct output for option #6 would be
Day 0: 90.0
Day 1: 10.0
Day 2: 60.0
Day 3: 42.0
Day 4: 75.0
Day 5: 70.0
Note that you are allowed to buy and sell on the same day, so the
minimum profit that you can make is 0.0, even if the stock does nothing but
drop, which is always a possibility... .
You should buy the stock on day #1 at a price of 10.0
You should sell the stock on day #4 at a price of 75.0
You will make a per-share profit of 65.0
The main method of your solution will start with the code for
inputting the initial data from file and storing it in an array, followed by a loop that
displays the menu using the first method defined below, get's the user's
choice, and processes it.
For full credit your solution must include and use the following
methods. Remember that each method needs a comment of the following form: (These are worth about 15 of the problem's 25 points).
// method: menu simply shows the user the set of program options
// inputs: no inputs
// outputs: no return values
public static void printMenu() public static void printPrices(double[] A)
Note: Methods 1 through 7 should NOT interact with the user at all. That is, they should not ask for input nor should they print anything. The printing should be done in main
public static double sum(double[] A)
public static double average(double[] A)
sum method to do most of the work. Please format your output
to use 2 places of precision with H.fmt.
public static double stdev(double[] A)
average function to do some of the work. Please format your output
to use 2 places of precision with H.fmt.
The standard deviation of an array A of length L
is 
where the symbol
Aav stands for the average of the array
A. Note that the squaring happens before the summation, not
after. The statistically savvy will recognize that this is sample,
not the full population, formula for standard deviation.
public static double min(double[] A)
42 31.5 90 62 this function should return 31.5.
public static int minIndex(double[] A)
42 31.5 90 62 this function should return 1
which is the index of the value 31.5 (since the value 42 has
index 0).
public static double max(double[] A)
42 31.5 90 62 this function should return 90.
public static int maxIndex(double[] A)
42 31.5 90 62 this function should return 2
which is the index of the value 90 (since the value 42 has
index 0).
Note: you may assume that the values will all be distinct so that there will never be more than one minimum or one maximum.
Here is a single run from a sample program, as a guide to a suitable
interface (the user's input is in blue). Notice that you don't have to type the
decimal point -- as long as you use H.nd(), the input will be
treated as a double. Note, too, that the input from the file is automatically
output when your program runs (you do not need to do that yourself).
Welcome to time-travel securities!
How many stocks would you like to analyze?
6
Please enter the prices:
90.0
10.0
60.0
42.0
75.0
70.0
Main Menu:
(0) Print prices and days
(1) Sum
(2) Average
(3) Standard Deviation
(4) Print minimum price
(5) Print the minimum price's DAY
(6) Print maximum price
(7) Print the maximum price's DAY
(8) Print the best investment strategy!
(9) Quit
Which choice would you like? 0
Day 0 Price is 90.0
Day 1 Price is 10.0
Day 2 Price is 60.0
Day 3 Price is 42.0
Day 4 Price is 75.0
Day 5 Price is 70.0
< MENU HERE >
Which choice would you like? 1
The sum is 347.0
< MENU HERE >
Which choice would you like? 2
The average price is 57.83
< MENU HERE >
Which choice would you like? 3
The standard deviation of the prices is 28.35
< MENU HERE >
Which choice would you like? 4
The minimum price is 10.0
< MENU HERE >
Which choice would you like? 5
The minimum price occurs on day 1
< MENU HERE >
Which choice would you like? 6
The maximum price is 90.0
< MENU HERE >
Which choice would you like? 7
The maximum price occurs on day 0
< MENU HERE >
Which choice would you like? 8
You should buy the stock on day #1 at a price of 10.0
You should sell the stock on day #4 at a price of 75.0
You will make a per-share profit of 65.0
< MENU HERE >
Which choice would you like? 10
That is not a valid choice.
< MENU HERE >
Which choice would you like? 9
Until yesterday, happy investing!
Submission and Extra Credit
Be sure to submit your CS5App.java file under homework 8,
problem 1. For extra credit, you should implement a file-reading
option on the menu that replaces the user's prices with
those from a file containing first the number of stock prices,
followed by those prices. Assume that all values are
separated by whitespace. (So
H.nd() will work correctly.)
For this problem, you are to implement a one-dimensional game of Lights Out. Briefly, the game consists of a row of lights that can be either on or off. By choosing a particular light, you switch its state and the state of its neighbors. That is, you turn the chosen light from off to on or from on to off, and you turn each neighbor from off to on or from on to off, depending on their initial states. The game is won when all of the lights are turned off.
For example, if there were 6 lights in the game in the following state (dark
squares indicate "off", light squares indicate "on"):
and the user selected light number 2, then the
resulting state would be
If the user then selected light number 0, the resulting state would be
Your program should first prompt the user for a number of lights. This must be between 3 and 15 (inclusive). If it is not in that range, your program should enter a small loop in which it continues to prompt the user for an integer in the valid range until one is typed. You may assume the user will always input an integer.
Your program should create an array of the specified length and go through each value in the array and randomly set it to "on" or "off". This randomization is for each value separately -- we want a random pattern of lights to begin with.
Notice! that this problem does not force you to represent the lights
in any particular manner -- you may use ints, doubles,
booleans, chars, or whatever you would like. In
addition, you are fee to choose a representation for "on" lights and "off"
lights. Also, whether or not you want to use any methods is entirely up to you.
While the first problem of this assignment is quite strict in what to do, for
this problem, you are free to design the program as you wish.
You should then print out the pattern of lights so that each "on" light is a
4x4 block of *'s and each "off" light is a 4x4 block of spaces.
Each light should be separated by vertical bars (|). After printing
the lights, you should include numbers beneath each light, starting from 0.
For instance, the final state of the example above would look like If you wish, you may want to stop printing light numbers at the
last light. However, you may also choose to print all of the possible light
numbers every time (this is easier to do). A full-sized, fifteen-light initial
configuration might look like this: | |****|****|****| |****|
| |****|****|****| |****|
| |****|****|****| |****|
| |****|****|****| |****|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
|****|****| | | |****| |****|****|****| | | | |****|
|****|****| | | |****| |****|****|****| | | | |****|
|****|****| | | |****| |****|****|****| | | | |****|
|****|****| | | |****| |****|****|****| | | | |****|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Then, you should ask the user what light they would like to select (it must be an integer). You should check to be sure that the light selected is actually valid for the current game -- if not, you should enter a small loop and continue prompting the user until a correct input is typed in. See the run below as an example.
Once a valid light number is input, your program should make the appropriate changes to the light array, and then continue by printing out the new array and prompting the user again. When the user wins the game by turning off all of the lights, the program should stop, print a congratulatory message, and quit. There is no need to prompt the user to play again; in this case, we'll let the user rerun the program if they would like to... .
Note: For some numbers of lights and some initial states, the Lights Out puzle isn't even solvable. (In fact, it's unsolvable 1/6 of the time.) Don't worry about this; it's simply to let you know... .
Here is a sample run of my program as a guide to an appropriate interface.
The user's input is in blue: Welcome to Lights Out!
How many lights would you like to have (3-15)? 10
| |****| | |****| | | | | |
| |****| | |****| | | | | |
| |****| | |****| | | | | |
| |****| | |****| | | | | |
0 1 2 3 4 5 6 7 8 9
Which light do you select? 2
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
0 1 2 3 4 5 6 7 8 9
Which light do you select? 1
|****|****| |****|****| | | | | |
|****|****| |****|****| | | | | |
|****|****| |****|****| | | | | |
|****|****| |****|****| | | | | |
0 1 2 3 4 5 6 7 8 9
Which light do you select? 1
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
| | |****|****|****| | | | | |
0 1 2 3 4 5 6 7 8 9
Which light do you select? 3
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
0 1 2 3 4 5 6 7 8 9
You win! Well done.
Be sure to submit your CS5App.java file under homework 8,
problem 2.
For extra credit (of up to 10%), include an "undo" feature in your lights out game. If the user types -1 at the prompt, your program should undo the last move the user made. If the user types -1 again, it should undo the move before that, etc., all the way back to the starting configuration of the lights.
If the user types -1 when she or he is at the starting configuration of lights, your program should simply indicate that no more "undoing" is possible and redisplay the same starting configuration again.
To do this, you will want to keep track of the user's moves in (what else...) an array! You may assume the game will not go past 1000 moves, so that your array of moves can be of size 1000.
If you do the extra credit, simply add the "undo" feature to your Hw8Pr2, and submit it as that problem (as you ordinarily would).
Extra Problem (also Extra Credit of 10 or more %)
There is an additional zip file with a program skeleton that
reads in short sound files. Supported formats include
.wav .au .aiff
and a few others... .mp3 is NOT supported, I'm afraid, though
there are lots of freely available converters out there.
The software you download demonstrates how to load sounds from
file, access the array of raw byte data that represents
the sound, and write sounds back to .wav file. Only
short sounds are supported currently (we're working on that...).
The sounds are encoded in 2-byte chunks called frames.
Your task, should you choose to accept it, is to alter the program
so that it reverses a sound and plays the reversed version.
As always, feel free to create other alterations to your sounds, too!
For more information than you might want on sound formatting, one
starting place is here.
If you do this problem and would like to submit it, please submit
it under Hw8 Pr3. Good luck!
![]() |
This page by Z. Dodds. It was built with vim on a unix box. |