Java Suppose that you are given a list of daily gains (incre

问题描述:

Java
Suppose that you are given a list of daily gains (increases) in the price of a stock.The investment threshold is defined as the number of days that you must hold that stock before the accumulated gains reach a certain total.For example,if your goal is to earn $1,000,then your investment threshold for a given stock would equal the number of days you need to hold that stock before your accumulated profits reach $1,000.
Example:Suppose that a stock sees the following gains over 6 days:5,3,8,2,9,1,and that your goal is to earn a profit of $17.The investment threshold would be 4,because it takes 4 days for the accumulated gains to reach 17 (5 + 3 + 8 + 2 = 18).
(a) Write a Java method with the following header:
public static int threshold (double [] gains,double goal)
The first argument is an array of double values corresponding to the daily increases in a stock investment (i.e.,the profit you made over the previous day).The second argument is a double that represents your desired profit goal.The method returns an int representing the minimum number of days that you need in order to reach (or exceed) your profit goal.If the profit goal cannot be reached using the values in the list of increases,the method returns -1.
General algorithm:
Set earned to 0
Set days to 0
While earned < goal and you have not yet reached the end of the array:
Add the next dayʼs increases to earned
Add 1 to days
Move to the next element in the array
Decide whether to return days or -1
(b) Complete your program by writing a main() method that asks the user to enter a profit goal.You
may define your own array of stock increase values.Your program should then calculate and print out
the investment threshold for that profit goal,or state that the goal cannot be reached.
(c) Modify your program so that it works from a randomly-generated array of stock gains.
1个回答 分类:英语 2014-10-24

问题解答:

我来补答
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
 
 
展开全文阅读
剩余:2000
上一页:高数一指数函数
下一页:必修五第四单元