Question: Exam prep: Using Java programming 1. Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of integers,

Exam prep: Using Java programming

1. Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of integers, and prints a message to the console indicating whether the sum starting from the first number is ever negative (you can assume that the Scanner has already been initialized to read from the file). The negativeSum method should return true if a negative sum is reached and false if not. For example, a file containing: 38 4 19 -27 -15 -3 4 19 38 would result in a message No negative sum displayed in the console and the method would return false. If the file contained: 14 7 -10 9 -18 -10 17 42 98 the method should display sum of -8 after 6 steps and return true.

To help answer this question, I include the solution to a simpler question : display the contents of a file of integers:

public static void fileContents(Scanner input)

{

while (input.hasNextInt())

{

System.out.println(input.nextInt());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!