# contingency table problem

Printable View

• 05-29-2012, 10:24 AM
wan Toushiro
could anyone help me with this contingency table problem?
I am struggling to find the java code for this problem. Firstly, I am in a beginner level and this question were hard for me to answer. I guess that this used a 2D array right? but i having problem to write the java code for this question. Kindly please help me?i need to submit this java code as soon as possible. thank you very much.

here is the question :

Write a Java program that
1.queries a user for the number of rows and columns of a contingency table,
2.read the data, row by row and
3.displays the data in tabular form along with the row totals, column totals and grand total.

For example if the six data of 2x3 table are

1,3,6,7,9, and 8. the program displays these six numbers together with the appropriate totals as

1 3 6 | 10
7 9 8 | 24
8 12 14 | 34

The character '|' is used to separate the data from the row totals
• 05-29-2012, 12:21 PM
Tolls
Re: contingency table problem
What have you written so far and where are you having problems, including any exceptions or compilation errors, showing which line they occur on.
• 05-29-2012, 08:21 PM
wan Toushiro
Re: contingency table problem
firstly i'm beginner in this java programming and do not know what type of code do i need to do whether it's loop array or etc.. could you guide me to solve this problem? thank you very much.
• 05-30-2012, 09:37 AM
Tolls
Re: contingency table problem
Well, you have 3 steps there, so start from the beginning.
So write the code to do part 1, storing the numRows and numCols in int variables.
Then extend that by writing the code to read in the data, storing that in something suitable (an array sounds good).
Then finally look at part 3.

There's no point looking at the whole problem and trying to figure it out in one go...that way lies madness.
• 05-30-2012, 12:56 PM
wan Toushiro
Re: contingency table problem
i have done until here. then what should i do? how to assign the number(data) that user enter to the rows and columns? please help me?

package javaapplication16;

/**
*
* @author USER
*/import java.util.Scanner;
import java.util.*;

public class JavaApplication16 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

int numRows;
int numCols;
int sizeTable;
Scanner readinput = new Scanner(System.in);

System.out.println(" Enter the size of table: Rows*Columns " );

numRows = readinput.nextInt();
numCols = readinput.nextInt();

sizeTable = numRows*numCols;

System.out.println(" The size of table are : "+numRows+"*"+numCols+"="+sizeTable );
//
int[][] ContigencyTable;
ContigencyTable = new int[numRows][numCols];

}
}
• 05-30-2012, 02:32 PM
Tolls
Re: contingency table problem
So there you go, you now have an (empty) table based on the user input.

Now for part 2.
I have no idea where exactly you are supposed to be reading the data from, but if it's the user then you will need to loop around that array asking the user for numbers, then assigning the result to the bit of the array you are currently on.
• 05-30-2012, 02:59 PM
wan Toushiro
Re: contingency table problem
could you please write me the code for the loop. the data is 1, 3, 4, 7. can you do it for 2 x 2 table.
• 05-30-2012, 03:01 PM
Tolls
Re: contingency table problem
See my sig.
• 05-30-2012, 03:29 PM
wan Toushiro
Re: contingency table problem
i've already email the question.
• 05-30-2012, 03:58 PM
Tolls
Re: contingency table problem
Please do not ask for code as refusal often offends.
• 05-30-2012, 04:05 PM
wan Toushiro
Re: contingency table problem
ok2..sory. what if you suggest the type of loop that i will use that require the data from user like(nested-loop,if-else or etc..)