Question

In: Computer Science

This is the code that needs to be completed... Thank you! public class MultiplicationTable { private...

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()
{

}
}

Solutions

Expert Solution

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.


Related Solutions

I need this code translated to C code. Thank you. public class FourColorTheorem { public static...
I need this code translated to C code. Thank you. public class FourColorTheorem { public static boolean isPrime(int num) { // Corner case if (num <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < num; i++) if (num % i == 0) return false; return true; } public static void main(String[] args) { int squares[] = new int[100]; for (int i = 1; i < squares.length; i++) squares[i-1] = i * i;...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /**...
All Tasks are commented in the code: public class MandelbrotUtil { private MandelbrotUtil() { } /** * Return the number of iterations needed to determine if z(n + 1) = z(n) * z(n) + c * remains bounded where z(0) = 0 + 0i. z(n + 1) is bounded if its magnitude * is less than or equal to 2. Returns 1 if the magnitude of c * is greater than 2. Returns max if the magnitude * of z(n...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Below is some code for a rectangle class that needs to be completed. Add member function...
Below is some code for a rectangle class that needs to be completed. Add member function declarations to the class declaration and member function definitions below the declaration. For the accessor functions, you can add the definitions directly into the class declaration. The goal is to code the class so that it works wthout changing the main program #include <iostream> using namespace std; // rectangle has a vertical height and horizontal width // The class below is a rectangle. It...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT