作业帮 > 英语 > 作业

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

来源:学生作业帮 编辑:大师作文网作业帮 分类:英语作业 时间:2024/11/12 07:23:36
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.
Java Suppose that you are given a list of daily gains (incre
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0)