Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. public class TwinArray {
  2.  
  3. private int [][] numbers;
  4.  
  5. public TwinArray(int[][] nums){
  6. numbers = new int [nums.length][nums[0].length]; //because you need to go through one row to
  7. //know how many columns are in it
  8. for(int row = 0; row < nums.length;row++){
  9. for(int col = 0; col < nums[row].length;col++){
  10. numbers[row][col] = nums[row][col];
  11. }
  12. }
  13. }
  14. // END CONSTRUCTOR
  15. //=================================================================================
  16. public int getLength(){
  17. return numbers[0].length;
  18. }
  19.  
  20.  
  21. //====================================================================
  22.  
  23. public int getTotal(){
  24. int sum = 0;
  25.  
  26. for(int row = 0;row < numbers.length;row++){
  27. for(int col = 0; col < numbers[row].length;col++){
  28. sum = sum + numbers[row][col];
  29. }
  30. }
  31. return sum;
  32. }
  33.  
  34. //=================================================================================
  35. public double getAverage(){
  36. int sum = 0;
  37. double avg =0;
  38.  
  39. for(int row = 0; row < numbers.length;row++){
  40. for(int col = 0; col < numbers[row].length;col++){
  41. sum = sum + numbers[row][col];
  42. avg = sum / numbers[0].length;
  43. }
  44. }
  45. return avg;
  46. }
  47.  
  48. //===========================================================================
  49. //The second argument should be the subscript of a row in the array.
  50. //The method should return the total of the values in the specified row.
  51.  
  52. public int getRowTotal(int num){
  53. int row = num;
  54. int totalRow = 0;
  55.  
  56. for(int i = 0; i < numbers[row].length; i++){
  57. totalRow = totalRow + numbers[row][i];
  58. }
  59. return totalRow;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement