Question

Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out t

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.text.DecimalFormat;
import java.util.Objects;
import java.util.Scanner;

public class Main {

  
public static void main(String args[]) {
  
double avg; //average
int count =0; //count of largest no
int largest=-1; //largest no
int total = 0; //total input numbers
int sum = 0; //sum of all numbers

while(true) {
System.out.print("Enter an integer, or quit with Q or q: ");
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()){
int num = sc.nextInt();
total++;
sum = sum + num; //calculate sum of all numbers
if(num>largest) {
largest = num; //got largest no
count = 1;
} else if(num == largest) {
count++; //increase count of largest no.
}
} else {
String input = sc.nextLine();
if(Objects.equals(input, "q")||Objects.equals(input, "Q")){ //check if q/Q is there
break;
}
}
}
System.out.println("The largest number is: " + largest);
System.out.println("The count for the largest number is: " + count);
avg = (double)sum/total;
DecimalFormat d = new DecimalFormat("#0.00"); //avg upto 2 decimal places1
System.out.println("The average is: " + d.format(avg));
  
}
}

Enter an integer, or quit with 2 or q 1 Enter an integer, or quit with 2 or q 2 Enter an integer, or quit with 2 or q 3 EnterEnter an integer, or quit with 2 or q 3 Enter an integer, or quit with 2 or q 5 Enter an integer, or quit with 2 or q 2 Enter

Add a comment
Know the answer?
Add Answer to:
Problem 4: Write a Java program that reads positive integers, find the largest of them, count...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with my java program

    I'm in my first java class at school and really need some help. I've looked online for different programs for the same assignment I'm doing now, and none of them seemto work. Here is the assignment:Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 55 5 0; the program finds that the largest is 5 and the occurrence count...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume...

    Write a program that reads integers, finds the smallest of them, and counts its occurrences. Assume that the input ends with number -1. Suppose that you entered 4 2 9 2 2 -1; the program finds that the smallest is 2 and the occurrence count for 2 is 3. (Hint: Maintain two variables, min and count. min stores the current min number, and count stores its occurrences. Initially, assign the first number to min and 1 to count. Compare each...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value have been read, and computes the total and average of the input values (not counting zeros. Your program ends with the input 0. Display the average as a floating point number Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and...

    (Count occurrence of numbers) Write a program that reads some integers between 1 and 100 and counts the occurrences of each. Here is a sample run of the program: Enter integers between 1 and 100: 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time 5 occurs 2 times 6 occurs 1 time 23 occurs 1 time 43 occurs 1 time 2 5 6 5 4 3 23 43 2 Note that if a number occurs more than...

  • Please write a Java program: Given an array of positive integers, return a count of the...

    Please write a Java program: Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20]) - 2 countEvens([3, 7, 1, 11]) 0 Go Save, Compile, Run (ctrl-enter) int countEvens (int[] nums) {

  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT