Java Reference
In-Depth Information
5. Write a method called printRange that accepts two integers as arguments and prints the sequence of numbers
between the two arguments, enclosed in square brackets. Print an increasing sequence if the first argument is smaller
than the second; otherwise, print a decreasing sequence. If the two numbers are the same, that number should be
printed between square brackets. Here are some sample calls to printRange :
printRange(2, 7);
printRange(19, 11);
printRange(5, 5);
The output produced from these calls should be the following sequences of numbers:
[2, 3, 4, 5, 6, 7]
[19, 18, 17, 16, 15, 14, 13, 12, 11]
[5]
6. Write a method called smallestLargest that accepts a Scanner for the console as a parameter and asks the user
to enter numbers, then prints the smallest and largest of all the numbers supplied by the user. You may assume that
the user enters a valid number greater than 0 for the number of numbers to read. Here is a sample execution:
How many numbers do you want to enter? 4
Number 1: 5
Number 2: 11
Number 3: - 2
Number 4: 3
Smallest = - 2
Largest = 11
7. Write a method called evenSumMax that accepts a Scanner for the console as a parameter. The method should
prompt the user for a number of integers, then prompt the integer that many times. Once the user has entered all the
integers, the method should print the sum of all the even numbers the user typed, along with the largest even number
typed. You may assume that the user will type at least one nonnegative even integer. Here is an example dialogue:
How many integers? 4
Next integer? 2
Next integer? 9
Next integer? 18
Next integer? 4
Even sum = 24, even max = 18
8. Write a method called printGPA that accepts a Scanner for the console as a parameter and calculates a student's
grade point average. The user will type a line of input containing the student's name, then a number that represents
the number of scores, followed by that many integer scores. Here are two example dialogues:
Enter a student record: Maria 5 72 91 84 89 78
Maria's grade is 82.8
Search WWH ::




Custom Search