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

need help with java! Assignment #2 will be the construction of a program that re

ID: 3661950 • Letter: N

Question

need help with java!

Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The minimum integer is 0

The count of even integers in the sequence is 1

The sum of negative integers is 0

This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, count how many even integers (can be divided by 2, "num%2 == 0") are in the sequence, and compute the sum of negative integers (are less than 0).

Note that the above is an output for the first test case. For other test cases, you will have different numbers.


Explanation / Answer

The following program keeps taking the input from the user untill he presses 0.It counts the number of even integers (evencount) including the negative even integers and the sum of negative integers(sum_negative).The program stops once the user enters 0 as the integer. This helps creating a while loop with condition till data is not equal to 0 execute it and keep inputtng numbers.

public class computations {

public static void main (String[] args) {

//name integers;

int data;

int sum_negative = 0;// sum of negative integers

int evencount = 0;// number of even integers

//Create Scanner     

Scanner input = new Scanner(System.in)

// initial input and continue until 0 is entered

System.out.print ("Enter an int value (The program exits if the input is 0): ");

data = input.nextInt();

while(data != 0){

if(data>0) {

             if(data%2==0){ // if it is even then increase even count

              evencount++;

               }

         else

     { System.out.println(“ it is an odd”);

     }

}

elseif(data <0){

sum_negative +=data; // if it is negative add the number to the sum count.

if(data %2==0) // if it is even and negative add to even count again

{ evencount++;

} else

{ System.out.println(“ enter the next number”);

data = input.nextInt();

}

}

}

System.out.println(“ the minimum number entered is 0”);

System.out.println ("The sum of negatives is " + sum_negative);

System.out.println ("The number of even numbers is " + evencount);

}

}

OUTPUT:

Enter an int value (The program exits if the input is 0):   1      4    6      -4     -6     0

The minimum number entered is 0

The sum of negatives is -10

The number of even numbers is 4