import cs1.Keyboard;
public class StockProfit {
// Determine best buy-sell period and maximum profit
// for a series of stock readings over time
public static void main (String[] args) {
int minPrice, // lowest price
so far
priceToday, //
current day's stock price
i,
// day when this current price was entered (1,2,3,...)
j,
// marker for a possible buy day (new minPrice found)
profit = 0, //
best profit possible so far
buyday = 0, //
best day to buy
sellday = 0; // best
day to sell
// Determine initial minimum price
System.out.print("Enter value of stock : ");
priceToday = Keyboard.readInt();
minPrice = priceToday;
i = 1; j = 1; // day 1 (also
a possible buy day)
while (priceToday!=0)
{
// Does a sell today maximize profit?
if (priceToday - minPrice >
profit)
{ profit = priceToday
- minPrice;
sellday
= i;
buyday
= j;
}
// Is today a new minimum price?
if (priceToday < minPrice)
{ minPrice = priceToday;
j = i;
}
// Next day's price.
System.out.print("Enter value of
stock : ");
priceToday = Keyboard.readInt();
i = i + 1; //
next day
}
System.out.println("Maximum profit = " + profit
+ " dollars.");
System.out.println(" buy on day " + buyday);
System.out.println(" sell on day " + sellday);
}
}
The program does not know in advance how many values will be entered, but only that a mouse click in the close box of the Java Output window signals that there is no more input. For example, if the values 2 6 3 0 are entered on separate lines (signifying values of $2, $6, and $3 on three consecutive days of trading), the program should display the following answer:
Maximum profit = 4 dollars.
buy on day 1
sell on day 2
Drag a copy of MyProject from the CS105 (Tucker) server to your desktop and drag a copy of the program StocProfit.java from the server into this desktop copy. Now add the program StockProfit.java to your project in place of the test.java program that is there. (Don't forget to set your Edit -> MyProject Debug Settings -> Main Class to StockProfit before running this program.)
The following steps will help you become acquainted with the CodeWarrior Debugger. This is a useful tool for diagnosing the run-time behavior of a program step-by-step and discovering run-time errors, which are often the most difficult to find.
Step 1 - Start a project and enable the debugger
In this project, select Enable Debugger and then Debug under the Project menu. You should see a window like the following open up:
The Debugger can be used to "single step" your program -- that is to run it one instruction at a time, beginning with the first. Notice the blue arrow in the bottom window of this picture - it points to the instruction in your Java program that is about to be executed.
Step 2- Exercise the Debugger
Now the right-pointing green arrow at the top of this picture will cause program to execute one line of Java code and the blue arrow will move down to the next line. Notice also the window in the upper righthand corner that displays all the variables in your program - minPrice, priceToday, profit, etc.
Whenever a step calls for Keyboard input, be sure to type that input in
the Java Console window (not the Java Output window, where you would
normally type input when you are not running the debugger).
Each time a while loop is repeated, the blue arrow will move back to be beginning of the loop. Now single step this program by repeatedly clicking the right-pointing green arrow several times, and watch the blue arrow move and the values of the variables change as each step executes.
1. Run the program with the following input:
17
18
16
17
21
15
16
0
(Be sure to terminate the input with a 0). What is the output of this run?
2. What is the value of each of the variables profit , i, j, buyday, and sellday just before each repetition of the while loop?
3. Briefly explain the need for a while loop in this problem. That is, why can't the problem be solved without a loop?
4. Answer exercises 3.4ace, 3.6ace, 3.7ace, and 3.8ace.
The purpose of this program is to provide an answer for Paulo to the question of which house's coordinates are closest to city A where he works, given the location of A along with the names and locations of an arbitrary number n of cities on the map. City names may be entered as Strings, and can be read by:
String cityName = Keyboard.readString();
Step 1 - Design a Program to Compute the Shortest Distance
Determine the general nature of the input to this program, as well as the
output and the kind of computation that is needed as each city's location
is read as input in order to develop the correct output. Recall that
the Distance between two points (x1, y1) and (x2, y2
) can be computed as follows:
________________________
Distance = \/ (x2 - x1)2 + (y2
- y1)2
From this information, determine the variables that your program needs, as well as any Math library functions that would be helpful.
Step 2 - Type and Run the Program
When you type your program, save it with an appropriate program name, lab3yourname.java. Be sure to add this program to your project in place of the test.java program. Now Run your program with the input values suggested in the example diagram above, as well as other input values. If you run into difficulties and your program doesn't give the correct answers, use the Debugger that you learned about in Part 1 of this lab to help discover what's happening.
Step 3 - Answer the following questions
1. What are the coordinates of the best city for Paulo to live, given the data above, and what is Paulo's commuting distance to that city?
2. What would be your answer to question 1 if housing were only available in cities C, E, F, and G?
3. Suppose the data were changed so that an eighth city, H, were added at location (0,0). How would your answers to question 1 change, if at all?
4. Suppose the problem were slightly different. That is, suppose
Paulo had to choose the city that is farthest away from city A were he worked,
assuming that city A was quite polluted so that the farther away he lived
from his job the better would be the quality of his life. Describe in detail
how your original program would need to be changed to accommodate this slightly
different requirement. Would this be a major change or a minor one?