Question
Download the file RandomArrayTemplate.java and save it as RandomArray.java before using it. The supplied TestLab4Bronze.java program will expect it to be named this way. This
-
Download the file RandomArrayTemplate.java and save it as RandomArray.java before using it. The
supplied TestLab4Bronze.java program will expect it to be named this way.
-
This class creates rectangular two-dimensional arrays filled with random positive integer values. Some of the class is written for you. Complete it by adding code in the four places marked //***ADD YOUR CODE HERE***.
-
Add a private instance variable that will hold a 2-dimensional array of integers.
-
Complete the constructor, which will create the 2-dimensional array, with the specified number of rows and columns, containing values from 0 to range-1. The code to generate such random numbers is supplied in the comments.
-
Complete the getRow method, which will return a copy (clone or deep copy) of the specified row of the array, as an array of integers of the correct length. In this method, use the built-in System.arraycopy method to copy the values from the original array into the result, as demonstrated in class.
-
Complete the getCol method, which will return a copy (clone or deep copy) of the specified column of the array, as an array of integers of the correct length. In this method, you will have to copy the values yourself using a for loop since the System.arraycopy method cannot be used in this situation.
-
-
Run the supplied TestLab4Bronze.java program to test your class. Sample output from this program is shown below. User input is in blue. Your numbers will be different.
How many rows? 3 How many columns? 4 The rows contain: Row 0: [15, 92, 48, 87] Row 1: [74, 32, 24, 10] Row 2: [55, 0, 50, 14]
The columns contain: Column 0: [15, 74, 55] Column 1: [92, 32, 0] Column 2: [48, 24, 50] Column 3: [87, 10, 14]
RandomArrayTemplate.java
/** * A class for rectangular arrays full of random * positive integers. */
public class RandomArray { //An instance variable to hold an array of integers //****YOUR CODE HERE**** public RandomArray(int rows, int cols, int range) { /* Construct a random array with the given number of * rows and columns, filled with random integers in * the range 0..range-1. * Use the expression (int)(range*Math.random()) */ //****YOUR CODE HERE**** }//constructor
public int[] getRow(int r){ //Return a copy (clone) of row r of the array //****YOUR CODE HERE**** return null; //DUMMY - REMOVE }//getRow method public int[] getCol(int c){ //Return an int[] array containing the numbers from column c. //****YOUR CODE HERE**** return null; //DUMMY - REMOVE }//getCol method
}//RandomArray class
TestLab4Bronze.java
/** * Lab 4 COMP 1020 * This file will test the * RandomArray class. */ import java.util.Scanner; import java.util.Arrays;
public class TestLab4Bronze { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //Values from 0..RANGE-1 should be generated final int RANGE = 100; //Ask the user for a number of rows and columns System.out.print("How many rows? "); int rows = keyboard.nextInt(); System.out.print("How many columns? "); int cols = keyboard.nextInt(); //Create an array with that number of rows and columns RandomArray a = new RandomArray(rows, cols, RANGE); //Print out the rows, one by one System.out.println("The rows contain:"); for(int r=0; r //Print out the columns, one by one System.out.println(" The columns contain:"); for(int c=0; c }//main }//TestLab4Bronze
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started