Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My assignment is to make an empty multiplication table for a set amount of value

ID: 3530572 • Letter: M

Question

My assignment is to make an empty multiplication table for a set amount of values (ie. If the value is 12, then the multiplication table will have twelve rows and twelve columns.) I have written out the code in eclipse but cannot seem to keep the value for the variable theTable from being either null or just empty. Here is the class that I believe the error to exist in:


MultiplicationTableBuilder


package edu.westga.multiplicationtable.model;

/**
* MultiplicationTableBuilder builds a String that represents a multiplication table.
* If the table size is set, the table will be for numbers 1 through the
* specified value; otherwise, it will be for 1 through the default size.
*
* @author
*
*/
public class MultiplicationTableBuilder {

// TODO Define an integer constant named DEFAULT_SIZE with value 10.
private int DEFAULT_SIZE = 10;

private int size;

private String theTable;

/**
* Builds a new MultiplicationTableBuilder object to build a multiplication table for
* numbers 1 through DEFAULT_SIZE.
*
* @precondition none
* @postcondition getSize() == DEFAULT_SIZE
*/
public MultiplicationTableBuilder() {
// TODO Initialize size as DEFAULT_SIZE and theTable as an empty String.
size = DEFAULT_SIZE;
theTable = "";
}

/**
* Builds the multiplication table for numbers 1 through getSize().
*
* @precondition none
* @postcondition getTable() returns the table
*/
public void buildTable() {
// TODO Call helper methods buildHeading and buildRows.
buildHeading();
buildRows();
}

/**
* Returns a String containing the multiplication table for numbers 1
* through getSize().
*
* @return the entire table
*
* @precondition none
*/
public String getTable() {
return theTable;
}


/**
* Returns the size of the table, that is, the highest number in it.
*
* @return the largest number in the table
*
* @precondition none
*/
public int getSize() {
return size;
}

/**
* Resets the size of the table - the highest number in it - to the
* specified value.
*
* @param size the largest number in the table
*
* TODO Write the appropriate precondition, given the precondition check
* in the body of the method.
* @precondition ???
* @postcondition getSize() == size
*/
public void setSize(int size) {
if (size < 1) {
throw new IllegalArgumentException("Row is less than 1");
}
this.size = size;
}



// ********************** private helper methods **************************


private void buildHeading() {
String theHeader = " ";

// TODO Fill in the 3 parts of the counting loop header to
// - initialize columnNumber as an int with value 1,
// - keep looping while columnNumber is less than or equal to getSize(),
// - increment columnNumber after each iteration.

for (int columnNumber = 1; columnNumber <= getSize(); columnNumber++)
theHeader += columnNumber + " ";


theTable += theHeader + " ";

}

private void buildRows() {
// TODO Fill in the 3 parts of the counting loop header to
// - initialize rowNumber as an int with value 1,
// - keep looping while rowNumber is less than or equal to getSize(),
// - increment rowNumber after each iteration.

for (int rowNumber = 1; rowNumber <= getSize(); rowNumber++)
theTable += nextRow(rowNumber);

}

private String nextRow(int rowNumber) {
String theRow = rowNumber + " - ";

// TODO Fill in the 3 parts of the counting loop header to
// - initialize columnNumber as an int with value 1,
// - keep looping while columnNumber is less than or equal to getSize(),
// - increment columnNumber after each iteration.

for (int columnNumber = 1; columnNumber <= getSize(); columnNumber++)
theRow += (rowNumber * columnNumber) + " ";


// TODO Modify the return statement to append a
// newline character onto theRow.
return theRow + " ";
}

}

----------------------------------------------------------------------------------------------------------------


and here are my other classes:


BuildTableController


package edu.westga.multiplicationtable.controllers;

import edu.westga.multiplicationtable.model.MultiplicationTableBuilder;

/**
* Creates a new BuildTableController object to manage the
* specified model object.
*
* @author
*/
public class BuildTableController {

private MultiplicationTableBuilder theBuilder;

/**
* Creates a new BuildTableController for the specified
* MultiplicationTableBuilder model.
*
* @param aBuilder
*
* @precondition aBuilder != null
* @postcondition getBuilder().equals(aBuilder)
*/
public BuildTableController(MultiplicationTableBuilder aBuilder) {
if (aBuilder == null) {
throw new IllegalArgumentException("Null MultiplicationTableBuilder");
}

this.theBuilder = aBuilder;
}

/**
* Tells the MultiplicationTableBuilder to reset the size of the table
* to the specified value.
*
* @param size
* the largest number in the table
*
* @precondition 1 >= size
* @postcondition getBuilder().getSize() == size
*/
public void setTableSize(int size) {
// TODO Write the appropriate condition to check the precondition.
if (size < 0) {
throw new IllegalArgumentException("Table size must be positive.");
}

// TODO Call theBuilder's setSize method.
getBuilder().setSize(size);
}

/**
* Directs the model object to build a multiplication table.
*
* @precondition none
* @postcondition the table has been built
*/
public void buildTable() {
// TODO Call theBuilder's buildTable method.
theBuilder.buildTable();
}


/**
* Returns the MultiplicationTableBuilder managed by this controller.
*
* @return the model object
*
* @precondition none
*/
public MultiplicationTableBuilder getBuilder() {
return theBuilder;
}

}


---------------------------------------------------------------------------------------------------------------


UserInterface


package edu.westga.multiplicationtable.view;

import javax.swing.JOptionPane;

import edu.westga.multiplicationtable.controllers.BuildTableController;
import edu.westga.multiplicationtable.model.MultiplicationTableBuilder;

/**
* UserInterface defines a simple interface that lets the user
* input the size of the multiplication table, and that displays
* the table in the console window.
*
* @author
*/
public class UserInterface {

private MultiplicationTableBuilder theBuilder;
private BuildTableController theController;

/**
* Creates a new UserInterface instance with the specified
* model and controller objects.
*
* @param aBuilder the model instance
* @param aController the controller instance
*
* @precondition aBuilder != null && aController != null
* @postcondition the user interface exists and is ready to run

*/
public UserInterface(MultiplicationTableBuilder aBuilder,
BuildTableController aController) {
if (aBuilder == null) {
throw new IllegalArgumentException("Null MultiplicationTableBuilder");
}
if (aController == null) {
throw new IllegalArgumentException("Null BuildTableController");
}

theBuilder = aBuilder;
theController = aController;
}

/**
* Asks the user to input the size of the multiplication table, tells the
* controller to have the table built, and finally displays the table.
*
* @precondition none
* @postcondition the table has been displayed in the console
*/
public void run() {
// TODO Implement the body of the method:
// - Call the helper method to have the user enter the size.
// - Tell theController to have the size set.
// - Tell theController to have the table built.
// - Print the table returned from theBuilder.

theController.getBuilder().setSize(inputTableSize());
System.out.println(theBuilder.getTable());
}



// ********************** private helper method **************************


private int inputTableSize() {
int size = 0;

// TODO Implement a conditional loop that repeats user input
// as long as the user enters a number less than 1.
String inputString = JOptionPane.showInputDialog(
"Enter the table size (an integer > 0)");
while (Integer.parseInt(inputString) < 1)
{
inputString = JOptionPane.showInputDialog(
"Enter the table size (an integer > 0)");
}
size = Integer.parseInt(inputString);

return size;
}

}


------------------------------------------------------------------------------------------------------------------


ApplicationController


package edu.westga.multiplicationtable.controllers;

import edu.westga.multiplicationtable.model.MultiplicationTableBuilder;
import edu.westga.multiplicationtable.view.UserInterface;

/**
* ApplicationController defines the main method
* for the program.
*
* @author
*/
public class ApplicationController {

/**
* Defines the entry point of the program.
*
* @param args not used
*
* @precondition none
* @postcondition the program has terminated
*/
public static void main(String[] args) {
MultiplicationTableBuilder theBuilder = new MultiplicationTableBuilder();
BuildTableController theController = new BuildTableController(theBuilder);

UserInterface theUi = new UserInterface(theBuilder, theController);
theUi.run();
}

}


**NOTE**

I am not looking to cheat so don't act like I am. I just need help getting the variable "theTable" to equal something other than nothingness. Any and all help is very much appreciated. Thanks in advance :D

Explanation / Answer

You have made a small mistake frend.

In UserInterface class run method.

u have to call buildTable() and then getTabel()

but u were calling directly getTable without building the table.

Hope this helped you rate 5.


i am writing the UserInterface class only. i have ommited the comments to make it mre readable.

u have to add only 1 line in run method thats it.


see below code. i have added that one line

public void run() {
theController.getBuilder().setSize(inputTableSize());


System.out.println("in run");


theBuilder.buildTable();// u had not written this line


System.out.println(theBuilder.getTable());
}



UserInterface class

--------------------------

import javax.swing.JOptionPane;

//import edu.westga.multiplicationtable.controllers.BuildTableController;
//import edu.westga.multiplicationtable.model.MultiplicationTableBuilder;

/**
* UserInterface defines a simple interface that lets the user
* input the size of the multiplication table, and that displays
* the table in the console window.
*
* @author
*/
public class UserInterface {

private MultiplicationTableBuilder theBuilder;
private BuildTableController theController;

public UserInterface(MultiplicationTableBuilder aBuilder,
BuildTableController aController) {
if (aBuilder == null) {
System.out.println("In UserInterface outer abuilder null");
throw new IllegalArgumentException("Null MultiplicationTableBuilder");
}
if (aController == null) {
System.out.println("In UserInterface outer acontroller null");
throw new IllegalArgumentException("Null BuildTableController");
}
System.out.println("In UserInterface outer");
theBuilder = aBuilder;
theController = aController;
}

public void run() {
theController.getBuilder().setSize(inputTableSize());


System.out.println("in run");
theBuilder.buildTable();
System.out.println(theBuilder.getTable());
}



private int inputTableSize() {
int size = 0;

String inputString = JOptionPane.showInputDialog(
"Enter the table size (an integer > 0)");
while (Integer.parseInt(inputString) < 1)
{
inputString = JOptionPane.showInputDialog(
"Enter the table size (an integer > 0)");
}
size = Integer.parseInt(inputString);

return size;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote