Question

In: Computer Science

Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...

Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run.

Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on.

Sample runs showing input prompts and outputs are (DO NOT read inputs as String type):

Enter positive integers (0 to quit): 3 4 5 -9 4 2 5 1 -5 2 5 0

You entered: 3 4 5 4 2 5 1 2 5

Largest value: 5

Occurrences: 3 times

Enter positive integers (0 to quit): 3 7 5 -4 4 2 -5 5 1 7 7 0

You entered: 3 7 5 4 2 5 1 7 7

Largest value: 7

Occurrences: 3 times

Document your code, and organize and space out your outputs as shown. Design your program such that it allows the user to re-run the program with different inputs in the same run (i.e., use a sentinel loop structure).

Solutions

Expert Solution

import java.util.Scanner;

public class LargestOccurenceCount {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char ch;
        do {
            System.out.print("Enter positive integers (0 to quit): ");
            int largest = 0, count = 0, num;
            while (true) {
                num = in.nextInt();
                if (num == 0) {
                    break;
                } else if (num > largest) {
                    largest = num;
                    count = 1;
                } else if (num == largest) {
                    count++;
                }
            }
            System.out.println("Largest value:\t" + largest);
            System.out.println("Occurrences:\t" + count + " times");
            System.out.print("Do you want to try again(y or n): ");
            ch = in.next().charAt(0);
            System.out.println();
        } while (ch == 'y' || ch == 'Y');
    }
}


Related Solutions

Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n...
I am coding with NetBeans LDE Java. Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers...
C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT