Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. import javax.swing.JFileChooser;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class Hw11P01 {
  9. static Scanner input = new Scanner(System.in);
  10.  
  11. public static void main(String[] args)
  12. {
  13. int number = 0, grabInt = 0, grabChoice = 0;
  14.  
  15. int integer = userMenu(number);
  16. menuChoices(integer);
  17. }
  18.  
  19.  
  20. public static int userMenu(int number)
  21. {
  22.  
  23. System.out.println("Option 1: Randomly Generate a 2D array");
  24. System.out.println("Option 2: Populate an array using File I/O");
  25. System.out.println("Option 3: Exit program");
  26. System.out.print("Enter your choice here: ");
  27.  
  28. number = input.nextInt();
  29. return number;
  30. }
  31. public static void menuChoices(int integer)
  32. {
  33. switch(integer)
  34. {
  35. case 1:
  36. int[][] array = creatingRandomArray();
  37. printingRandomArray(array);
  38.  
  39. break;
  40.  
  41. case 2:
  42. //asking user to choose a file
  43. JOptionPane.showMessageDialog(null, "Choose a file to calculate from");
  44.  
  45. //creating jfc as a variable
  46. JFileChooser jfc = new JFileChooser();
  47.  
  48. //creating the array and counter variables
  49.  
  50. int counter = 0;
  51. int x = jfc.showOpenDialog(null);
  52. int sizeRow = 0, sizeColumn = 0;
  53.  
  54. //if user approves the file
  55. if(x == JFileChooser.APPROVE_OPTION)
  56. {
  57. //allowing for jfc to pick a file
  58. File newFile = jfc.getSelectedFile();
  59.  
  60. try
  61. {
  62. Scanner in = new Scanner(newFile);
  63. //making the program grab data from the selected file
  64. sizeRow = Integer.parseInt(in.nextLine());
  65. sizeColumn = Integer.parseInt(in.nextLine());
  66.  
  67. float sumOfArray = 0, sumOfMajor = 0, sumOfMinor = 0;
  68. double averageOfMinor = 0, averageOfArray = 0, averageOfMajor = 0;
  69. int array1[][] = new int[sizeRow][sizeColumn];
  70.  
  71. //creating the loop to enter all the data while updating counter
  72. while(in.hasNext())
  73. {
  74. for(int row = 0; row < sizeRow; row++)
  75. {
  76. for(int column = 0; column < sizeColumn; column++)
  77. {
  78. //adding spaces in between the numbers
  79. array1[row][column] = Integer.parseInt(in.nextLine());
  80.  
  81. sumOfArray += array1[row][column];
  82.  
  83.  
  84.  
  85. //insert the methods that solve the problem in this loop
  86. }
  87. sumOfMajor += array1[row][row];
  88. }
  89. for(int row = sizeRow - 1; row != 0; row--)
  90. {
  91. sumOfMinor += array1[row][row];
  92. }
  93. }
  94. averageOfArray = (sumOfArray / (sizeRow * sizeColumn));
  95. averageOfMajor = (sumOfMajor / (sizeRow));
  96. averageOfMinor = (sumOfMinor / (sizeRow));
  97.  
  98. printingRandomArray(array1);
  99. System.out.println();
  100. System.out.println("The total sum of the array is " + sumOfArray);
  101. System.out.println("The total average of the array is " + roundingNumbers(averageOfArray));
  102. sumOfRows(array1, sizeRow, sizeColumn);
  103. sumOfColumns(array1, sizeRow, sizeColumn);
  104. System.out.println("The total sum of the major diagonal is " + sumOfMajor);
  105. System.out.println("The total average of the major diagonal is " + roundingNumbers(averageOfMajor));
  106. System.out.println("The total sum of the minor diagonal is " + sumOfMinor);
  107. System.out.println("The total average of the minor diagonal is " + roundingNumbers(averageOfMinor));
  108. System.out.println();
  109. }
  110. //catches errors
  111. catch(IOException e)
  112. {
  113. System.err.println(e);
  114. System.exit(1);
  115. }
  116. }
  117. //cancels the jfc
  118. else if(x == JFileChooser.CANCEL_OPTION)
  119. {
  120. JOptionPane.showMessageDialog(null, "Cancelled");
  121. System.exit(1);
  122. }
  123. //tells the user when there was an error
  124. else if(x == JFileChooser.ERROR_OPTION)
  125. {
  126. JOptionPane.showMessageDialog(null, "You caused an error.");
  127. System.exit(1);
  128. }
  129. break;
  130.  
  131. case 3:
  132. System.out.println("You have exited the program!");
  133. System.exit(1);
  134.  
  135. default:
  136. System.out.print("The entered value is not an option!");
  137. break;
  138.  
  139. }
  140. }
  141. public static double roundingNumbers(double rounded)
  142. {
  143. rounded = Math.round(rounded * 100);
  144. rounded = rounded/100;
  145. return rounded;
  146. }
  147.  
  148. public static int[][] creatingRandomArray()
  149. {
  150. System.out.print("You Have Chosen to Randomly Generate a 2D array\n");
  151. System.out.print("How many rows would you like in this array?: ");
  152. int rows1 = input.nextInt();
  153. System.out.print("How many columns would you like in this array?: ");
  154. int columns1 = input.nextInt();
  155. System.out.print("What will be the minimum for the array?: ");
  156. int minimum = input.nextInt();
  157. System.out.print("What will be the maximum for the array?: ");
  158. int maximum = input.nextInt();
  159.  
  160. int [][] arr = new int[rows1][columns1];
  161.  
  162. for(int row = 0; row < arr.length; row++)
  163. {
  164. for(int column = 0; column < arr[row].length; column++)
  165. {
  166. //creating the random number generator
  167. int randomNumber = (int)(maximum * Math.random() + minimum);
  168. //adding spaces in between the numbers
  169. arr[row][column] = randomNumber;
  170.  
  171. }
  172. }
  173.  
  174. return arr;
  175. }
  176.  
  177. public static void printingRandomArray(int[][] arr)
  178. {
  179. for(int row = 0; row < arr.length; row++)
  180. {
  181. for(int column = 0; column < arr[row].length; column++)
  182. {
  183. System.out.print(arr[row][column] + "\t");
  184. }
  185. System.out.println();
  186. }
  187. }
  188.  
  189. public static void sumOfRows(int[][] arr, int sizeRow, int sizeColumn)
  190. {
  191. double sum = 0;
  192.  
  193. for(int row = 0; row < sizeRow; row++)
  194. {
  195. sum = 0;
  196. for(int column = 0; column < sizeColumn; column++)
  197. {
  198.  
  199. sum += arr[row][column];
  200. }
  201. System.out.println("The sum of row " + (row + 1) + " is "+ sum + " and the average is " + avgOfRows(arr, sizeRow, sum));
  202.  
  203. }
  204. System.out.println("The max average of rows is " + maxOfRows(arr, sizeRow, sum));
  205.  
  206. }
  207.  
  208. public static void sumOfColumns(int[][] arr, int sizeRow, int sizeColumn)
  209. {
  210. float sum1 = 0;
  211.  
  212. for(int column = 0; column < sizeColumn; column++)
  213. {
  214. sum1 = 0;
  215. for(int row = 0; row < sizeRow; row++)
  216. {
  217.  
  218. sum1 += arr[row][column];
  219. }
  220. System.out.println("The sum of column " + (column + 1) + " is "+ sum1 + " and the average is " + avgOfColumns(arr, sizeColumn, sum1));
  221. }
  222. System.out.println("The max average of columns is " + maxOfColumns(arr, sizeColumn, sum1));
  223. }
  224.  
  225. public static double avgOfColumns(int[][] arr, int sizeColumn, float sum1)
  226. {
  227. double avg = 0;
  228. double max = 0;
  229.  
  230. for(int column = 0; column < sizeColumn; column++)
  231. {
  232. avg = (sum1 / sizeColumn);
  233. avg = roundingNumbers(avg);
  234. }
  235. if(max < avg)
  236. {
  237. max = avg;
  238. }
  239. return avg;
  240. }
  241.  
  242. public static double maxOfColumns(int[][] arr, int sizeColumn, double sum)
  243. {
  244. double avg = 0;
  245. double max = 0;
  246.  
  247. for(int column = 0; column < sizeColumn; column++)
  248. {
  249. avg = (sum / sizeColumn);
  250. avg = roundingNumbers(avg);
  251. }
  252. if(max < avg)
  253. {
  254. max = avg;
  255. }
  256. return max;
  257. }
  258.  
  259. public static double maxOfRows(int[][] arr, int sizeRows, double sum)
  260. {
  261. double avg = 0;
  262. double max = 0;
  263.  
  264. for(int column = 0; column < sizeRows; column++)
  265. {
  266. avg = (sum / sizeRows);
  267. avg = roundingNumbers(avg);
  268. }
  269. if(avg > max)
  270. {
  271. max = avg;
  272. }
  273. return max;
  274. }
  275.  
  276. public static double avgOfRows(int[][] arr, int sizeRow, double sum)
  277. {
  278. double avg = 0;
  279.  
  280. for(int row = 0; row < sizeRow; row++)
  281. {
  282. avg = (sum / sizeRow);
  283. avg = roundingNumbers(avg);
  284. }
  285.  
  286. return avg;
  287. }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement