Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Develop a class TwoWayTable to hold and print a table of integers. Your class should be capable of holding a rectangular table of any

1. Develop a class TwoWayTable to hold and print a table of integers. Your class should be capable of holding a rectangular table of any size (i.e., 3 rows and 5 columns, 8 rows and 4 columns, etc.). Store the table cells (the integers) in a two-dimensional array of integers. Make sure you copy the actual table values into the array in your class. Dont just copy the reference that comes in through the constructor. Also, store the row sums and column sums in two separate one-dimensional integer arrays of the appropriate size. Finally, hold the grand total, i.e., the sum of all the table entries in an integer variable. Provide a constructor that accepts a two-dimensional integer array as its argument, and a method that computes the row sums, column sums and grand total. Finally, write a toString() method that formats the table as nicely as possible.

Use the following skeleton as a guide:

public class TwoWayTable {

int numRows;

int numCols;

int[][] cell;

int[] rowSum;

int[] colSum;

int grandTotal;

// constructor

TwoWayTable(int[][] data) {

// add code here

setMargins();

}

void setMargins() {

// compute the row and column sums;

// margins is the statisticians term

// for these totals, since they appear

// in the margins

}

public String toString() {

// add code here

}

}

The following test program

public class TestTwoWayTable {

public static void main(String[] args) {

int[][] testArray = {

{2, 5, 6, 3}, {9, 4, 4, 7},

{1, 10, 2, 3}, {8, 4, 5, 3} };

TwoWayTable t = new TwoWayTable(testArray);

System.out.println(t);

}

}

should produce output something like the following:

2 5 6 3 | 16

9 4 4 7 | 24

1 10 2 3 | 16

8 4 5 3 | 20

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

20 23 17 16 | 76

Dont get too carried away with the formatting. Its fairly difficult (well, not difficult, but time consuming) to account for the sizes of the cell values and add padding to make them line up. Try using the printf() method.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Case Studies In Business Data Bases

Authors: James Bradley

1st Edition

0030141346, 978-0030141348

More Books

Students also viewed these Databases questions

Question

5. List the forces that shape a groups decisions

Answered: 1 week ago

Question

4. Identify how culture affects appropriate leadership behavior

Answered: 1 week ago