Question

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 for 5 is 4. (Hint: Maintain two variables, max and count. max stores the current max number,and count stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each subsequent number with max. If the number is greater than max,assign it to max and reset count to 1. If the number is equal to max, increment count by 1.)
Here are sample runs of the program:

Sample 1:
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5 The occurrence count of the largest number is 4

Sample 2:
Enter numbers: 3 6 5 4 2 4 5 4 5 5 0
The largest number is 6 The occurrence count of the largest number is 1


Any help given is really appreciated, thanks!!



0 0
Add a comment Improve this question Transcribed image text
Answer #1
Hope this helps,
please rate!


import java.util.Scanner;


public class largestNumber {

/**
* @param args
*/
public static void main(String[] args) {
int max = 0;
int count = 0;
System.out.println("How many numbers would you like to enter?");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[]array = new int[size];
for(int i =0; i<size;i++){
System.out.print("Enter Number: ");
array[i] = sc.nextInt();
}
for(int i = 0; i<size;i++){
if(array[i]>max){
max = array[i];
count = 1;
}
else if(max == array[i]){
count++;
}
}
System.out.println("The largest number is "+max+ " Its occurence is "+count);

}

}
answered by: Safya
Add a comment
Answer #2
import java.util.*;
public class Main
{public static void main(String[] args)
{int max=0,count=0,num;
Scanner in=new Scanner(System.in);
System.out.print("enter numbers: ");
num=in.nextInt();
while(num!=0)
{if(num>max)
{count=1;
max=num;
}
else if(max==num)
count++;
num=in.nextInt();
}
System.out.println("The largest number is "+max+" The occurrence count of the largest number is "+count);
}
}
answered by: Tayllor
Add a comment
Know the answer?
Add Answer to:
Please help with my java program
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
  • 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...

  • (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...

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

  • Write a Java program called Histogram.java that displays a list of distinct characters in an input...

    Write a Java program called Histogram.java that displays a list of distinct characters in an input tile and the occurrence of each eharacte. Your iogram should 1ead an input file name from a use. After that, your program should read characters in the file and display a list of distinct characters and their occurTeces. Finally, your program should draw a veril bafo the occuences For the assignment, your program has.to display the result exactly as the sample run. For instance,...

  • USING PYTHON: Write a program that reads an unspecified number of integers and finds the ones...

    USING PYTHON: Write a program that reads an unspecified number of integers and finds the ones that have the most occurrences. For example, if you enter 2 3 40 3 5 4 –3 3 3 2 0, the number 3 occurs most often. Enter all numbers in one line. If not one but several numbers have the most occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9 30 3 9...

  • Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds

    Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header:public static String convertMillis(long millis)For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, andconvertMillis(555550000) returns the string154:19:10.Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1 and 100 and counts the occurrence of each (you should store the numbers in an array). Output...

  • JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the...

    JAVA - the program should output as follows: Please enter a seed: 2345 Please enter the size of the array: 1 Array size must be greater than 1. Please reenter: 0 Array size must be greater than 1. Please reenter: -1 Array size must be greater than 1. Please reenter: 8 Please choose an option: 1 Print the array 2 Find the average 3 Find the largest element 4 Count how many times 3 occurred 5 Count how many elements...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • 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...

  • any help! my program not working , there is an error but cannot solve it i...

    any help! my program not working , there is an error but cannot solve it i did this program after editing a previous program  and i tried to follow the following ; 1. Instead of printing the prime numbers from 2 to n, your program will print the first n prime numbers. 2. It will be an error if n is less than 1. Exampl: $ ./a 1 2 3 #include<stdio.h> 4 int p; // p is the global variable. 5...

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