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...
The question asks to modify this for an unspecified number of months. This is the python...
The question asks to modify this for an unspecified number of months. This is the python code I have, please advise next steps def main(): months = range(1,13) rain_month = 0.0 rain_per_month = [] month = 0 count = 1 amount = 0.0 total_rain = 0.0 average_rain = 0.0 for month in months: print("Rain for month "+str(count)+":") rain_amount = get_rainfall() rain_per_month.append(rain_amount) count = count + 1 total_rain = calc_total_rain(rain_per_month) average_rain = rain_average(total_rain) print("Total Rain:", total_rain) print("Average Rain:", average_rain) def get_rainfall():...
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....
Python! Create a program that does the following: Reads a number from the user. Calls a...
Python! Create a program that does the following: Reads a number from the user. Calls a function that finds all the divisors of that number. Calls another function to see if the number 7 is a divisor of the original number. Keeps reading input from the user and calling the function above until the user enters the letter ‘q’. Create 2 functions findDivisors() and lucky7(). Use docstrings to explain what each function does. Use the help() function to output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT