Question

In: Computer Science

Assignment #2 will be the construction of a program that reads in an unspecified number of...

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 maximum integer is 0
The largest even integer in the sequence is 0
The count of negative integers in the sequence is 0
The sum of even integers is 0

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

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.

Solutions

Expert Solution

Code

import java.util.Scanner;
public class Assignment2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int maxInt=0,largestEven=0,negativeCount=0,evenSum=0;
int num;
while(true)
{
num=sc.nextInt();
if(num==0)
break;
if(maxInt<num)
maxInt=num;
if(num%2==0)
{
evenSum+=num;
if(largestEven<num)
largestEven=num;
}
if(num<0)
negativeCount++;
}
System.out.println("The maximum integer is "+maxInt);
System.out.println("The largest even integer in the sequence is "+largestEven);
System.out.println("The count of negative integers in the sequence is "+negativeCount);
System.out.println("The sum of even integers is "+evenSum);
}
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Convert the following text into a code format: The program reads an unspecified number of integers...
Convert the following text into a code format: The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user enters zero, the program computes the average of the input values, not counting the zero. At the end, the program displays the average, and...
Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
For this assignment, you are required to write a MIPS program that reads its own instructions...
For this assignment, you are required to write a MIPS program that reads its own instructions and counts the number of occurrences of each type of instruction (R-type, I-type, or J-type). To accomplish this task, your program should execute the following steps: First, your program should load the address of its first instruction (0x400000) into a register, and initialize three separate instruction class counters to zero. Next, your program should enter a loop that reads each instruction from memory. For...
Write a Java program that prompts for and reads the number N of cities or locations...
Write a Java program that prompts for and reads the number N of cities or locations to be processed. It then loops N times to prompt for and read, for each location, the decimal latitude, decimal longitude, and decimal magnetic declination. It then computes and displays, for each location, the Qibla direction (or bearing) from Magnetic North. The true bearing from a point A to a point B is the angle measured in degrees, in a clockwise direction, from the...
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
In this assignment, you will be given a functioning program, called minor3.c, that simply reads user...
In this assignment, you will be given a functioning program, called minor3.c, that simply reads user input keys and echoes them back to the screen using the producer-consumer paradigm. The single producer thread reads user input keys and adds them to the shared buffer while two consumer threads read the added keys from the buffer and echo them back to the screen. To complicate matters, each key is read and echoed by exactly one consumer thread. A shared variable, called...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
Program 5: The concept of a 5-digit palindromenumber is a 5-digit number that reads the...
Program 5: The concept of a 5-digit palindrome number is a 5-digit number that reads the same from left to right and from right to left. For example, 12121, 45454, and 14741 are valid 5-digit palindrome numbers. Design (pseudocode) and implement (source code) a program (name it FiveDigitPalindrom) that reads a 5-digit number from the user (as integer value, not string) and then mathematically (using division and remainder operations) determines whether the entered number is a 5-digit palindrome or not....
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT