Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. Write a complete Java program in a class called LargestTwo with one method called main. The program reads in a sequence of integers of specified length and then reports the two largest values given. For this assignment you must store the numbers into an array and you are not allowed to invoke any Java library functions to help you (such as sorting). In class we discussed an algorithm to locate the maximum value in an array. You can take that approach to find the largest element. Then, if you're careful, you can take a very similar approach to find the second largest element. Your code must work in the presence of duplicate values. If there is more than one copy of the largest value, that same value will also be the second-largest. See the test cases for more examples.
  2.  
  3. Test cases will only provide valid input. Your code must work for any valid input. Here is an outline of what the code must do:
  4.  
  5. prompt the user for the count of integers to be given
  6. read in exactly that many integers and store them in an array
  7. print only the largest value and the second-largest value, separated by a space.
  8. Follow the example runs below. Each run is a separate execution of the code -- in other words, your code exits after printing the two values. Program outputs are the English prompts and the final line with two numbers. Program inputs are the rest of the lines.
  9.  
  10. RUN 1:
  11.  
  12. How many numbers (2 or more)?
  13. 4
  14. Enter 4 integers one by one:
  15. 1
  16. 2
  17. 3
  18. 4
  19. 4 3
  20. RUN 2:
  21.  
  22. How many numbers (2 or more)?
  23. 5
  24. Enter 5 integers one by one:
  25. 5
  26. 1
  27. 3
  28. 2
  29. 4
  30. 5 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement