Write a method named negativeSum that accepts a Scanner as a parameter reading i
ID: 3658848 • Letter: W
Question
Write a method named negativeSum that accepts a Scanner as a parameter reading input from a file containing a series of integers, and determine whether the sum starting from the first number is ever negative. The method should print a message indicating whether a negative sum is possible and should return true if a negative sum can be reached and false if not. For example, if the file contains the following text, your method will consider the sum of just one number (38), the sum of the first two numbers (38 + 4), the sum of the first three numbers (38 + 4 + 19), and so on up to the sum of all of the numbers:Explanation / Answer
valueOf public static Integer valueOf(String s) throws NumberFormatException Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string. In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s)) Parameters: s - the string to be parsed. Returns: an Integer object holding the value represented by the string argument. Throws: NumberFormatException - if the string cannot be parsed as an integer. valueOf public static Integer valueOf(int i) Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range. Parameters: i - an int value. Returns: an Integer instance representing i. Since: 1.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.