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

I need help with the construction of a program that reads in an unspecified numb

ID: 3875053 • Letter: I

Question

I need help with 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 largest integer that is divisible by 2 is 0
The count of even integers in the sequence is 1
The sum of positive integers is 0

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

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

Do not prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.

I already have most of the code done, I just need to figure out how to find the largest integer divisible by 2. I have no idea how to do that. Here is my code below:

import java.util.Scanner;

public class Assignment2

{

public static void main (String[] args)

{

int even=0; //variable to store total of even integers

int nnum=0; //variable to store sum of positive integers

int count=0; //variable for positive integers count

int max=0;   

int num; //variable to store keybaord input

Scanner scan = new Scanner(System.in);

do {num = scan.nextInt();

if (num%2== 0 && num>0) //finds even and positive integers and adds to even and nnum

{

even+=num;

nnum+=num;

if (max > num)

{max = num;}

}

else if (num < 0) //finds negative integers and adds to nnum

{

nnum+=num;

if (max > num)

{max = num;}

}

else if (num%2!= 0 && num>0) //finds odd and positive integers and adds to count and odd

{

count++;

even +=num;

if (max > num)

{max = num;}

}

else //finds any other positive integer and adds to count

{

count++;

if (max > num)

{max = num;}

}

}

while (num!=0);

System.out.println("The minimum integer is " + max);

System.out.println("The largest integer divisible by 2 is " + nnum);

System.out.println("The sum of positive integers is " + even);

System.out.println("The count of even integers in the sequence is " + (count)); //had to subtract one because count would always come up 1 over

}

}

I just need to figure out what code i need to add to find the largest integer divisible by 2 without using arrys, I will definitely rate your anser highly if it works. Thank you

Explanation / Answer


Given below is the modified code. I have modified the variable names so they indicate what they store. Hope you find the answer helpful.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i

import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
int evenCount = 0; // variable to store count of even integers
int sumPositive = 0; // variable to store sum of positive integers
int minimum = 0; // variable to store minimum
int largestEven = 0; //variable to store largest even number
int num; // variable to store keybaord input
System.out.println("Enter a sequence of numbers (type 0 when done)");
Scanner scan = new Scanner(System.in);
do {
num = scan.nextInt();
if (num % 2 == 0 ) // the number is even?
{
evenCount++; //count hte even number
if(num > largestEven) //is this the largest even number
largestEven = num;
}
if (num < minimum) // is this the minimum number?
minimum = num;
if(num > 0) //is it a positive number? add it to sumPositive
sumPositive += num;
}
while (num != 0);
System.out.println("The minimum integer is " + minimum);
System.out.println("The largest integer divisible by 2 is " + largestEven);
System.out.println("The sum of positive integers is " + sumPositive);
System.out.println("The count of even integers in the sequence is " + evenCount);
}
}


output
=======
Enter a sequence of numbers (type 0 when done)
0
The minimum integer is 0
The largest integer divisible by 2 is 0
The sum of positive integers is 0
The count of even integers in the sequence is 1

------
Enter a sequence of numbers (type 0 when done)
2
-5
8
1
5
9
10
0
The minimum integer is -5
The largest integer divisible by 2 is 10
The sum of positive integers is 35
The count of even integers in the sequence is 4