In: Computer Science
This is the code that needs to be completed... Thank you!
public class MultiplicationTable
{
private int[][] table;
private int rows;
private int cols;
/* Instantiate the two dimensional array named table.
* Assign the numRows parameter to the rows field.
* Assign the numCols parameter in the cols field.
*/
public MultiplicationTable(int numRows, int numCols)
{
}
/* Using nested for loops, fill the table array with
* the values shown in the Multiplication Table document.
*/
public void fillArray()
{
}
/* Using nested for loops, print the table so it looks like
the
* table shown in the Multiplication Table document.
*/
public void printArray()
{
}
}
public class MultiplicationTable
{
private int[][] table;
private int rows;
private int cols;
public MultiplicationTable(int numRows, int numCols)
{
int i,j;
rows=numRows;
cols=numCols;
table= new int[numRows][numCols];
for(i=0;i<numRows;i++){
for(j=0;j<numCols;j++){
table[i][j]=0;
}
}
}
public void fillArray()
{
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
table[i][j]=i*j;
}
}
}
public void printArray()
{
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
System.out.print(table[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
}
}
In the problem, we're given some private members and we need to access and modify them from the public methods of the class.
The first function, initiates the table array, for this we use new keyword. And every entry is initialized with 0 using for loops.
Similarly the second function implements the multiplication table , where each entry in the table is the product of row no and col no as i*j.
And finally the 3rd function prints the values using nested for loops.