Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 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. //Inserting the scanner
  10. static Scanner input = new Scanner(System.in);
  11.  
  12. //main method
  13. public static void main(String[] args)
  14. {
  15. int number = 0;
  16.  
  17. int integer = userMenu(number);
  18. //printing the method
  19. menuChoices(integer);
  20. }
  21.  
  22. //method for usermenu and user input
  23. public static int userMenu(int number)
  24. {
  25.  
  26. System.out.println("Option 1: Randomly Generate a 2D array");
  27. System.out.println("Option 2: Populate an array using File I/O");
  28. System.out.println("Option 3: Exit program");
  29. System.out.print("Enter your choice here: ");
  30.  
  31. number = input.nextInt();
  32. return number;
  33. }
  34.  
  35. //method for the menu choices
  36. public static void menuChoices(int integer)
  37. {
  38. //inserting a switch for the options
  39. switch(integer)
  40. {
  41. case 1:
  42. //printing the array and results
  43. int[][] array = creatingRandomArray();
  44. printingRandomArray(array);
  45.  
  46. break;
  47.  
  48. case 2:
  49. //asking user to choose a file
  50. JOptionPane.showMessageDialog(null, "Choose a file to calculate from");
  51.  
  52. //creating jfc as a variable
  53. JFileChooser jfc = new JFileChooser();
  54.  
  55. //creating the array and counter variables
  56.  
  57. int counter = 0;
  58. int x = jfc.showOpenDialog(null);
  59. int sizeRow = 0, sizeColumn = 0;
  60.  
  61. //if user approves the file
  62. if(x == JFileChooser.APPROVE_OPTION)
  63. {
  64. //allowing for jfc to pick a file
  65. File newFile = jfc.getSelectedFile();
  66.  
  67. try
  68. {
  69. Scanner in = new Scanner(newFile);
  70. //making the program grab data from the selected file
  71. sizeRow = Integer.parseInt(in.nextLine());
  72. sizeColumn = Integer.parseInt(in.nextLine());
  73.  
  74. float sumOfArray = 0, sumOfMajor = 0, sumOfMinor = 0;
  75. double averageOfMinor = 0, averageOfArray = 0, averageOfMajor = 0;
  76. int array1[][] = new int[sizeRow][sizeColumn];
  77.  
  78. //creating the loop to enter all the data while updating counter
  79. while(in.hasNext())
  80. {
  81. for(int row = 0; row < sizeRow; row++)
  82. {
  83. for(int column = 0; column < sizeColumn; column++)
  84. {
  85. //adding spaces in between the numbers
  86. array1[row][column] = Integer.parseInt(in.nextLine());
  87.  
  88. sumOfArray += array1[row][column];
  89.  
  90.  
  91.  
  92. //insert the methods that solve the problem in this loop
  93. }
  94. sumOfMajor += array1[row][row];
  95. }
  96. for(int row = sizeRow - 1; row != 0; row--)
  97. {
  98. sumOfMinor += array1[row][row];
  99. }
  100. }
  101. //average formulas
  102. averageOfArray = (sumOfArray / (sizeRow * sizeColumn));
  103. averageOfMajor = (sumOfMajor / (sizeRow));
  104. averageOfMinor = (sumOfMinor / (sizeRow));
  105.  
  106. //printing all the results
  107. printingRandomArray(array1);
  108. System.out.println();
  109. System.out.println("The total sum of the array is " + sumOfArray);
  110. System.out.println("The total average of the array is " + roundingNumbers(averageOfArray));
  111. sumOfRows(array1, sizeRow, sizeColumn);
  112. sumOfColumns(array1, sizeRow, sizeColumn);
  113. System.out.println("The total sum of the major diagonal is " + sumOfMajor);
  114. System.out.println("The total average of the major diagonal is " + roundingNumbers(averageOfMajor));
  115. System.out.println("The total sum of the minor diagonal is " + sumOfMinor);
  116. System.out.println("The total average of the minor diagonal is " + roundingNumbers(averageOfMinor));
  117. }
  118. //catches errors
  119. catch(IOException e)
  120. {
  121. System.err.println(e);
  122. System.exit(1);
  123. }
  124. }
  125. //cancels the jfc
  126. else if(x == JFileChooser.CANCEL_OPTION)
  127. {
  128. JOptionPane.showMessageDialog(null, "Cancelled");
  129. System.exit(1);
  130. }
  131. //tells the user when there was an error
  132. else if(x == JFileChooser.ERROR_OPTION)
  133. {
  134. JOptionPane.showMessageDialog(null, "You caused an error.");
  135. System.exit(1);
  136. }
  137. break;
  138.  
  139. case 3:
  140. System.out.println("You have exited the program!");
  141. System.exit(1);
  142.  
  143. default:
  144. System.out.print("The entered value is not an option!");
  145. break;
  146.  
  147. }
  148. }
  149. //method for rounding the numbers
  150. public static double roundingNumbers(double rounded)
  151. {
  152. rounded = Math.round(rounded * 100);
  153. rounded = rounded/100;
  154. return rounded;
  155. }
  156.  
  157. //creating the method for asking for user input of array parameters
  158. public static int[][] creatingRandomArray()
  159. {
  160. float sumOfArray = 0, sumOfMajor = 0, sumOfMinor = 0;
  161. double averageOfMinor = 0, averageOfArray = 0, averageOfMajor = 0;
  162.  
  163. System.out.print("You Have Chosen to Randomly Generate a 2D array\n");
  164. System.out.print("How many rows would you like in this array?: ");
  165. int rows1 = input.nextInt();
  166. System.out.print("How many columns would you like in this array?: ");
  167. int columns1 = input.nextInt();
  168. System.out.print("What will be the minimum for the array?: ");
  169. int minimum = input.nextInt();
  170. System.out.print("What will be the maximum for the array?: ");
  171. int maximum = input.nextInt();
  172.  
  173. int [][] arr = new int[rows1][columns1];
  174.  
  175. //loops to input random numberes into the array
  176. for(int row = 0; row < arr.length; row++)
  177. {
  178. for(int column = 0; column < arr[row].length; column++)
  179. {
  180. //creating the random number generator
  181. int randomNumber = (int)(maximum * Math.random() + minimum);
  182. //adding spaces in between the numbers
  183. arr[row][column] = randomNumber;
  184. sumOfArray += arr[row][column];
  185. }
  186. sumOfMajor += arr[row][row];
  187. }
  188. for(int row = rows1 - 1; row != 0; row--)
  189. {
  190. sumOfMinor += arr[row][row];
  191. }
  192. //average formoulas
  193. averageOfMajor = (sumOfMajor / (rows1));
  194. averageOfMinor = (sumOfMinor / (rows1));
  195. averageOfArray = (sumOfArray / (rows1 * columns1));
  196.  
  197. //printing out the results
  198. System.out.println("The total sum of the array is " + sumOfArray);
  199. System.out.println("The total average of the array is " + roundingNumbers(averageOfArray));
  200. sumOfRows1(arr, rows1, columns1);
  201. sumOfColumns1(arr, rows1, columns1);
  202.  
  203. System.out.println("The total sum of the major diagonal is " + sumOfMajor);
  204. System.out.println("The total average of the major diagonal is " + roundingNumbers(averageOfMajor));
  205. System.out.println("The total sum of the minor diagonal is " + sumOfMinor);
  206. System.out.println("The total average of the minor diagonal is " + roundingNumbers(averageOfMinor));
  207.  
  208. return arr;
  209. }
  210.  
  211. //method for the rows number 2!!!
  212. public static void sumOfRows1(int[][] arr, int rows1, int columns1)
  213. {
  214. double sum = 0;
  215.  
  216. for(int row = 0; row < rows1; row++)
  217. {
  218. sum = 0;
  219. for(int column = 0; column < columns1; column++)
  220. {
  221.  
  222. sum += arr[row][column];
  223. }
  224. System.out.println("The sum of row " + (row + 1) + " is "+ sum + " and the average is " + avgOfRows(arr, rows1, sum));
  225.  
  226. }
  227. System.out.println("The max average of rows is " + maxOfRows(arr, rows1, sum));
  228.  
  229. }
  230. //method for the columns number 2!!!!
  231. public static void sumOfColumns1(int[][] arr, int rows1, int columns1)
  232. {
  233. float sum1 = 0;
  234.  
  235. for(int column = 0; column < columns1; column++)
  236. {
  237. sum1 = 0;
  238. for(int row = 0; row < rows1; row++)
  239. {
  240.  
  241. sum1 += arr[row][column];
  242. }
  243. System.out.println("The sum of column " + (column + 1) + " is "+ sum1 + " and the average is " + avgOfColumns(arr, columns1, sum1));
  244. }
  245. System.out.println("The max average of columns is " + maxOfColumns(arr, columns1, sum1));
  246. }
  247.  
  248. //printing the random array
  249. public static void printingRandomArray(int[][] arr)
  250. {
  251. for(int row = 0; row < arr.length; row++)
  252. {
  253. for(int column = 0; column < arr[row].length; column++)
  254. {
  255. System.out.print(arr[row][column] + "\t");
  256. }
  257. System.out.println();
  258.  
  259. }
  260. }
  261.  
  262. //finding the sum of the numbers and average
  263. public static void sumOfRows(int[][] arr, int sizeRow, int sizeColumn)
  264. {
  265. double sum = 0;
  266.  
  267. for(int row = 0; row < sizeRow; row++)
  268. {
  269. sum = 0;
  270. for(int column = 0; column < sizeColumn; column++)
  271. {
  272.  
  273. sum += arr[row][column];
  274. }
  275. System.out.println("The sum of row " + (row + 1) + " is "+ sum + " and the average is " + avgOfRows(arr, sizeRow, sum));
  276.  
  277. }
  278. System.out.println("The max average of rows is " + maxOfRows(arr, sizeRow, sum));
  279.  
  280. }
  281.  
  282.  
  283. //finding the sum of the columns and average
  284. public static void sumOfColumns(int[][] arr, int sizeRow, int sizeColumn)
  285. {
  286. float sum1 = 0;
  287.  
  288. for(int column = 0; column < sizeColumn; column++)
  289. {
  290. sum1 = 0;
  291. for(int row = 0; row < sizeRow; row++)
  292. {
  293.  
  294. sum1 += arr[row][column];
  295. }
  296. System.out.println("The sum of column " + (column + 1) + " is "+ sum1 + " and the average is " + avgOfColumns(arr, sizeColumn, sum1));
  297. }
  298. System.out.println("The max average of columns is " + maxOfColumns(arr, sizeColumn, sum1));
  299. }
  300. //method for average of columns
  301. public static double avgOfColumns(int[][] arr, int sizeColumn, float sum1)
  302. {
  303. double avg = 0;
  304. double max = 0;
  305.  
  306. for(int column = 0; column < sizeColumn; column++)
  307. {
  308. avg = (sum1 / sizeColumn);
  309. avg = roundingNumbers(avg);
  310. if(max < avg)
  311. {
  312. max = avg;
  313. }
  314. }
  315.  
  316. return max;
  317. }
  318.  
  319. //method for average of rows
  320. public static double avgOfRows(int[][] arr, int sizeRow, double sum)
  321. {
  322. double avg = 0;
  323.  
  324. for(int row = 0; row < sizeRow; row++)
  325. {
  326. avg = (sum / sizeRow);
  327. avg = roundingNumbers(avg);
  328. }
  329.  
  330. return avg;
  331. }
  332.  
  333. //method to find the max within the columns
  334. public static double maxOfColumns(int[][] arr, int sizeColumn, double sum)
  335. {
  336. double avg = 0;
  337. double max = 0;
  338.  
  339. for(int column = 0; column < sizeColumn; column++)
  340. {
  341. avg = (sum / sizeColumn);
  342. avg = roundingNumbers(avg);
  343. if(max < avg)
  344. {
  345. max = avg;
  346. }
  347. }
  348.  
  349. return max;
  350. }
  351.  
  352. //method for finding max within rows
  353. public static double maxOfRows(int[][] arr, int sizeRows, double sum)
  354. {
  355. double avg = 0;
  356. double max = 0;
  357.  
  358. for(int row = 0; row < sizeRows; row++)
  359. {
  360. avg = (sum / sizeRows);
  361. avg = roundingNumbers(avg);
  362. }
  363. if(avg > max)
  364. {
  365. max = avg;
  366. }
  367. return max;
  368. }
  369.  
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement