Question: Create a single class, called Lab13. All of your work for this lab will go into this single class. Create a main method. Inside your
Create a single class, called Lab13. All of your work for this lab will go into this single class.
Create a main method. Inside your main method create a Lab13 object. You will use this object to execute the methods that you will create in this lab.
Create a method called printRectangle() that returns void, and accepts two parameters as ints: rows and columns. Your method will use nested loops to print a grid of * characters. You should use the parameters passed to your method. See R6.27 for help with your loops. For example, if you make the call:
//lab13 is a Lab13 object created in step 2.
lab13.printRectangle(3, 4);
You should see the following output:
Add code to your main method in order to test your printRectangle() method.
Add a method called printArray() that returns void, and accepts a single parameter, an int array. Your method header should look something like this:
void printArray(int[] a)
The name of the array is a. Remember you can use a.length to determine how long an array is. Use a for loop in your printArray() method to print all the elements of the array in a single column. Your output should look like this (once you complete step 6):
In your main method declare and initialize an array called primes that holds the values of the first 5 prime numbers. (See self check 7.1). Then call your printArray() method, and use the primes array as your parameter.
Create a method called getRandomArray() that accepts 1 parameter as an int, and returns an array of ints. Your method header will look like this:
int[] getRandomArray(int size)
(Notice how we indicate that this method returns an array of ints, and not just a single int). The job of this method will be to fill an array with random values between 0 and 99 (inclusive) and then return this array. (You return an array just like any other variable. If your array is called myArray, you will write a statement return myArray; at the end of your method).
You will need to take the following steps to finish this method:
Declare an array using the size parameter.
Create a random number generator (you will have to import java.util.Random at the start of your file).
Create a for loop that is based on the size of the array you created in step a.
At each iteration of the loop you need to create a random value (use the nextInt() method of the Random class), and assign this value to the current element in the array. (If your array is called a, then your first iteration will set a[0], your second iteration will set a[1], and so forth).
At the end of your method, and outside the loop, return the array.
Now we have a method to create an array of random values, and a method to print arrays, so use them together in your main method. You can use something like the following sequence of code to create an array with 10 random values, and then print it.
[] randomArray = lab13.getRandomArray(10);
.printArray(randomArray);
Create a method called printEvens(). This method should have a return type of void, and accept an int array as a parameter. This method will need to iterate over the input parameter, and print the current element only if it is even. Additionally, you should keep a count of the number of even elements found in your loop. Outside of your loop you should print the number of even values your method found. (We will use this method in step 10).
Use the random array created in step 8 as an input parameter to the printEvens() method. You should have a printout similar to the following (starting from step 8).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
