Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. package CIS111b;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.Scanner;
  7.  
  8. /**
  9. * Created by Charles on 2/25/2015.
  10. */
  11.  
  12.  
  13.  
  14. /*
  15. Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year.
  16. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following
  17. methods:
  18.  
  19. 1.) getData: This method reads and stores the data in the two-dimensional array.
  20. 2.) averageHigh; This method calculates and returns the average high temp of the year.
  21. 3.) averageLow: This method calcs and returns the average low temp of the year.
  22. 4.) indexHighTemp: This method returns the index of the highest temperature in the array.
  23. 5.) indexLowTemp: This method returns the index of the lowest temp in the array.
  24. */
  25. public class ArrayAssignmentClient {
  26.  
  27. public static void main(String[] args) throws FileNotFoundException {
  28.  
  29. int array[][] = getData();
  30. System.out.println("The average high of the year is: " + averageHigh(array));
  31. System.out.println("The average low of the year is: " + averageLow(array));
  32. System.out.println("The highest temperature is: " + indexHighTemp(array));
  33. System.out.println("The lowest temperature is: " + indexLowTemp(array));
  34.  
  35.  
  36. }
  37.  
  38. /* public static int[][] getData() {
  39. // i - row j - collum
  40.  
  41. int[][] data = {{40, 44, 53, 64, 74, 83, 87, 85, 78, 67, 56, 45},
  42. {26, 28, 34, 44, 54, 64, 69, 68, 60, 48, 39, 30}};
  43.  
  44. return data;
  45. }
  46. */
  47.  
  48. public static int[][] getData() throws FileNotFoundException {
  49. Scanner input = new Scanner(new File("TEMP.txt"));
  50.  
  51. int[][] array = new int[2][12];
  52.  
  53.  
  54. for (int i = 0; i < array.length; i++) {
  55. for (int j = 0; j < array[i].length; j++) {
  56. array[i][j] = input.nextInt();
  57. }
  58. }
  59. return array;
  60. }
  61. /*
  62. public static int [][] getData(){
  63. Scanner keyboard = new Scanner(System.in);
  64. int array[][] = new int[2][12];
  65.  
  66. System.out.print("Please enter the first high temp of each month, then then the low value: \n\n\n");
  67. for (int i = 0; i <array.length ; i++) {
  68. for (int j = 0; j < array[i].length ; j++) {
  69. System.out.print("> ");
  70. array[i][j] = keyboard.nextInt();
  71.  
  72.  
  73. }
  74.  
  75. }
  76. return array;
  77. }
  78. */
  79.  
  80.  
  81. public static int averageHigh(int[][] data) { // average high = 64.667, rounds down as an int
  82. int total = 0;
  83. int average = 0;
  84. for (int i = 0; i < data[0].length; i++) {
  85. total += data[0][i];
  86. average = total / data[0].length;
  87. }
  88. return average;
  89. }
  90.  
  91. public static int averageLow(int[][] data) { //average low = 46
  92. int total = 0;
  93. int average = 0;
  94. for (int i = 0; i < data[1].length; i++) {
  95. total += data[1][i];
  96. average = total / data[1].length;
  97. }
  98. return average;
  99.  
  100. }
  101.  
  102. public static int indexHighTemp(int[][] data) {
  103. int tempVal;
  104. int maxIndex = 0;
  105. for (int i = 0; i < data[0].length; i++) {
  106. tempVal = data[0][i];
  107. if (tempVal > maxIndex) {
  108. maxIndex = data[0][i];
  109. }
  110.  
  111. }
  112. return maxIndex;
  113. }
  114.  
  115.  
  116. public static int indexLowTemp(int[][] data) {
  117. int tempVal;
  118. int lowIndex = Integer.MAX_VALUE;
  119. for (int i = 0; i < data[1].length; i++) {
  120.  
  121. tempVal = data[1][i];
  122. if (lowIndex > tempVal) {
  123. lowIndex = data[1][i];
  124.  
  125. }
  126.  
  127. }
  128. return lowIndex;
  129. }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement