Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CIS111b;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- /**
- * Created by Charles on 2/25/2015.
- */
- /*
- Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year.
- The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following
- methods:
- 1.) getData: This method reads and stores the data in the two-dimensional array.
- 2.) averageHigh; This method calculates and returns the average high temp of the year.
- 3.) averageLow: This method calcs and returns the average low temp of the year.
- 4.) indexHighTemp: This method returns the index of the highest temperature in the array.
- 5.) indexLowTemp: This method returns the index of the lowest temp in the array.
- */
- public class ArrayAssignmentClient {
- public static void main(String[] args) throws FileNotFoundException {
- int array[][] = getData();
- System.out.println("The average high of the year is: " + averageHigh(array));
- System.out.println("The average low of the year is: " + averageLow(array));
- System.out.println("The highest temperature is: " + indexHighTemp(array));
- System.out.println("The lowest temperature is: " + indexLowTemp(array));
- }
- /* public static int[][] getData() {
- // i - row j - collum
- int[][] data = {{40, 44, 53, 64, 74, 83, 87, 85, 78, 67, 56, 45},
- {26, 28, 34, 44, 54, 64, 69, 68, 60, 48, 39, 30}};
- return data;
- }
- */
- public static int[][] getData() throws FileNotFoundException {
- Scanner input = new Scanner(new File("TEMP.txt"));
- int[][] array = new int[2][12];
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[i].length; j++) {
- array[i][j] = input.nextInt();
- }
- }
- return array;
- }
- /*
- public static int [][] getData(){
- Scanner keyboard = new Scanner(System.in);
- int array[][] = new int[2][12];
- System.out.print("Please enter the first high temp of each month, then then the low value: \n\n\n");
- for (int i = 0; i <array.length ; i++) {
- for (int j = 0; j < array[i].length ; j++) {
- System.out.print("> ");
- array[i][j] = keyboard.nextInt();
- }
- }
- return array;
- }
- */
- public static int averageHigh(int[][] data) { // average high = 64.667, rounds down as an int
- int total = 0;
- int average = 0;
- for (int i = 0; i < data[0].length; i++) {
- total += data[0][i];
- average = total / data[0].length;
- }
- return average;
- }
- public static int averageLow(int[][] data) { //average low = 46
- int total = 0;
- int average = 0;
- for (int i = 0; i < data[1].length; i++) {
- total += data[1][i];
- average = total / data[1].length;
- }
- return average;
- }
- public static int indexHighTemp(int[][] data) {
- int tempVal;
- int maxIndex = 0;
- for (int i = 0; i < data[0].length; i++) {
- tempVal = data[0][i];
- if (tempVal > maxIndex) {
- maxIndex = data[0][i];
- }
- }
- return maxIndex;
- }
- public static int indexLowTemp(int[][] data) {
- int tempVal;
- int lowIndex = Integer.MAX_VALUE;
- for (int i = 0; i < data[1].length; i++) {
- tempVal = data[1][i];
- if (lowIndex > tempVal) {
- lowIndex = data[1][i];
- }
- }
- return lowIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement