Labs
- Create a program with a
mainmethod. Declare and initialize an array for the days of the week, starting with Sunday. - Print the days in order, but do not print Saturday and Sunday. Your logic should exclude these days by checking the array index.
- Do the previous option again, but your logic should check the array element against the String literals
SaturdayandSunday.
(Solutions: DayOfWeekIterating1.java, DayOfWeekIterating2.java)
- In this lab you will see how
forloops and arrays work well together. It looks long, but it is manageable if you go step-by-step. - Write a program to declare an array with space for five
ints. Prompt a user to enter five scores by repeating the promptScore 1:,Score 2:, etc. Store the entries in the correct array index. - Write a method called
calculateAverageto calculate and return the average of all the scores. It should take one parameter and return a number value. - To ensure that the method for calculating scores works, we are going to write a test method.
- Write a method called
public static void testCalculateAverage(). - In the method, declare and initialize an array of four
ints whose average will divide evenly - no decimal place. - Call the
calculateAveragemethod and store the result in a variable. - Use an
ifstatement to check if the result is what you expected. If so, print the messagePassed. If not, print the messageFailedwith the value that you expected and the value returned from the method. - Call the
testCalculateAveragemethod frommain. Fix any errors incalculateAverage. Once the method works, comment out the call totestCalculateAverage.
- Write a method called
- Now that we know calculating the average works, call the
calculateAveragemethod inmainand print the average for the user to see.
(Solution: AverageScores.java)
- Change your program from (2) to ask the user how many scores they are going to enter. Then prompt the user for that number of scores. Use the value the user entered to create your array.
(Solution: AverageScoresPrompt.java)
- Modify the solution. After all of the elements have been read and printed, use a loop to find the largest number entered. Print the array index of the number and the number.
(Solution: AverageScoresLargest.java)
- Write a program whose
mainmethod declares and initializes an array of severalStrings with the names of animals, using{}shorthand initialization. Inmainwrite aforloop that loops 10 times. Each time, it will choose and print the string at a randomly-selected index of the array.
(Solution: RandomString.java)
Practice Exercise¶
You may recall that Math.random() returns a randomly-generated double greater than or equal to 0.0 and less than 1.0. To turn that into an int within a given range:
* Multiply it by the highest number needed plus one.
// Random double from 0.0 through 19.9999...
double d = Math.random() * 20;
int, truncating off the fractional part.
// Convert to integer, now range is 0 through 19:
int index = (int)d;
// Do it in one statement:
index = (int)(Math.random() * 20);