Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have literally no idea how to do this problem. If you manage to get it correct

ID: 3546961 • Letter: I

Question

I have literally no idea how to do this problem. If you manage to get it correctly please explain how each method was done. Thank You



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:

None of these sums is negative, so the method would produce the following message and return false:

If the file instead contains the following numbers, the method finds that a negative sum of -8 is reached after adding 6 numbers together (14 + 7 + -10 + 9 + -18 + -10):

It should output the following and return true, indicating that a negative sum can be reached:

Explanation / Answer

import java.util.*;
import java.io.*;
public class negativeSum_test
{
public static boolean negativeSum(Scanner in)
{
int sum=0;
int count=0;
while(in.hasNextLine()) // for every line start reading number
{
int value = in.nextInt(); // get next value in file.
sum = sum +value; // add to sum/
count++; // increment counter.
if(sum < 0) // if sum is negative. go inside and print the results and return true.
{
System.out.println(" "+ sum + " after " + count +" steps");
return true;
}
}// if sum not negative
// print no negative sum and return false.
System.out.println("no negative sum");
return false;
}
public static void main(String[] args) throws FileNotFoundException
{
    File file = null;
try
{
// try opening file here.
file = new File("input.txt");
// if file doesnt exist throw excpetion.
if(!file.exists()) throw new FileNotFoundException();
}
catch(FileNotFoundException FFE)
{
// print file not found message here.
System.out.println("File not found ");
}
// come here and create Scanner class to read from file.
Scanner in = new Scanner(file);
// pass Scanner object to negativeSum METHOD.
negativeSum(in);
}
}