Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TwinArray {
- private int [][] numbers;
- public TwinArray(int[][] nums){
- numbers = new int [nums.length][nums[0].length]; //because you need to go through one row to
- //know how many columns are in it
- for(int row = 0; row < nums.length;row++){
- for(int col = 0; col < nums[row].length;col++){
- numbers[row][col] = nums[row][col];
- }
- }
- }
- // END CONSTRUCTOR
- //=================================================================================
- public int getLength(){
- return numbers[0].length;
- }
- //====================================================================
- public int getTotal(){
- int sum = 0;
- for(int row = 0;row < numbers.length;row++){
- for(int col = 0; col < numbers[row].length;col++){
- sum = sum + numbers[row][col];
- }
- }
- return sum;
- }
- //=================================================================================
- public double getAverage(){
- int sum = 0;
- double avg =0;
- for(int row = 0; row < numbers.length;row++){
- for(int col = 0; col < numbers[row].length;col++){
- sum = sum + numbers[row][col];
- avg = sum / numbers[0].length;
- }
- }
- return avg;
- }
- //===========================================================================
- //The second argument should be the subscript of a row in the array.
- //The method should return the total of the values in the specified row.
- public int getRowTotal(int num){
- int row = num;
- int totalRow = 0;
- for(int i = 0; i < numbers[row].length; i++){
- totalRow = totalRow + numbers[row][i];
- }
- return totalRow;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement