Tom will be 63 years old in the year 2050.
- Write a program that generates 10 random numbers between 1 and 5, inclusive. The program will then display the number of occurrences of each number. For example, if the numbers generated are 5, 4, 3, 3, 1, 4, 5, 3, 1, 1 the output will be similar to:
(Concepts reviewed include Random Numbers section 3.7, looping, working with arrays- or using primitive variables- depending on your solution, and displaying output.)
- Write a method called lastIndexOf that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return -1 if the value is not found. For example, in the array [72, 83, 100, 97, 99, 83, 54, 77, 42, 72] the last index of the value 83 is 5. The number to be searched for is entered by the user. Include a main that will call method lastIndexOf(myArray, someValue). You may hardcode the array values when you declare the array rather than having the array contents entered from the console (section 7.2.5). Set your array size at 10 and fill your array with 10 values. Use the length (section 7.2.3) property to determine the size of your array rather than hardcoding the loop invariant.
(Concepts reviewed include I/O from the console, declaring and using an array (Ch. 7) of integers, looping, returning a value from a method.)
- Write a program with the following overloaded methods:
public static void printMe(char c, int n)
public static void printMe(int someInt, int n)
public static void printMe(double someDouble, char c, int n)
public static void printMe(String s, int n)
The method printMe will print the arguments in the method call n times. For example the call printMe(‘a’, 5) will display
The call printMe(5.2,’Z’,3) will display
Include a main that will call each of the printMe methods. You may hardcode the arguments as shown in the examples above rather than gather this information from the user. (Concepts reviewed include methods, section 6.8 overloading, and looping.)
- Book Club Points
This is a review problem from Programming I. You will need to take input from the console, use if statements (or a switch), and print statements.
Serendipity Booksellers has a book club that awards points to its customers based on the number of books purchased each month. The points are awarded as follows:
If a customer purchases 0 books, he or she earns 0 points.
If a customer purchases 1 book, he or she earns 5 points.
If a customer purchases 2 books, he or she earns 15 points.
If a customer purchases 3 books, he or she earns 30 points.
If a customer purchases 4 or more books, he or she earns 60 points.
Write a program that asks the user to enter the number of books that he or she has purchased this month and then displays the number of points awarded.
A Sample Run…
Please enter number of books purchased: 3
You have earned 30 points.
- This problem will be a review of arrays, calling methods, returning values from a method, reading input, and displaying output.
Write a program that do the following (in the order listed):
- Read in 5 integer values from the console and store them in an array.
- Print out the contents of the array.
- Call a method public static void numOdd(int[ ] array) that will display the total number of odd integers in the array. (Use num % 2 to determine even or odd)
- Call a method public static int tripleSum(int[ ] array) that will return three times the sum of the contents of array. Have the main call the method tripleSum and display the result from the the call.
Please enter 5 integers: 4 3 7 5 2
Contents of the array: 4 3 7 5 2
The number of odd integers is: 3
The triple of the sum is 63